From 3f102f5881c85d6db20daedb8645fa980c66ba19 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Thu, 11 Oct 2018 23:28:04 +0000 Subject: [PATCH] Provide string functions for use before ifuncs get resolved. The change is a no-op for architectures which don't ifunc memset, memcpy nor memmove. Convert places which need them. Xen bits by royger. Reviewed by: kib Approved by: re (gjb) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D17487 --- sys/amd64/include/cpu.h | 4 ++++ sys/conf/files | 1 + sys/kern/link_elf.c | 6 +----- sys/sys/systm.h | 6 ++++++ sys/x86/x86/ucode.c | 3 +-- sys/x86/xen/pv.c | 4 ++-- 6 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sys/amd64/include/cpu.h b/sys/amd64/include/cpu.h index cf98abdddcf9..db3554fad84f 100644 --- a/sys/amd64/include/cpu.h +++ b/sys/amd64/include/cpu.h @@ -92,6 +92,10 @@ get_cyclecount(void) return (rdtsc()); } +#define MEMSET_EARLY_FUNC memset_std +#define MEMCPY_EARLY_FUNC memcpy_std +#define MEMMOVE_EARLY_FUNC memmove_std + #endif #endif /* !_MACHINE_CPU_H_ */ diff --git a/sys/conf/files b/sys/conf/files index 0c6c1b13f0cb..f9747600c30e 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -3876,6 +3876,7 @@ kern/subr_compressor.c standard \ kern/subr_counter.c standard kern/subr_devstat.c standard kern/subr_disk.c standard +kern/subr_early.c standard kern/subr_epoch.c standard kern/subr_eventhandler.c standard kern/subr_fattime.c standard diff --git a/sys/kern/link_elf.c b/sys/kern/link_elf.c index 9338b06bb268..9c82b7178a69 100644 --- a/sys/kern/link_elf.c +++ b/sys/kern/link_elf.c @@ -1682,14 +1682,10 @@ link_elf_ireloc(caddr_t kmdp) { struct elf_file eff; elf_file_t ef; - volatile char *c; - size_t i; ef = &eff; - /* Do not use bzero/memset before ireloc is done. */ - for (c = (char *)ef, i = 0; i < sizeof(*ef); i++) - c[i] = 0; + bzero_early(ef, sizeof(*ef)); ef->modptr = kmdp; ef->dynamic = (Elf_Dyn *)&_DYNAMIC; diff --git a/sys/sys/systm.h b/sys/sys/systm.h index ab8a925715d0..a008cc39d8de 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -324,6 +324,12 @@ void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n); int memcmp(const void *b1, const void *b2, size_t len); #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len)) +void *memset_early(void * _Nonnull buf, int c, size_t len); +#define bzero_early(buf, len) memset_early((buf), 0, (len)) +void *memcpy_early(void * _Nonnull to, const void * _Nonnull from, size_t len); +void *memmove_early(void * _Nonnull dest, const void * _Nonnull src, size_t n); +#define bcopy_early(from, to, len) memmove_early((to), (from), (len)) + int copystr(const void * _Nonnull __restrict kfaddr, void * _Nonnull __restrict kdaddr, size_t len, size_t * __restrict lencopied); diff --git a/sys/x86/x86/ucode.c b/sys/x86/x86/ucode.c index 5b039491345a..c0810ae7eb21 100644 --- a/sys/x86/x86/ucode.c +++ b/sys/x86/x86/ucode.c @@ -355,8 +355,7 @@ ucode_load_bsp(uintptr_t free) if (match != NULL) { addr = map_ucode(free, len); /* We can't use memcpy() before ifunc resolution. */ - for (i = 0; i < len; i++) - addr[i] = ((volatile uint8_t *)match)[i]; + memcpy_early(addr, match, len); match = addr; error = ucode_loader->load(match, false, &nrev, &orev); diff --git a/sys/x86/xen/pv.c b/sys/x86/xen/pv.c index 4505907172c0..e86efde1fe39 100644 --- a/sys/x86/xen/pv.c +++ b/sys/x86/xen/pv.c @@ -259,7 +259,7 @@ hammer_time_xen_legacy(start_info_t *si, uint64_t xenstack) */ kenv = (void *)(physfree + KERNBASE); physfree += PAGE_SIZE; - bzero(kenv, PAGE_SIZE); + bzero_early(kenv, PAGE_SIZE); init_static_kenv(kenv, PAGE_SIZE); /* Set the hooks for early functions that diverge from bare metal */ @@ -320,7 +320,7 @@ hammer_time_xen(vm_paddr_t start_info_paddr) */ kenv = (void *)(physfree + KERNBASE); physfree += PAGE_SIZE; - bzero(kenv, PAGE_SIZE); + bzero_early(kenv, PAGE_SIZE); init_static_kenv(kenv, PAGE_SIZE); if (start_info->modlist_paddr != 0) {