Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_STRING_64_H |
| 3 | #define _ASM_X86_STRING_64_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | #ifdef __KERNEL__ |
Tony Luck | 3637efb | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 6 | #include <linux/jump_label.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 8 | /* Written 2002 by Andi Kleen */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 10 | /* Only used for special circumstances. Stolen from i386/string.h */ |
| 11 | static __always_inline void *__inline_memcpy(void *to, const void *from, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | { |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 13 | unsigned long d0, d1, d2; |
| 14 | asm volatile("rep ; movsl\n\t" |
| 15 | "testb $2,%b4\n\t" |
| 16 | "je 1f\n\t" |
| 17 | "movsw\n" |
| 18 | "1:\ttestb $1,%b4\n\t" |
| 19 | "je 2f\n\t" |
| 20 | "movsb\n" |
| 21 | "2:" |
| 22 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) |
| 23 | : "0" (n / 4), "q" (n), "1" ((long)to), "2" ((long)from) |
| 24 | : "memory"); |
| 25 | return to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /* Even with __builtin_ the compiler may decide to use the out of line |
| 29 | function. */ |
| 30 | |
| 31 | #define __HAVE_ARCH_MEMCPY 1 |
Andrey Ryabinin | a75ca54 | 2015-10-16 14:28:53 +0300 | [diff] [blame] | 32 | extern void *memcpy(void *to, const void *from, size_t len); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 33 | extern void *__memcpy(void *to, const void *from, size_t len); |
| 34 | |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 35 | #ifndef CONFIG_FORTIFY_SOURCE |
Andrey Ryabinin | a75ca54 | 2015-10-16 14:28:53 +0300 | [diff] [blame] | 36 | #if (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || __GNUC__ < 4 |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 37 | #define memcpy(dst, src, len) \ |
| 38 | ({ \ |
| 39 | size_t __len = (len); \ |
| 40 | void *__ret; \ |
| 41 | if (__builtin_constant_p(len) && __len >= 64) \ |
| 42 | __ret = __memcpy((dst), (src), __len); \ |
| 43 | else \ |
| 44 | __ret = __builtin_memcpy((dst), (src), __len); \ |
| 45 | __ret; \ |
| 46 | }) |
Andi Kleen | aac57f8 | 2007-07-21 17:09:58 +0200 | [diff] [blame] | 47 | #endif |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 48 | #endif /* !CONFIG_FORTIFY_SOURCE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #define __HAVE_ARCH_MEMSET |
Andi Kleen | 6edfba1 | 2006-03-25 16:29:49 +0100 | [diff] [blame] | 51 | void *memset(void *s, int c, size_t n); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 52 | void *__memset(void *s, int c, size_t n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Matthew Wilcox | 4c51248 | 2017-09-08 16:13:56 -0700 | [diff] [blame] | 54 | #define __HAVE_ARCH_MEMSET16 |
| 55 | static inline void *memset16(uint16_t *s, uint16_t v, size_t n) |
| 56 | { |
| 57 | long d0, d1; |
| 58 | asm volatile("rep\n\t" |
| 59 | "stosw" |
| 60 | : "=&c" (d0), "=&D" (d1) |
| 61 | : "a" (v), "1" (s), "0" (n) |
| 62 | : "memory"); |
| 63 | return s; |
| 64 | } |
| 65 | |
| 66 | #define __HAVE_ARCH_MEMSET32 |
| 67 | static inline void *memset32(uint32_t *s, uint32_t v, size_t n) |
| 68 | { |
| 69 | long d0, d1; |
| 70 | asm volatile("rep\n\t" |
| 71 | "stosl" |
| 72 | : "=&c" (d0), "=&D" (d1) |
| 73 | : "a" (v), "1" (s), "0" (n) |
| 74 | : "memory"); |
| 75 | return s; |
| 76 | } |
| 77 | |
| 78 | #define __HAVE_ARCH_MEMSET64 |
| 79 | static inline void *memset64(uint64_t *s, uint64_t v, size_t n) |
| 80 | { |
| 81 | long d0, d1; |
| 82 | asm volatile("rep\n\t" |
| 83 | "stosq" |
| 84 | : "=&c" (d0), "=&D" (d1) |
| 85 | : "a" (v), "1" (s), "0" (n) |
| 86 | : "memory"); |
| 87 | return s; |
| 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #define __HAVE_ARCH_MEMMOVE |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 91 | void *memmove(void *dest, const void *src, size_t count); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 92 | void *__memmove(void *dest, const void *src, size_t count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Joe Perches | 953b2f1 | 2008-03-23 01:03:34 -0700 | [diff] [blame] | 94 | int memcmp(const void *cs, const void *ct, size_t count); |
| 95 | size_t strlen(const char *s); |
| 96 | char *strcpy(char *dest, const char *src); |
| 97 | char *strcat(char *dest, const char *src); |
| 98 | int strcmp(const char *cs, const char *ct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 100 | #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) |
| 101 | |
| 102 | /* |
| 103 | * For files that not instrumented (e.g. mm/slub.c) we |
| 104 | * should use not instrumented version of mem* functions. |
| 105 | */ |
| 106 | |
| 107 | #undef memcpy |
| 108 | #define memcpy(dst, src, len) __memcpy(dst, src, len) |
| 109 | #define memmove(dst, src, len) __memmove(dst, src, len) |
| 110 | #define memset(s, c, n) __memset(s, c, n) |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 111 | |
| 112 | #ifndef __NO_FORTIFY |
| 113 | #define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ |
| 114 | #endif |
| 115 | |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 116 | #endif |
| 117 | |
Dan Williams | 6abccd1 | 2017-01-13 14:14:23 -0800 | [diff] [blame] | 118 | #define __HAVE_ARCH_MEMCPY_MCSAFE 1 |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 119 | __must_check unsigned long __memcpy_mcsafe(void *dst, const void *src, |
| 120 | size_t cnt); |
Tony Luck | 3637efb | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 121 | DECLARE_STATIC_KEY_FALSE(mcsafe_key); |
| 122 | |
Tony Luck | 92b0729 | 2016-02-18 11:47:26 -0800 | [diff] [blame] | 123 | /** |
| 124 | * memcpy_mcsafe - copy memory with indication if a machine check happened |
| 125 | * |
| 126 | * @dst: destination address |
| 127 | * @src: source address |
| 128 | * @cnt: number of bytes to copy |
| 129 | * |
| 130 | * Low level memory copy function that catches machine checks |
Tony Luck | 9a6fb28 | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 131 | * We only call into the "safe" function on systems that can |
| 132 | * actually do machine check recovery. Everyone else can just |
| 133 | * use memcpy(). |
Tony Luck | 92b0729 | 2016-02-18 11:47:26 -0800 | [diff] [blame] | 134 | * |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 135 | * Return 0 for success, or number of bytes not copied if there was an |
| 136 | * exception. |
Tony Luck | 92b0729 | 2016-02-18 11:47:26 -0800 | [diff] [blame] | 137 | */ |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 138 | static __always_inline __must_check unsigned long |
Tony Luck | 9a6fb28 | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 139 | memcpy_mcsafe(void *dst, const void *src, size_t cnt) |
| 140 | { |
| 141 | #ifdef CONFIG_X86_MCE |
| 142 | if (static_branch_unlikely(&mcsafe_key)) |
Dan Williams | da7bc9c | 2018-05-03 17:06:11 -0700 | [diff] [blame] | 143 | return __memcpy_mcsafe(dst, src, cnt); |
Tony Luck | 9a6fb28 | 2016-09-01 11:39:33 -0700 | [diff] [blame] | 144 | else |
| 145 | #endif |
| 146 | memcpy(dst, src, cnt); |
| 147 | return 0; |
| 148 | } |
Tony Luck | 92b0729 | 2016-02-18 11:47:26 -0800 | [diff] [blame] | 149 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
| 151 | #define __HAVE_ARCH_MEMCPY_FLUSHCACHE 1 |
| 152 | void memcpy_flushcache(void *dst, const void *src, size_t cnt); |
| 153 | #endif |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | #endif /* __KERNEL__ */ |
| 156 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 157 | #endif /* _ASM_X86_STRING_64_H */ |