Skip to contentSkip to conversation

Use load address to calculate kernel entry

PATCH Superseded [email protected] 3 participants 10 messages Dec 5, 2023

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Use load address to calculate kernel entry

From: Wang Yao <[email protected]>

1, The PIE kernel will be loaded anywhere, so use load address to calculate kernel entry address.

2, Use twice efi_relocate_kernel() to make load address is not equal to link address for test.

Wang Yao (2):
efi/loongarch: Use load address to calculate kernel entry address efi/loongarch: load address is not equal to link address

 arch/loongarch/include/asm/efi.h              |  2 +-
 drivers/firmware/efi/libstub/loongarch-stub.c | 16 +++++++++++++---
 drivers/firmware/efi/libstub/loongarch.c      |  6 +++---
 3 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v3 1/2 efi/loongarch: Use load address to calculate kernel entry address

From: Wang Yao <[email protected]>

The efi_relocate_kernel() may load the PIE kernel to anywhere, the loaded address may not be equal to link address or EFI_KIMG_PREFERRED_ADDRESS.

---

v2->v3:
Dropped TO_CACHE:
kernel_addr may be DA/PG address, both access kernel entry.

v1->v2:
Use link address that from DOS header to calculate kernel entry, corrected by Huacai Chen.

 arch/loongarch/include/asm/efi.h              | 2 +-
 drivers/firmware/efi/libstub/loongarch-stub.c | 4 ++--
 drivers/firmware/efi/libstub/loongarch.c      | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)
arch/loongarch/include/asm/efi.h +1 -1
index 091897d40b03..91d81f9730ab 100644
@@ -32,6 +32,6 @@ static inline unsigned long efi_get_kimg_min_align(void)
 
 #define EFI_KIMG_PREFERRED_ADDRESS	PHYSADDR(VMLINUX_LOAD_ADDRESS)
 
-unsigned long kernel_entry_address(void);
+unsigned long kernel_entry_address(unsigned long kernel_addr);
 
 #endif /* _ASM_LOONGARCH_EFI_H */
drivers/firmware/efi/libstub/loongarch-stub.c +2 -2
index 72c71ae201f0..d6ec5d4b8dbe 100644
@@ -35,9 +35,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	return status;
 }
 
-unsigned long kernel_entry_address(void)
+unsigned long kernel_entry_address(unsigned long kernel_addr)
 {
 	unsigned long base = (unsigned long)&kernel_offset - kernel_offset;
 
-	return (unsigned long)&kernel_entry - base + VMLINUX_LOAD_ADDRESS;
+	return (unsigned long)&kernel_entry - base + kernel_addr;
 }
drivers/firmware/efi/libstub/loongarch.c +3 -3
index 807cba2693fc..0e0aa6cda73f 100644
@@ -37,9 +37,9 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
 	return EFI_SUCCESS;
 }
 
-unsigned long __weak kernel_entry_address(void)
+unsigned long __weak kernel_entry_address(unsigned long kernel_addr)
 {
-	return *(unsigned long *)(PHYSADDR(VMLINUX_LOAD_ADDRESS) + 8);
+	return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
 }
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
@@ -73,7 +73,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
 	csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
 
-	real_kernel_entry = (void *)kernel_entry_address();
+	real_kernel_entry = (void *)kernel_entry_address(kernel_addr);
 
 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
 			  (unsigned long)efi_system_table);
Signature · 1 line
2.27.0
#2 lore raw

PATCH v3 2/2 efi/loongarch: load address is not equal to link address

