Skip to content

Commit d5cdf4c

Browse files
nivedita76Ingo Molnar
authored and
Ingo Molnar
committed
efi/x86: Don't relocate the kernel unless necessary
Add alignment slack to the PE image size, so that we can realign the decompression buffer within the space allocated for the image. Only relocate the kernel if it has been loaded at an unsuitable address: - Below LOAD_PHYSICAL_ADDR, or - Above 64T for 64-bit and 512MiB for 32-bit For 32-bit, the upper limit is conservative, but the exact limit can be difficult to calculate. Signed-off-by: Arvind Sankar <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 964124a commit d5cdf4c

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

arch/x86/boot/tools/build.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,17 @@ static void update_pecoff_text(unsigned int text_start, unsigned int file_sz,
238238

239239
pe_header = get_unaligned_le32(&buf[0x3c]);
240240

241-
#ifdef CONFIG_EFI_MIXED
242241
/*
243-
* In mixed mode, we will execute startup_32() at whichever offset in
244-
* memory it happened to land when the PE/COFF loader loaded the image,
245-
* which may be misaligned with respect to the kernel_alignment field
246-
* in the setup header.
242+
* The PE/COFF loader may load the image at an address which is
243+
* misaligned with respect to the kernel_alignment field in the setup
244+
* header.
247245
*
248-
* In order for startup_32 to safely execute in place at this offset,
249-
* we need to ensure that the CONFIG_PHYSICAL_ALIGN aligned allocation
250-
* it creates for the page tables does not extend beyond the declared
251-
* size of the image in the PE/COFF header. So add the required slack.
246+
* In order to avoid relocating the kernel to correct the misalignment,
247+
* add slack to allow the buffer to be aligned within the declared size
248+
* of the image.
252249
*/
253250
bss_sz += CONFIG_PHYSICAL_ALIGN;
254251
init_sz += CONFIG_PHYSICAL_ALIGN;
255-
#endif
256252

257253
/*
258254
* Size of code: Subtract the size of the first sector (512 bytes)

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include "efistub.h"
1919

20+
/* Maximum physical address for 64-bit kernel with 4-level paging */
21+
#define MAXMEM_X86_64_4LEVEL (1ull << 46)
22+
2023
static efi_system_table_t *sys_table;
2124
extern const bool efi_is64;
2225
extern u32 image_offset;
@@ -718,6 +721,7 @@ unsigned long efi_main(efi_handle_t handle,
718721
struct boot_params *boot_params)
719722
{
720723
unsigned long bzimage_addr = (unsigned long)startup_32;
724+
unsigned long buffer_start, buffer_end;
721725
struct setup_header *hdr = &boot_params->hdr;
722726
efi_status_t status;
723727
unsigned long cmdline_paddr;
@@ -729,10 +733,33 @@ unsigned long efi_main(efi_handle_t handle,
729733
efi_exit(handle, EFI_INVALID_PARAMETER);
730734

731735
/*
732-
* If the kernel isn't already loaded at the preferred load
733-
* address, relocate it.
736+
* If the kernel isn't already loaded at a suitable address,
737+
* relocate it.
738+
*
739+
* It must be loaded above LOAD_PHYSICAL_ADDR.
740+
*
741+
* The maximum address for 64-bit is 1 << 46 for 4-level paging. This
742+
* is defined as the macro MAXMEM, but unfortunately that is not a
743+
* compile-time constant if 5-level paging is configured, so we instead
744+
* define our own macro for use here.
745+
*
746+
* For 32-bit, the maximum address is complicated to figure out, for
747+
* now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
748+
* KASLR uses.
749+
*
750+
* Also relocate it if image_offset is zero, i.e. we weren't loaded by
751+
* LoadImage, but we are not aligned correctly.
734752
*/
735-
if (bzimage_addr - image_offset != hdr->pref_address) {
753+
754+
buffer_start = ALIGN(bzimage_addr - image_offset,
755+
hdr->kernel_alignment);
756+
buffer_end = buffer_start + hdr->init_size;
757+
758+
if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
759+
(IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
760+
(IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
761+
(image_offset == 0 && !IS_ALIGNED(bzimage_addr,
762+
hdr->kernel_alignment))) {
736763
status = efi_relocate_kernel(&bzimage_addr,
737764
hdr->init_size, hdr->init_size,
738765
hdr->pref_address,

0 commit comments

Comments
 (0)