From: Zhou Guanghui zhouguanghui1@huawei.com
When a CDM node is deployed, limit the random address range of the kernel to be moved when the kaslr is enabled to ensure that the kernel is not moved to the CDM node.
Signed-off-by: Zhou Guanghui zhouguanghui1@huawei.com Signed-off-by: Zhang Zekun zhangzekun11@huawei.com Reviewed-by: Weilong Chen chenweilong@huawei.com --- drivers/firmware/efi/libstub/efi-stub-helper.c | 3 +++ drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/randomalloc.c | 11 +++++++++++ 3 files changed, 15 insertions(+)
diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 537db49c31b9..7268d05d367a 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -23,6 +23,7 @@ bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE); bool efi_noinitrd; int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT; bool efi_novamap = IS_ENABLED(CONFIG_LOONGARCH); /* LoongArch call svam() in kernel */; +bool efi_hascdm;
static bool efi_nosoftreserve; static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); @@ -216,6 +217,8 @@ efi_status_t efi_parse_options(char const *cmdline) efi_loglevel = CONSOLE_LOGLEVEL_QUIET; } else if (!strcmp(param, "noinitrd")) { efi_noinitrd = true; + } else if (!strcmp(param, "cdm-nodes")) { + efi_hascdm = true; } else if (!strcmp(param, "efi") && val) { efi_nochunk = parse_option_str(val, "nochunk"); efi_novamap = parse_option_str(val, "novamap"); diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index db009721f543..1a7fe8c967b4 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -34,6 +34,7 @@ extern bool efi_nokaslr; extern bool efi_noinitrd; extern int efi_loglevel; extern bool efi_novamap; +extern bool efi_hascdm;
extern const efi_system_table_t *efi_system_table;
diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index 724155b9e10d..66221b75d8e0 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -85,6 +85,17 @@ efi_status_t efi_random_alloc(unsigned long size,
slots = get_entry_num_slots(md, size, ilog2(align)); MD_NUM_SLOTS(md) = slots; + + /* + * If CDM nodes are deployed, the memory_map's end address + * which exceeds 1GB is not included in the total slots + * calculation. This ensures that the start address does + * not exceed 1 GB during subsequent random address calculation. + */ + if (efi_hascdm && + (md->phys_addr + md->num_pages * EFI_PAGE_SIZE) > 0x40000000) + continue; + total_slots += slots; }