From fd2ef8ef5a054d9f26b6b36ea56a5414c7683e39 Mon Sep 17 00:00:00 2001 From: Maxim Sobolev Date: Wed, 25 Nov 2020 23:19:01 +0000 Subject: [PATCH] Unobfuscate "KERNLOAD" parameter on amd64. This change lines-up amd64 with the i386 and the rest of supported architectures by defining KERNLOAD in the vmparam.h and getting rid of magic constant in the linker script, which albeit documented via comment but isn't programmatically accessible at a compile time. Use KERNLOAD to eliminate another (matching) magic constant 100 lines down inside unremarkable TU "copy.c" 3 levels deep in the EFI loader tree. Reviewed by: markj Approved by: markj MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D27355 --- stand/efi/loader/copy.c | 8 +++----- sys/amd64/amd64/genassym.c | 1 + sys/amd64/amd64/locore.S | 3 ++- sys/amd64/include/vmparam.h | 8 ++++++++ sys/conf/ldscript.amd64 | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/stand/efi/loader/copy.c b/stand/efi/loader/copy.c index 1185c94740d..17a4c57f865 100644 --- a/stand/efi/loader/copy.c +++ b/stand/efi/loader/copy.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #if defined(__i386__) || defined(__amd64__) #include #include +#include /* * The code is excerpted from sys/x86/x86/identcpu.c: identify_cpu(), @@ -89,8 +90,6 @@ running_on_hyperv(void) return (1); } -#define KERNEL_PHYSICAL_BASE (2*1024*1024) - static void efi_verify_staging_size(unsigned long *nr_pages) { @@ -134,12 +133,11 @@ efi_verify_staging_size(unsigned long *nr_pages) start = p->PhysicalStart; end = start + p->NumberOfPages * EFI_PAGE_SIZE; - if (KERNEL_PHYSICAL_BASE < start || - KERNEL_PHYSICAL_BASE >= end) + if (KERNLOAD < start || KERNLOAD >= end) continue; available_pages = p->NumberOfPages - - ((KERNEL_PHYSICAL_BASE - start) >> EFI_PAGE_SHIFT); + ((KERNLOAD - start) >> EFI_PAGE_SHIFT); break; } diff --git a/sys/amd64/amd64/genassym.c b/sys/amd64/amd64/genassym.c index f643eeeb07b..f89c53e7550 100644 --- a/sys/amd64/amd64/genassym.c +++ b/sys/amd64/amd64/genassym.c @@ -115,6 +115,7 @@ ASSYM(val_KPML4I, KPML4I); ASSYM(val_PML4PML4I, PML4PML4I); ASSYM(VM_MAXUSER_ADDRESS, VM_MAXUSER_ADDRESS); ASSYM(KERNBASE, KERNBASE); +ASSYM(KERNLOAD, KERNLOAD); ASSYM(DMAP_MIN_ADDRESS, DMAP_MIN_ADDRESS); ASSYM(DMAP_MAX_ADDRESS, DMAP_MAX_ADDRESS); diff --git a/sys/amd64/amd64/locore.S b/sys/amd64/amd64/locore.S index a9a7b5f3972..3addc3fb10c 100644 --- a/sys/amd64/amd64/locore.S +++ b/sys/amd64/amd64/locore.S @@ -36,8 +36,9 @@ /* * Compiled KERNBASE location */ - .globl kernbase, loc_PTmap, loc_PDmap, loc_PDPmap, dmapbase, dmapend + .globl kernbase, kernload, loc_PTmap, loc_PDmap, loc_PDPmap, dmapbase, dmapend .set kernbase,KERNBASE + .set kernload,KERNLOAD .set dmapbase,DMAP_MIN_ADDRESS .set dmapend,DMAP_MAX_ADDRESS diff --git a/sys/amd64/include/vmparam.h b/sys/amd64/include/vmparam.h index 5d3010e2e25..c7ffb218dd4 100644 --- a/sys/amd64/include/vmparam.h +++ b/sys/amd64/include/vmparam.h @@ -148,6 +148,14 @@ #define PA_LOCK_COUNT 256 #endif +/* + * Kernel physical load address. Needs to be aligned at 2MB superpage + * boundary. + */ +#ifndef KERNLOAD +#define KERNLOAD 0x200000 +#endif + /* * Virtual addresses of things. Derived from the page directory and * page table indexes from pmap.h for precision. diff --git a/sys/conf/ldscript.amd64 b/sys/conf/ldscript.amd64 index d62b531454e..c11ffb6ea49 100644 --- a/sys/conf/ldscript.amd64 +++ b/sys/conf/ldscript.amd64 @@ -5,8 +5,8 @@ ENTRY(btext) SEARCH_DIR("/usr/lib"); SECTIONS { + kernphys = kernload; /* Read-only sections, merged into text segment: */ - kernphys = 0x200000; /* 2MB superpage size */ . = kernbase + kernphys + SIZEOF_HEADERS; /* * Use the AT keyword in order to set the right LMA that contains