Index: sys/amd64/include/cpu.h =================================================================== --- sys/amd64/include/cpu.h +++ sys/amd64/include/cpu.h @@ -92,6 +92,10 @@ 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_ */ Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -3876,6 +3876,7 @@ 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 Index: sys/kern/link_elf.c =================================================================== --- sys/kern/link_elf.c +++ sys/kern/link_elf.c @@ -1682,14 +1682,10 @@ { 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; Index: sys/kern/subr_early.c =================================================================== --- /dev/null +++ sys/kern/subr_early.c @@ -0,0 +1,76 @@ +/*- + * Copyright (c) 2018 The FreeBSD Foundation + * + * This software was developed by Mateusz Guzik + * under sponsorship from the FreeBSD Foundation. + * + * 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. + * + * $FreeBSD$ + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#ifndef MEMSET_EARLY_FUNC +#define MEMSET_EARLY_FUNC memset +#else +void *MEMSET_EARLY_FUNC(void *, int, size_t); +#endif + +void * +memset_early(void *buf, int c, size_t len) +{ + + return (MEMSET_EARLY_FUNC(buf, c, len)); +} + +#ifndef MEMCPY_EARLY_FUNC +#define MEMCPY_EARLY_FUNC memcpy +#else +void *MEMCPY_EARLY_FUNC(void *, const void *, size_t); +#endif + +void * +memcpy_early(void *to, const void *from, size_t len) +{ + + return (MEMCPY_EARLY_FUNC(to, from, len)); +} + +#ifndef MEMMOVE_EARLY_FUNC +#define MEMMOVE_EARLY_FUNC memmove +#else +void *MEMMOVE_EARLY_FUNC(void *, const void *, size_t); +#endif + +void * +memmove_early(void *to, const void *from, size_t len) +{ + + return (MEMMOVE_EARLY_FUNC(to, from, len)); +} Index: sys/sys/systm.h =================================================================== --- sys/sys/systm.h +++ sys/sys/systm.h @@ -324,6 +324,12 @@ 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); Index: sys/x86/x86/ucode.c =================================================================== --- sys/x86/x86/ucode.c +++ sys/x86/x86/ucode.c @@ -355,8 +355,7 @@ 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); Index: sys/x86/xen/pv.c =================================================================== --- sys/x86/xen/pv.c +++ sys/x86/xen/pv.c @@ -259,7 +259,7 @@ */ 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 @@ */ 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) {