git: 2b3598753e20 - stable/13 - stand: Move md_copymodules into modinfo.c and reduce copies

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:12:37 UTC
The branch stable/13 has been updated by imp:

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

commit 2b3598753e203b5a418cafdcd7a1e981d82d0657
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-09-16 15:08:47 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:34 +0000

    stand: Move md_copymodules into modinfo.c and reduce copies
    
    md_copymodules, bi_copymdoules, bi_copymodules32 (x2) and
    bi_copymodules64 (x2) are all the same routine... Replace them all with
    md_copymodules. This saves about 800 bytes on i386 BIOS loader, which is
    a nice bonus.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D36572
    
    (cherry picked from commit 5d1531d9d4e7d1b1b706ab23ac3f864416e87522)
    
    stand: fix mismerge
    
    Remove stray line from mismerge of 5d1531d9d4e7. This is no longer
    needed.
    
    Fixes:                  5d1531d9d4e7
    Sponsored by:           Netflix
    
    (cherry picked from commit dd2b9c2967767b19bcbc33de0e487405c612f59f)
    
    stand/efi: Call md_copymodules based on __LP64__ to fix 32-bit arm
    
    When I refactored everything, I neglected to pass in the proper is64
    value on 32-bit platforms. This corrects that. This prevented armv7 and
    armv6 platforms from booting due to misaligned data in the kernel.  The
    only platform we support 32-bit booting in armv[67], which I apparently
    neglected to test before commiting my refactoring.
    
    Tested by:              skibo
    Fixes:                  5d1531d9d4e7d
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37095
    
    (cherry picked from commit e0c3f66b4d5f0282e9c7c4803c4cd26b5a388a38)
---
 stand/common/metadata.c              | 44 ------------------
 stand/common/modinfo.c               | 86 ++++++++++++++++++++++++++++++++++++
 stand/common/modinfo.h               |  2 +
 stand/efi/loader/bootinfo.c          | 45 ++++---------------
 stand/i386/libi386/bootinfo32.c      | 35 +--------------
 stand/i386/libi386/bootinfo64.c      | 38 +---------------
 stand/loader.mk                      |  1 +
 stand/userboot/userboot/bootinfo32.c | 34 +-------------
 stand/userboot/userboot/bootinfo64.c | 38 +---------------
 9 files changed, 105 insertions(+), 218 deletions(-)

diff --git a/stand/common/metadata.c b/stand/common/metadata.c
index 99b67cfd1060..11a5276d0705 100644
--- a/stand/common/metadata.c
+++ b/stand/common/metadata.c
@@ -93,49 +93,6 @@ md_copyenv(vm_offset_t addr)
     return(addr);
 }
 
-static int align;
-#define MOD_ALIGN(l)	roundup(l, align)
-
-static vm_offset_t
-md_copymodules(vm_offset_t addr, int kern64)
-{
-    struct preloaded_file	*fp;
-    struct file_metadata	*md;
-    uint64_t			scratch64;
-    uint32_t			scratch32;
-    int				c;
-
-    c = addr != 0;
-    /* start with the first module on the list, should be the kernel */
-    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-
-	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
-	MOD_TYPE(addr, fp->f_type, c);
-	if (fp->f_args)
-	    MOD_ARGS(addr, fp->f_args, c);
-	if (kern64) {
-		scratch64 = fp->f_addr;
-		MOD_ADDR(addr, scratch64, c);
-		scratch64 = fp->f_size;
-		MOD_SIZE(addr, scratch64, c);
-	} else {
-		scratch32 = fp->f_addr;
-#ifdef __arm__
-		scratch32 -= __elfN(relocation_offset);
-#endif
-		MOD_ADDR(addr, scratch32, c);
-		MOD_SIZE(addr, fp->f_size, c);
-	}
-	for (md = fp->f_metadata; md != NULL; md = md->md_next) {
-	    if (!(md->md_type & MODINFOMD_NOCOPY)) {
-		MOD_METADATA(addr, md, c);
-	    }
-	}
-    }
-    MOD_END(addr, c);
-    return(addr);
-}
-
 /*
  * Load the information expected by a kernel.
  *
@@ -177,7 +134,6 @@ md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64)
     };
 #endif
 
-    align = kern64 ? 8 : 4;
     howto = md_getboothowto(args);
 
     /*
diff --git a/stand/common/modinfo.c b/stand/common/modinfo.c
new file mode 100644
index 000000000000..a274890ace64
--- /dev/null
+++ b/stand/common/modinfo.c
@@ -0,0 +1,86 @@
+/*-
+ * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
+ */
+#include <stand.h>
+#include <sys/param.h>
+#include <sys/linker.h>
+#include <sys/boot.h>
+#include <sys/reboot.h>
+#if defined(LOADER_FDT_SUPPORT)
+#include <fdt_platform.h>
+#endif
+
+#ifdef __arm__
+#include <machine/elf.h>
+#endif
+#include <machine/metadata.h>
+
+#include "bootstrap.h"
+#include "modinfo.h"
+
+static int align;
+#define MOD_ALIGN(l)	roundup(l, align)
+
+vm_offset_t
+md_copymodules(vm_offset_t addr, bool kern64)
+{
+	struct preloaded_file	*fp;
+	struct file_metadata	*md;
+	uint64_t		scratch64;
+	uint32_t		scratch32;
+	int			c;
+
+	c = addr != 0;
+	/* start with the first module on the list, should be the kernel */
+	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
+
+		MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
+		MOD_TYPE(addr, fp->f_type, c);
+		if (fp->f_args)
+			MOD_ARGS(addr, fp->f_args, c);
+		if (kern64) {
+			scratch64 = fp->f_addr;
+			MOD_ADDR(addr, scratch64, c);
+			scratch64 = fp->f_size;
+			MOD_SIZE(addr, scratch64, c);
+		} else {
+			scratch32 = fp->f_addr;
+#ifdef __arm__
+			scratch32 -= __elfN(relocation_offset);
+#endif
+			MOD_ADDR(addr, scratch32, c);
+			MOD_SIZE(addr, fp->f_size, c);
+		}
+		for (md = fp->f_metadata; md != NULL; md = md->md_next) {
+			if (!(md->md_type & MODINFOMD_NOCOPY)) {
+				MOD_METADATA(addr, md, c);
+			}
+		}
+	}
+	MOD_END(addr, c);
+	return(addr);
+}
diff --git a/stand/common/modinfo.h b/stand/common/modinfo.h
index 245784ccb2ed..3d7f26a3c963 100644
--- a/stand/common/modinfo.h
+++ b/stand/common/modinfo.h
@@ -69,4 +69,6 @@
     COPY32(0, a, c);				\
 }
 
