acrn-hypervisor/hypervisor/arch/x86/lib/memory.c
Ziheng Li eb8bcb06b3 Update copyright year range in code headers
Modified the copyright year range in code, and corrected "int32_tel"
into "Intel" in two "hypervisor/include/debug/profiling.h" and
"hypervisor/include/debug/profiling_internal.h".

Tracked-On: #7559
Signed-off-by: Ziheng Li <ziheng.li@intel.com>
2022-07-15 11:48:35 +08:00

62 lines
1.3 KiB
C

/*
* Copyright (C) 2018-2022 Intel Corporation.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <types.h>
static inline void memset_erms(void *base, uint8_t v, size_t n)
{
asm volatile("rep ; stosb"
: "+D"(base)
: "a" (v), "c"(n));
}
void *memset(void *base, uint8_t v, size_t n)
{
/*
* Some CPUs support enhanced REP MOVSB/STOSB feature. It is recommended
* to use it when possible.
*/
if ((base != NULL) && (n != 0U)) {
memset_erms(base, v, n);
}
return base;
}
static inline void memcpy_erms(void *d, const void *s, size_t slen)
{
asm volatile ("rep; movsb"
: "=&D"(d), "=&S"(s)
: "c"(slen), "0" (d), "1" (s)
: "memory");
}
/*
* @brief Copies at most slen bytes from src address to dest address, up to dmax.
*
* INPUTS
*
* @param[in] d pointer to Destination address
* @param[in] dmax maximum length of dest
* @param[in] s pointer to Source address
* @param[in] slen maximum number of bytes of src to copy
*
* @return 0 for success and -1 for runtime-constraint violation.
*/
int32_t memcpy_s(void *d, size_t dmax, const void *s, size_t slen)
{
int32_t ret = -1;
if ((d != NULL) && (s != NULL) && (dmax >= slen) && ((d > (s + slen)) || (s > (d + dmax)))) {
if (slen != 0U) {
memcpy_erms(d, s, slen);
}
ret = 0;
} else {
(void)memset(d, 0U, dmax);
}
return ret;
}