efi/libstub: use EFI provided memcpy/memset routines
The stub is used in different execution environments, but on arm64,
RISC-V and LoongArch, we still use the core kernel's implementation of
memcpy and memset, as they are just a branch instruction away, and can
generally be reused even from code such as the EFI stub that runs in a
completely different address space.
KAsan complicates this slightly, resulting in the need for some hacks to
expose the uninstrumented, __ prefixed versions as the normal ones, as
the latter are instrumented to include the KAsan checks, which only work
in the core kernel.
Unfortunately, #define'ing memcpy to __memcpy when building C code does
not guarantee that no explicit memcpy() calls will be emitted. And with
the upcoming zboot support, which consists of a separate binary which
therefore needs its own implementation of memcpy/memset anyway, it's
better to provide one explicitly instead of linking to the existing one.
Given that EFI exposes implementations of memmove() and memset() via the
boot services table, let's wire those up in the appropriate way, and
drop the references to the core kernel ones.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-09 16:45:17 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <asm/efi.h>
|
|
|
|
#include <asm/string.h>
|
|
|
|
|
|
|
|
#include "efistub.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_KASAN
|
|
|
|
#undef memcpy
|
|
|
|
#undef memmove
|
|
|
|
#undef memset
|
|
|
|
void *__memcpy(void *__dest, const void *__src, size_t __n) __alias(memcpy);
|
|
|
|
void *__memmove(void *__dest, const void *__src, size_t count) __alias(memmove);
|
|
|
|
void *__memset(void *s, int c, size_t count) __alias(memset);
|
|
|
|
#endif
|
|
|
|
|
2025-02-27 18:35:11 +01:00
|
|
|
static void *efistub_memmove(u8 *dst, const u8 *src, size_t len)
|
|
|
|
{
|
|
|
|
if (src > dst || dst >= (src + len))
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
dst[i] = src[i];
|
|
|
|
else
|
|
|
|
for (ssize_t i = len - 1; i >= 0; i--)
|
|
|
|
dst[i] = src[i];
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *efistub_memset(void *dst, int c, size_t len)
|
|
|
|
{
|
|
|
|
for (u8 *d = dst; len--; d++)
|
|
|
|
*d = c;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
efi/libstub: use EFI provided memcpy/memset routines
The stub is used in different execution environments, but on arm64,
RISC-V and LoongArch, we still use the core kernel's implementation of
memcpy and memset, as they are just a branch instruction away, and can
generally be reused even from code such as the EFI stub that runs in a
completely different address space.
KAsan complicates this slightly, resulting in the need for some hacks to
expose the uninstrumented, __ prefixed versions as the normal ones, as
the latter are instrumented to include the KAsan checks, which only work
in the core kernel.
Unfortunately, #define'ing memcpy to __memcpy when building C code does
not guarantee that no explicit memcpy() calls will be emitted. And with
the upcoming zboot support, which consists of a separate binary which
therefore needs its own implementation of memcpy/memset anyway, it's
better to provide one explicitly instead of linking to the existing one.
Given that EFI exposes implementations of memmove() and memset() via the
boot services table, let's wire those up in the appropriate way, and
drop the references to the core kernel ones.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-09 16:45:17 +02:00
|
|
|
void *memcpy(void *dst, const void *src, size_t len)
|
|
|
|
{
|
2025-02-27 18:35:11 +01:00
|
|
|
if (efi_table_attr(efi_system_table, boottime) == NULL)
|
|
|
|
return efistub_memmove(dst, src, len);
|
|
|
|
|
efi/libstub: use EFI provided memcpy/memset routines
The stub is used in different execution environments, but on arm64,
RISC-V and LoongArch, we still use the core kernel's implementation of
memcpy and memset, as they are just a branch instruction away, and can
generally be reused even from code such as the EFI stub that runs in a
completely different address space.
KAsan complicates this slightly, resulting in the need for some hacks to
expose the uninstrumented, __ prefixed versions as the normal ones, as
the latter are instrumented to include the KAsan checks, which only work
in the core kernel.
Unfortunately, #define'ing memcpy to __memcpy when building C code does
not guarantee that no explicit memcpy() calls will be emitted. And with
the upcoming zboot support, which consists of a separate binary which
therefore needs its own implementation of memcpy/memset anyway, it's
better to provide one explicitly instead of linking to the existing one.
Given that EFI exposes implementations of memmove() and memset() via the
boot services table, let's wire those up in the appropriate way, and
drop the references to the core kernel ones.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-09 16:45:17 +02:00
|
|
|
efi_bs_call(copy_mem, dst, src, len);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void *memmove(void *dst, const void *src, size_t len) __alias(memcpy);
|
|
|
|
|
|
|
|
void *memset(void *dst, int c, size_t len)
|
|
|
|
{
|
2025-02-27 18:35:11 +01:00
|
|
|
if (efi_table_attr(efi_system_table, boottime) == NULL)
|
|
|
|
return efistub_memset(dst, c, len);
|
|
|
|
|
efi/libstub: use EFI provided memcpy/memset routines
The stub is used in different execution environments, but on arm64,
RISC-V and LoongArch, we still use the core kernel's implementation of
memcpy and memset, as they are just a branch instruction away, and can
generally be reused even from code such as the EFI stub that runs in a
completely different address space.
KAsan complicates this slightly, resulting in the need for some hacks to
expose the uninstrumented, __ prefixed versions as the normal ones, as
the latter are instrumented to include the KAsan checks, which only work
in the core kernel.
Unfortunately, #define'ing memcpy to __memcpy when building C code does
not guarantee that no explicit memcpy() calls will be emitted. And with
the upcoming zboot support, which consists of a separate binary which
therefore needs its own implementation of memcpy/memset anyway, it's
better to provide one explicitly instead of linking to the existing one.
Given that EFI exposes implementations of memmove() and memset() via the
boot services table, let's wire those up in the appropriate way, and
drop the references to the core kernel ones.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-09 16:45:17 +02:00
|
|
|
efi_bs_call(set_mem, dst, len, c & U8_MAX);
|
|
|
|
return dst;
|
|
|
|
}
|
2022-10-11 15:15:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* memcmp - Compare two areas of memory
|
|
|
|
* @cs: One area of memory
|
|
|
|
* @ct: Another area of memory
|
|
|
|
* @count: The size of the area.
|
|
|
|
*/
|
|
|
|
#undef memcmp
|
|
|
|
int memcmp(const void *cs, const void *ct, size_t count)
|
|
|
|
{
|
|
|
|
const unsigned char *su1, *su2;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
|
|
|
|
if ((res = *su1 - *su2) != 0)
|
|
|
|
break;
|
|
|
|
return res;
|
|
|
|
}
|