+vm_offset_t md_copymodules(vm_offset_t addr, bool kern64);
+
 #endif /* COMMON_MODINFO_H */
diff --git a/stand/efi/loader/bootinfo.c b/stand/efi/loader/bootinfo.c
index 05dfc9f59c1f..bcd2dad0bae1 100644
--- a/stand/efi/loader/bootinfo.c
+++ b/stand/efi/loader/bootinfo.c
@@ -61,11 +61,6 @@ __FBSDID("$FreeBSD$");
 #include "geliboot.h"
 #endif
 
-/*
- * Align to a pointer / long
- */
-#define MOD_ALIGN(l)	roundup(l, sizeof(u_long))
-
 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
     bool exit_bs);
 
@@ -172,36 +167,6 @@ bi_copyenv(vm_offset_t start)
 	return(last);
 }
 
-static vm_offset_t
-bi_copymodules(vm_offset_t addr)
-{
-	struct preloaded_file *fp;
-	struct file_metadata *md;
-	int c;
-	uint64_t v;
-
-	c = addr != 0;
-	/* Start with the first module on the list, should be the kernel. */
-	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-		MOD_NAME(addr, fp->f_name, c); /* This must come first. */
-		MOD_TYPE(addr, fp->f_type, c);
-		if (fp->f_args)
-			MOD_ARGS(addr, fp->f_args, c);
-		v = fp->f_addr;
-#if defined(__arm__)
-		v -= __elfN(relocation_offset);
-#endif
-		MOD_ADDR(addr, v, c);
-		v = fp->f_size;
-		MOD_SIZE(addr, v, c);
-		for (md = fp->f_metadata; md != NULL; md = md->md_next)
-			if (!(md->md_type & MODINFOMD_NOCOPY))
-				MOD_METADATA(addr, md, c);
-	}
-	MOD_END(addr, c);
-	return(addr);
-}
-
 static EFI_STATUS
 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
 {
@@ -392,6 +357,7 @@ bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
 	vm_offset_t size;
 	char *rootdevname;
 	int howto;
+	bool is64;
 #if defined(LOADER_FDT_SUPPORT)
 	vm_offset_t dtbp;
 	int dtb_size;
@@ -411,6 +377,11 @@ bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
 #endif
 	};
 #endif
+#ifdef __LP64__
+	is64 = true;
+#else
+	is64 = false;
+#endif
 
 	howto = bi_getboothowto(args);
 
@@ -489,7 +460,7 @@ bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
 #endif
 	bi_load_efi_data(kfp, exit_bs);
 
-	size = bi_copymodules(0);
+	size = md_copymodules(0, is64);
 	kernend = roundup(addr + size, PAGE_SIZE);
 	*kernendp = kernend;
 
@@ -514,7 +485,7 @@ bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
 #endif
 
 	/* Copy module list and metadata. */
