git: e27261d80594 - releng/15.1 - amd64: there is no reason to copy ucode around in ucode_load_bsp()

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Fri, 05 Jun 2026 22:39:15 UTC
The branch releng/15.1 has been updated by cperciva:

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

commit e27261d80594f32eefb7562989a4836cb81ba262
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-05-30 15:57:46 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-06-05 22:38:47 +0000

    amd64: there is no reason to copy ucode around in ucode_load_bsp()
    
    Approved by:    re (cperciva)
    PR:     294630
    
    (cherry picked from commit 16f21c5af35002b8361ffb2e83ff3c92cd899a3a)
    (cherry picked from commit c6ccef4a32b4e09927dfdcc0f734af25c6193f5a)
---
 sys/x86/x86/ucode.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/sys/x86/x86/ucode.c b/sys/x86/x86/ucode.c
index 72133de211f8..613a7b03489f 100644
--- a/sys/x86/x86/ucode.c
+++ b/sys/x86/x86/ucode.c
@@ -361,18 +361,20 @@ ucode_load_ap(int cpu)
 		(void)ucode_loader->load(ucode_data, UNSAFE, NULL, NULL);
 }
 
-static void *
-map_ucode(uintptr_t free, size_t len)
+static const void *
+map_ucode(const void *match, uintptr_t free, size_t len)
 {
 #ifdef __i386__
 	uintptr_t va;
 
 	for (va = free; va < free + len; va += PAGE_SIZE)
 		pmap_kenter(va, (vm_paddr_t)va);
+	memcpy_early(free, match, len);
+	return ((const void *)free);
 #else
 	(void)len;
+	return (match);
 #endif
-	return ((void *)free);
 }
 
 static void
@@ -405,7 +407,7 @@ ucode_load_bsp(uintptr_t free)
 		char vendor[13];
 	} cpuid;
 	const uint8_t *fileaddr, *match;
-	uint8_t *addr;
+	const uint8_t *addr;
 	char *type;
 	uint64_t nrev, orev;
 	caddr_t file;
@@ -440,14 +442,10 @@ ucode_load_bsp(uintptr_t free)
 		len = preload_fetch_size(file);
 		match = ucode_loader->match(fileaddr, &len);
 		if (match != NULL) {
-			addr = map_ucode(free, len);
-			/* We can't use memcpy() before ifunc resolution. */
-			memcpy_early(addr, match, len);
-			match = addr;
-
-			error = ucode_loader->load(match, EARLY, &nrev, &orev);
+			addr = map_ucode(match, free, len);
+			error = ucode_loader->load(addr, EARLY, &nrev, &orev);
 			if (error == 0) {
-				ucode_data = early_ucode_data = match;
+				ucode_data = early_ucode_data = addr;
 				ucode_nrev = nrev;
 				ucode_orev = orev;
 				return (len);