blob: 877a1dfbf7707e5a2297cbbef8b65c55212f8e94 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_STRING_64_H
2#define _ASM_X86_STRING_64_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4#ifdef __KERNEL__
Tony Luck3637efb2016-09-01 11:39:33 -07005#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Joe Perches953b2f12008-03-23 01:03:34 -07007/* Written 2002 by Andi Kleen */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Joe Perches953b2f12008-03-23 01:03:34 -07009/* Only used for special circumstances. Stolen from i386/string.h */
10static __always_inline void *__inline_memcpy(void *to, const void *from, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011{
Joe Perches953b2f12008-03-23 01:03:34 -070012 unsigned long d0, d1, d2;
13 asm volatile("rep ; movsl\n\t"
14 "testb $2,%b4\n\t"
15 "je 1f\n\t"
16 "movsw\n"
17 "1:\ttestb $1,%b4\n\t"
18 "je 2f\n\t"
19 "movsb\n"
20 "2:"
21 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
22 : "0" (n / 4), "q" (n), "1" ((long)to), "2" ((long)from)
23 : "memory");
24 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
27/* Even with __builtin_ the compiler may decide to use the out of line
28 function. */
29
30#define __HAVE_ARCH_MEMCPY 1
Andrey Ryabinina75ca542015-10-16 14:28:53 +030031extern void *memcpy(void *to, const void *from, size_t len);
Andrey Ryabinin393f2032015-02-13 14:39:56 -080032extern void *__memcpy(void *to, const void *from, size_t len);
33
Vegard Nossumf8561292008-04-04 00:53:23 +020034#ifndef CONFIG_KMEMCHECK
Andrey Ryabinina75ca542015-10-16 14:28:53 +030035#if (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || __GNUC__ < 4
Joe Perches953b2f12008-03-23 01:03:34 -070036#define memcpy(dst, src, len) \
37({ \
38 size_t __len = (len); \
39 void *__ret; \
40 if (__builtin_constant_p(len) && __len >= 64) \
41 __ret = __memcpy((dst), (src), __len); \
42 else \
43 __ret = __builtin_memcpy((dst), (src), __len); \
44 __ret; \
45})
Andi Kleenaac57f82007-07-21 17:09:58 +020046#endif
Vegard Nossumf8561292008-04-04 00:53:23 +020047#else
48/*
49 * kmemcheck becomes very happy if we use the REP instructions unconditionally,
50 * because it means that we know both memory operands in advance.
51 */
52#define memcpy(dst, src, len) __inline_memcpy((dst), (src), (len))
53#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define __HAVE_ARCH_MEMSET
Andi Kleen6edfba12006-03-25 16:29:49 +010056void *memset(void *s, int c, size_t n);
Andrey Ryabinin393f2032015-02-13 14:39:56 -080057void *__memset(void *s, int c, size_t n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define __HAVE_ARCH_MEMMOVE
Joe Perches953b2f12008-03-23 01:03:34 -070060void *memmove(void *dest, const void *src, size_t count);
Andrey Ryabinin393f2032015-02-13 14:39:56 -080061void *__memmove(void *dest, const void *src, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Joe Perches953b2f12008-03-23 01:03:34 -070063int memcmp(const void *cs, const void *ct, size_t count);
64size_t strlen(const char *s);
65char *strcpy(char *dest, const char *src);
66char *strcat(char *dest, const char *src);
67int strcmp(const char *cs, const char *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Andrey Ryabinin393f2032015-02-13 14:39:56 -080069#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
70
71/*
72 * For files that not instrumented (e.g. mm/slub.c) we
73 * should use not instrumented version of mem* functions.
74 */
75
76#undef memcpy
77#define memcpy(dst, src, len) __memcpy(dst, src, len)
78#define memmove(dst, src, len) __memmove(dst, src, len)
79#define memset(s, c, n) __memset(s, c, n)
80#endif
81
Tony Luck3637efb2016-09-01 11:39:33 -070082DECLARE_STATIC_KEY_FALSE(mcsafe_key);
83
Tony Luck92b07292016-02-18 11:47:26 -080084/**
85 * memcpy_mcsafe - copy memory with indication if a machine check happened
86 *
87 * @dst: destination address
88 * @src: source address
89 * @cnt: number of bytes to copy
90 *
91 * Low level memory copy function that catches machine checks
92 *
Tony Luckcbf8b5a2016-03-14 15:33:39 -070093 * Return 0 for success, -EFAULT for fail
Tony Luck92b07292016-02-18 11:47:26 -080094 */
Tony Luckcbf8b5a2016-03-14 15:33:39 -070095int memcpy_mcsafe(void *dst, const void *src, size_t cnt);
Tony Luck92b07292016-02-18 11:47:26 -080096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif /* __KERNEL__ */
98
H. Peter Anvin1965aae2008-10-22 22:26:29 -070099#endif /* _ASM_X86_STRING_64_H */