-	(void)bi_copymodules(addr);
+	(void)md_copymodules(addr, is64);
 
 	return (0);
 }
diff --git a/stand/i386/libi386/bootinfo32.c b/stand/i386/libi386/bootinfo32.c
index f00c692a3993..965b62bbad81 100644
--- a/stand/i386/libi386/bootinfo32.c
+++ b/stand/i386/libi386/bootinfo32.c
@@ -44,37 +44,6 @@ __FBSDID("$FreeBSD$");
 
 static struct bootinfo  bi;
 
-/*
- * We have 4 byte alignment for 32-bit targets. This code is compiled as 32-bit
- * code...
- */
-#define MOD_ALIGN(l) roundup(l, sizeof(u_long))
-
-static vm_offset_t
-bi_copymodules32(vm_offset_t addr)
-{
-    struct preloaded_file	*fp;
-    struct file_metadata	*md;
-    int				c;
-
-    c = addr != 0;
-    /* start with the first module on the list, should be the kernel */
-    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-
-	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
-	MOD_TYPE(addr, fp->f_type, c);
-	if (fp->f_args)
-	    MOD_ARGS(addr, fp->f_args, c);
-	MOD_ADDR(addr, fp->f_addr, c);
-	MOD_SIZE(addr, fp->f_size, c);
-	for (md = fp->f_metadata; md != NULL; md = md->md_next)
-	    if (!(md->md_type & MODINFOMD_NOCOPY))
-		MOD_METADATA(addr, md, c);
-    }
-    MOD_END(addr, c);
-    return(addr);
-}
-
 /*
  * Load the information expected by an i386 kernel.
  *
@@ -179,7 +148,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
 
     /* Figure out the size and location of the metadata */
     *modulep = addr;
-    size = bi_copymodules32(0);
+    size = md_copymodules(0, false);
     kernend = roundup(addr + size, PAGE_SIZE);
     *kernendp = kernend;
 
@@ -188,7 +157,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
     bcopy(&kernend, md->md_data, sizeof kernend);
 
     /* copy module list and metadata */
-    (void)bi_copymodules32(addr);
+    (void)md_copymodules(addr, false);
 
     ssym = esym = 0;
     md = file_findmetadata(kfp, MODINFOMD_SSYM);
diff --git a/stand/i386/libi386/bootinfo64.c b/stand/i386/libi386/bootinfo64.c
index 18ed4af3e27f..8b5eb8c1db93 100644
--- a/stand/i386/libi386/bootinfo64.c
+++ b/stand/i386/libi386/bootinfo64.c
@@ -45,40 +45,6 @@ __FBSDID("$FreeBSD$");
 #include "geliboot.h"
 #endif
 
-/*
- * We have 8 byte alignment for 64-bit targets. This code is compiled as 64-bit
- * code...
- */
-#define MOD_ALIGN(l)	roundup(l, sizeof(uint64_t))
-
-static vm_offset_t
-bi_copymodules64(vm_offset_t addr)
-{
-    struct preloaded_file	*fp;
-    struct file_metadata	*md;
-    int				c;
-    uint64_t			v;
-
-    c = addr != 0;
-    /* start with the first module on the list, should be the kernel */
-    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-
-	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
-	MOD_TYPE(addr, fp->f_type, c);
-	if (fp->f_args)
-	    MOD_ARGS(addr, fp->f_args, c);
-	v = fp->f_addr;
-	MOD_ADDR(addr, v, c);
-	v = fp->f_size;
-	MOD_SIZE(addr, v, c);
-	for (md = fp->f_metadata; md != NULL; md = md->md_next)
-	    if (!(md->md_type & MODINFOMD_NOCOPY))
-		MOD_METADATA(addr, md, c);
-    }
-    MOD_END(addr, c);
-    return(addr);
-}
-
 /*
  * Check to see if this CPU supports long mode.
  */
