projects/linux·blob master · refs

106 lines · 2.5 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_STRING_64_H
3#define _ASM_X86_STRING_64_H
4
5#ifdef __KERNEL__
6
7/* Written 2002 by Andi Kleen */
8
9/* Even with __builtin_ the compiler may decide to use the out of line
10 function. */
11
12#if defined(__SANITIZE_MEMORY__) && defined(__NO_FORTIFY)
13#include <linux/kmsan_string.h>
14#endif
15
16#define __HAVE_ARCH_MEMCPY 1
17extern void *memcpy(void *to, const void *from, size_t len);
18extern void *__memcpy(void *to, const void *from, size_t len);
19
20#define __HAVE_ARCH_MEMSET
21void *memset(void *s, int c, size_t n);
22void *__memset(void *s, int c, size_t n);
23KCFI_REFERENCE(__memset);
24
25/*
26 * KMSAN needs to instrument as much code as possible. Use C versions of
27 * memsetXX() from lib/string.c under KMSAN.
28 */
29#if !defined(CONFIG_KMSAN)
30#define __HAVE_ARCH_MEMSET16
31static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
32{
33 const auto s0 = s;
34 asm volatile (
35 "rep stosw"
36 : "+D" (s), "+c" (n)
37 : "a" (v)
38 : "memory"
39 );
40 return s0;
41}
42
43#define __HAVE_ARCH_MEMSET32
44static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
45{
46 const auto s0 = s;
47 asm volatile (
48 "rep stosl"
49 : "+D" (s), "+c" (n)
50 : "a" (v)
51 : "memory"
52 );
53 return s0;
54}
55
56#define __HAVE_ARCH_MEMSET64
57static inline void *memset64(uint64_t *s, uint64_t v, size_t n)
58{
59 const auto s0 = s;
60 asm volatile (
61 "rep stosq"
62 : "+D" (s), "+c" (n)
63 : "a" (v)
64 : "memory"
65 );
66 return s0;
67}
68#endif
69
70#define __HAVE_ARCH_MEMMOVE
71void *memmove(void *dest, const void *src, size_t count);
72void *__memmove(void *dest, const void *src, size_t count);
73KCFI_REFERENCE(__memmove);
74
75int memcmp(const void *cs, const void *ct, size_t count);
76size_t strlen(const char *s);
77char *strcpy(char *dest, const char *src);
78char *strcat(char *dest, const char *src);
79int strcmp(const char *cs, const char *ct);
80
81#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
82#define __HAVE_ARCH_MEMCPY_FLUSHCACHE 1
83void __memcpy_flushcache(void *dst, const void *src, size_t cnt);
84static __always_inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
85{
86 if (__builtin_constant_p(cnt)) {
87 switch (cnt) {
88 case 4:
89 asm ("movntil %1, %0" : "=m"(*(u32 *)dst) : "r"(*(u32 *)src));
90 return;
91 case 8:
92 asm ("movntiq %1, %0" : "=m"(*(u64 *)dst) : "r"(*(u64 *)src));
93 return;
94 case 16:
95 asm ("movntiq %1, %0" : "=m"(*(u64 *)dst) : "r"(*(u64 *)src));
96 asm ("movntiq %1, %0" : "=m"(*(u64 *)(dst + 8)) : "r"(*(u64 *)(src + 8)));
97 return;
98 }
99 }
100 __memcpy_flushcache(dst, src, cnt);
101}
102#endif
103
104#endif /* __KERNEL__ */
105
106#endif /* _ASM_X86_STRING_64_H */