git: ea0f267c8bf0 - main - stand/efi: move G(x) and M(x) macros to loader_efi.h

From: Warner Losh <imp_at_FreeBSD.org>
Date: Thu, 05 Dec 2024 00:18:55 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=ea0f267c8bf07bafc53a50fe52e7cdf42e63c3f2

commit ea0f267c8bf07bafc53a50fe52e7cdf42e63c3f2
Author:     Ahmad Khalifa <ahmadkhalifa570@gmail.com>
AuthorDate: 2024-09-27 16:01:27 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-12-05 00:19:17 +0000

    stand/efi: move G(x) and M(x) macros to loader_efi.h
    
    These are often needed for copying to the staging area or just general
    page table calculations on amd64.
    
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1446
---
 stand/efi/loader/arch/amd64/elf64_freebsd.c  | 9 ++++-----
 stand/efi/loader/arch/i386/elf64_freebsd.c   | 3 ---
 stand/efi/loader/copy.c                      | 3 ---
 stand/efi/loader/loader_efi.h                | 4 ++++
 stand/kboot/kboot/arch/amd64/elf64_freebsd.c | 9 ++++-----
 5 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/stand/efi/loader/arch/amd64/elf64_freebsd.c b/stand/efi/loader/arch/amd64/elf64_freebsd.c
index 3883c309e724..c4265aca035e 100644
--- a/stand/efi/loader/arch/amd64/elf64_freebsd.c
+++ b/stand/efi/loader/arch/amd64/elf64_freebsd.c
@@ -101,8 +101,7 @@ elf64_exec(struct preloaded_file *fp)
 	ehdr = (Elf_Ehdr *)&(md->md_data);
 
 	trampcode = copy_staging == COPY_STAGING_ENABLE ?
-	    (vm_offset_t)0x0000000040000000 /* 1G */ :
-	    (vm_offset_t)0x0000000100000000; /* 4G */;
+	    (vm_offset_t)G(1) : (vm_offset_t)G(4);
 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
 	if (EFI_ERROR(err)) {
@@ -117,7 +116,7 @@ elf64_exec(struct preloaded_file *fp)
 	trampoline = (void *)trampcode;
 
 	if (copy_staging == COPY_STAGING_ENABLE) {
-		PT4 = (pml4_entry_t *)0x0000000040000000; /* 1G */
+		PT4 = (pml4_entry_t *)G(1);
 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
 		if (EFI_ERROR(err)) {
@@ -154,11 +153,11 @@ elf64_exec(struct preloaded_file *fp)
 			/*
 			 * The L2 page slots are mapped with 2MB pages for 1GB.
 			 */
-			PT2[i] = (pd_entry_t)i * (2 * 1024 * 1024);
+			PT2[i] = (pd_entry_t)i * M(2);
 			PT2[i] |= PG_V | PG_RW | PG_PS;
 		}
 	} else {
-		PT4 = (pml4_entry_t *)0x0000000100000000; /* 4G */
+		PT4 = (pml4_entry_t *)G(4);
 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,
 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
 		if (EFI_ERROR(err)) {
diff --git a/stand/efi/loader/arch/i386/elf64_freebsd.c b/stand/efi/loader/arch/i386/elf64_freebsd.c
index 2c6be607dc4d..0dc16437d905 100644
--- a/stand/efi/loader/arch/i386/elf64_freebsd.c
+++ b/stand/efi/loader/arch/i386/elf64_freebsd.c
@@ -71,9 +71,6 @@ struct gdtr {
 #define GDT_RW	0x00020000000000
 #define GDT_L	0x20000000000000
 
-#define M(x)	((x) * 1024 * 1024)
-#define G(x)	(1ULL * (x) * 1024 * 1024 * 1024)
-
 typedef uint64_t p4_entry_t;
 typedef uint64_t p3_entry_t;
 typedef uint64_t p2_entry_t;
diff --git a/stand/efi/loader/copy.c b/stand/efi/loader/copy.c
index 90a55089ffc4..485ec152ae21 100644
--- a/stand/efi/loader/copy.c
+++ b/stand/efi/loader/copy.c
@@ -35,9 +35,6 @@
 
 #include "loader_efi.h"
 
-#define	M(x)	((x) * 1024 * 1024)
-#define	G(x)	(1ULL * (x) * 1024 * 1024 * 1024)
-
 #if defined(__amd64__)
 #include <machine/cpufunc.h>
 #include <machine/specialreg.h>
diff --git a/stand/efi/loader/loader_efi.h b/stand/efi/loader/loader_efi.h
index b2d7817b87dd..be991eeb9573 100644
--- a/stand/efi/loader/loader_efi.h
+++ b/stand/efi/loader/loader_efi.h
@@ -41,6 +41,10 @@ enum {
 extern int copy_staging;
 #endif
 
+/* Useful for various calculations */
+#define	M(x)	((x) * 1024 * 1024)
+#define	G(x)	(1ULL * (x) * 1024 * 1024 * 1024)
+
 extern EFI_LOADED_IMAGE *boot_img;
 
 int	efi_autoload(void);
diff --git a/stand/kboot/kboot/arch/amd64/elf64_freebsd.c b/stand/kboot/kboot/arch/amd64/elf64_freebsd.c
index b0efbfe09629..495c4c0b320e 100644
--- a/stand/kboot/kboot/arch/amd64/elf64_freebsd.c
+++ b/stand/kboot/kboot/arch/amd64/elf64_freebsd.c
@@ -209,8 +209,7 @@ elf64_exec(struct preloaded_file *fp)
 
 #ifdef EFI
 	trampcode = copy_staging == COPY_STAGING_ENABLE ?
-	    (vm_offset_t)0x0000000040000000 /* 1G */ :
-	    (vm_offset_t)0x0000000100000000; /* 4G */;
+	    (vm_offset_t)G(1) : (vm_offset_t)G(4);
 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
 	if (EFI_ERROR(err)) {
@@ -234,7 +233,7 @@ elf64_exec(struct preloaded_file *fp)
 
 #ifdef EFI
 	if (copy_staging == COPY_STAGING_ENABLE) {
-		PT4 = (pml4_entry_t *)0x0000000040000000; /* 1G */
+		PT4 = (pml4_entry_t *)G(1);
 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
 		if (EFI_ERROR(err)) {
@@ -271,11 +270,11 @@ elf64_exec(struct preloaded_file *fp)
 			/*
 			 * The L2 page slots are mapped with 2MB pages for 1GB.
 			 */
-			PT2[i] = (pd_entry_t)i * (2 * 1024 * 1024);
+			PT2[i] = (pd_entry_t)i * M(2);
 			PT2[i] |= PG_V | PG_RW | PG_PS;
 		}
 	} else {
-		PT4 = (pml4_entry_t *)0x0000000100000000; /* 4G */
+		PT4 = (pml4_entry_t *)G(4);
 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,
 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
 		if (EFI_ERROR(err)) {