Skip to content

Commit d9a96cc

Browse files
Peter Zijlstra (Intel)hansendc
authored andcommitted
x86/asm: Introduce inline memcpy and memset
Provide inline memcpy and memset functions that can be used instead of the GCC builtins when necessary. The immediate use case is for the text poking functions to avoid the standard memcpy()/memset() calls because objtool complains about such dynamic calls within an AC=1 region. See tools/objtool/Documentation/objtool.txt, warning #9, regarding function calls with UACCESS enabled. Some user copy functions such as copy_user_generic() and __clear_user() have similar rep_{movs,stos} usages. But, those are highly specialized and hard to combine or reuse for other things. Define these new helpers for all other usages that need a completely unoptimized, strictly inline version of memcpy() or memset(). Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Sohil Mehta <sohil.mehta@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://patch.msgid.link/20251118182911.2983253-4-sohil.mehta%40intel.com
1 parent e39c538 commit d9a96cc

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

arch/x86/include/asm/string.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_X86_STRING_H
3+
#define _ASM_X86_STRING_H
4+
25
#ifdef CONFIG_X86_32
36
# include <asm/string_32.h>
47
#else
58
# include <asm/string_64.h>
69
#endif
10+
11+
static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
12+
{
13+
void *ret = to;
14+
15+
asm volatile("rep movsb"
16+
: "+D" (to), "+S" (from), "+c" (len)
17+
: : "memory");
18+
return ret;
19+
}
20+
21+
static __always_inline void *__inline_memset(void *s, int v, size_t n)
22+
{
23+
void *ret = s;
24+
25+
asm volatile("rep stosb"
26+
: "+D" (s), "+c" (n)
27+
: "a" ((uint8_t)v)
28+
: "memory");
29+
return ret;
30+
}
31+
32+
#endif /* _ASM_X86_STRING_H */

0 commit comments

Comments
 (0)