In reply to [email protected]

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -1
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_handle_t image_handle)
 {
 	efi_status_t status;
-	unsigned long kernel_addr = 0;
+	unsigned long kernel_addr = 0, tmp_addr = 0;
 
 	kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+	/*
+	 * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+	 * test, this will lead to the kernel be loaded at 0x4000000.
+	 */
+	status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+		     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+	efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+	/* kernel_addr == 0x400000 */
 	status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
 		     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+	efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
 	*image_addr = kernel_addr;
 	*image_size = kernel_asize;
Signature · 1 line
2.27.0
maintainer
#3 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Why send a test patch?

Quoted 42 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

#4 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 3 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:

Why send a test patch?

Confirm the accuracy of the testing method

Quoted 43 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

maintainer
#5 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 7 lines · Ainux Wang
Quoted 3 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:

Why send a test patch?

Confirm the accuracy of the testing method

Don't use maillist to do this.

Quoted 44 lines
Quoted 44 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

#6 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 11 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:18写道:
Quoted 7 lines · Ainux Wang
Quoted 3 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:

Why send a test patch?

Confirm the accuracy of the testing method

Don't use maillist to do this.

Sorry, I don't know about this :-(

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

maintainer
#7 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 7 lines · Ainux Wang
Quoted 5 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:18写道:
Quoted 3 lines · Ainux Wang
Quoted 1 line · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Why send a test patch?

Confirm the accuracy of the testing method

Don't use maillist to do this.

Sorry, I don't know about this :-(

But from this patch, you only test the vmlinux.efi not loaded at link address, but haven't tested the vmlinuz.efi not decompressed to the link address. You should test that, but don't submit patches for that.

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
Quoted 46 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

#8 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 12 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 15:41写道:
Quoted 10 lines · Ainux Wang
Quoted 8 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:18写道:
Quoted 6 lines · Ainux Wang
Quoted 3 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:

Why send a test patch?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Confirm the accuracy of the testing method

Don't use maillist to do this.

Sorry, I don't know about this :-(

But from this patch, you only test the vmlinux.efi not loaded at link address, but haven't tested the vmlinuz.efi not decompressed to the link address. You should test that, but don't submit patches for that.

vmlinuz.efi decompress at 0x300000 is ok.

Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0

maintainer Acked-by
#9 lore raw

Re: [PATCH v3 2/2] efi/loongarch: load address is not equal to link address

Quoted 16 lines · Ainux Wang
Quoted 14 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 15:41写道:
Quoted 12 lines · Ainux Wang
Quoted 10 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:18写道:
Quoted 7 lines · Ainux Wang
Quoted 3 lines · Huacai Chen <[email protected]> 于2023年12月5日周二 14:09写道:

Why send a test patch?

Confirm the accuracy of the testing method

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Don't use maillist to do this.

Sorry, I don't know about this :-(

But from this patch, you only test the vmlinux.efi not loaded at link address, but haven't tested the vmlinuz.efi not decompressed to the link address. You should test that, but don't submit patches for that.

vmlinuz.efi decompress at 0x300000 is ok.

Then you can resend the first patch as v4, add:

Huacai

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines · On Tue, Dec 5, 2023 at 2:04 PM <[email protected]> wrote:
Quoted 42 lines

From: Wang Yao <[email protected]>

This is a test patch for PIE kernel that load address is not equal to link address.

---
 drivers/firmware/efi/libstub/loongarch-stub.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch-stub.c +11 -2
index d6ec5d4b8dbe..c13c022d18cf 100644
@@ -22,12 +22,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
                                 efi_handle_t image_handle)
 {
        efi_status_t status;
-       unsigned long kernel_addr = 0;
+       unsigned long kernel_addr = 0, tmp_addr = 0;
 
        kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
+       /*
+        * Allocate 2M space at 0x200000(EFI_KIMG_PREFERRED_ADDRESS) for
+        * test, this will lead to the kernel be loaded at 0x4000000.
+        */
+       status = efi_relocate_kernel(&tmp_addr, 0x100000, 0x200000,
+                    EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] tmp_addr: 0x%lx\n", __func__, tmp_addr);
+
+       /* kernel_addr == 0x400000 */
        status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
                     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
+       efi_info("@@@[%s] kernel_addr: 0x%lx\n", __func__, kernel_addr);
 
        *image_addr = kernel_addr;
        *image_size = kernel_asize;
--

2.27.0