@@ -197,7 +163,7 @@ bi_load64(char *args, vm_offset_t *modulep,
 #endif
     bi_load_vbe_data(kfp);
 
-    size = bi_copymodules64(0);
+    size = md_copymodules(0, true);
 
     /* copy our environment */
     envp = roundup(addr + size, PAGE_SIZE);
@@ -216,7 +182,7 @@ bi_load64(char *args, vm_offset_t *modulep,
     bcopy(&envp, md->md_data, sizeof envp);
 
     /* copy module list and metadata */
-    (void)bi_copymodules64(*modulep);
+    (void)md_copymodules(*modulep, true);
 
     return(0);
 }
diff --git a/stand/loader.mk b/stand/loader.mk
index aa88122cb7a1..9fa4c74e2501 100644
--- a/stand/loader.mk
+++ b/stand/loader.mk
@@ -6,6 +6,7 @@ CFLAGS+=-I${LDRSRC}
 
 SRCS+=	boot.c commands.c console.c devopen.c interp.c 
 SRCS+=	interp_backslash.c interp_parse.c ls.c misc.c 
+SRCS+=	modinfo.c
 SRCS+=	module.c nvstore.c pnglite.c tslog.c
 
 CFLAGS.module.c += -I$(SRCTOP)/sys/teken -I${SRCTOP}/contrib/pnglite
diff --git a/stand/userboot/userboot/bootinfo32.c b/stand/userboot/userboot/bootinfo32.c
index a717f4ad1f5a..8203f90c98fe 100644
--- a/stand/userboot/userboot/bootinfo32.c
+++ b/stand/userboot/userboot/bootinfo32.c
@@ -43,36 +43,6 @@ __FBSDID("$FreeBSD$");
 
 static struct bootinfo  bi;
 
-/*
- * We have 4 byte alignment for 32-bit targets.
- */
-#define MOD_ALIGN(l)	roundup(l, sizeof(uint32_t))
-
-static vm_offset_t
-bi_copymodules32(vm_offset_t addr)
-{
-    struct preloaded_file	*fp;
-    struct file_metadata	*md;
-    int				c;
-
-    c = addr != 0;
-    /* start with the first module on the list, should be the kernel */
-    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-
-	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
-	MOD_TYPE(addr, fp->f_type, c);
-	if (fp->f_args)
-	    MOD_ARGS(addr, fp->f_args, c);
-	MOD_ADDR(addr, fp->f_addr, c);
-	MOD_SIZE(addr, fp->f_size, c);
-	for (md = fp->f_metadata; md != NULL; md = md->md_next)
-	    if (!(md->md_type & MODINFOMD_NOCOPY))
-		MOD_METADATA(addr, md, c);
-    }
-    MOD_END(addr, c);
-    return(addr);
-}
-
 /*
  * Load the information expected by an i386 kernel.
  *
@@ -157,7 +127,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
 
     /* Figure out the size and location of the metadata */
     *modulep = addr;
-    size = bi_copymodules32(0);
+    size = md_copymodules(0, false);
     kernend = roundup(addr + size, PAGE_SIZE);
     *kernendp = kernend;
 
@@ -166,7 +136,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
     bcopy(&kernend, md->md_data, sizeof kernend);
 
     /* copy module list and metadata */
-    (void)bi_copymodules32(addr);
+    (void)md_copymodules(addr, false);
 
     ssym = esym = 0;
     md = file_findmetadata(kfp, MODINFOMD_SSYM);
diff --git a/stand/userboot/userboot/bootinfo64.c b/stand/userboot/userboot/bootinfo64.c
index 773f59474bf4..6d711cf51759 100644
--- a/stand/userboot/userboot/bootinfo64.c
+++ b/stand/userboot/userboot/bootinfo64.c
@@ -40,40 +40,6 @@ __FBSDID("$FreeBSD$");
 #include "modinfo.h"
 #include "libuserboot.h"
 
-/*
- * We have 8 byte alignment for 64-bit targets. This code is compiled as 32-bit
- * code...
- */
-#define MOD_ALIGN(l)	roundup(l, sizeof(uint64_t))
-
-static vm_offset_t
-bi_copymodules64(vm_offset_t addr)
-{
-    struct preloaded_file	*fp;
-    struct file_metadata	*md;
-    int				c;
-    uint64_t			v;
-
-    c = addr != 0;
-    /* start with the first module on the list, should be the kernel */
-    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
-
-	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
-	MOD_TYPE(addr, fp->f_type, c);
-	if (fp->f_args)
-	    MOD_ARGS(addr, fp->f_args, c);
-	v = fp->f_addr;
-	MOD_ADDR(addr, v, c);
-	v = fp->f_size;
-	MOD_SIZE(addr, v, c);
-	for (md = fp->f_metadata; md != NULL; md = md->md_next)
-	    if (!(md->md_type & MODINFOMD_NOCOPY))
-		MOD_METADATA(addr, md, c);
-    }
-    MOD_END(addr, c);
-    return(addr);
-}
-
 /*
  * Check to see if this CPU supports long mode.
  */
@@ -190,7 +156,7 @@ bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
 
     /* Figure out the size and location of the metadata */
     *modulep = addr;
-    size = bi_copymodules64(0);
+    size = md_copymodules(0, true);
     kernend = roundup(addr + size, PAGE_SIZE);
     *kernendp = kernend;
 
@@ -199,7 +165,7 @@ bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
     bcopy(&kernend, md->md_data, sizeof kernend);
 
     /* copy module list and metadata */
-    (void)bi_copymodules64(addr);
+    (void)md_copymodules(addr, true);
 
     return(0);
 }