blob: f74362b056199ded31ada323de99fa2a7506f49f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_STRING_32_H
3#define _ASM_X86_STRING_32_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Joe Perches06b0f572008-03-23 01:03:33 -07007/* Let gcc decide whether to inline or use the out of line functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#define __HAVE_ARCH_STRCPY
Andi Kleenb520b852007-07-21 17:09:59 +020010extern char *strcpy(char *dest, const char *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#define __HAVE_ARCH_STRNCPY
Andi Kleenb520b852007-07-21 17:09:59 +020013extern char *strncpy(char *dest, const char *src, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define __HAVE_ARCH_STRCAT
Andi Kleenb520b852007-07-21 17:09:59 +020016extern char *strcat(char *dest, const char *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define __HAVE_ARCH_STRNCAT
Andi Kleenb520b852007-07-21 17:09:59 +020019extern char *strncat(char *dest, const char *src, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define __HAVE_ARCH_STRCMP
Andi Kleenb520b852007-07-21 17:09:59 +020022extern int strcmp(const char *cs, const char *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define __HAVE_ARCH_STRNCMP
Andi Kleenb520b852007-07-21 17:09:59 +020025extern int strncmp(const char *cs, const char *ct, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define __HAVE_ARCH_STRCHR
Andi Kleenb520b852007-07-21 17:09:59 +020028extern char *strchr(const char *s, int c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define __HAVE_ARCH_STRLEN
Andi Kleenb520b852007-07-21 17:09:59 +020031extern size_t strlen(const char *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Joe Perches78d64fc2008-05-12 15:44:39 +020033static __always_inline void *__memcpy(void *to, const void *from, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Joe Perches78d64fc2008-05-12 15:44:39 +020035 int d0, d1, d2;
36 asm volatile("rep ; movsl\n\t"
37 "movl %4,%%ecx\n\t"
38 "andl $3,%%ecx\n\t"
39 "jz 1f\n\t"
40 "rep ; movsb\n\t"
41 "1:"
42 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
43 : "0" (n / 4), "g" (n), "1" ((long)to), "2" ((long)from)
44 : "memory");
45 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48/*
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070049 * This looks ugly, but the compiler can optimize it totally,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * as the count is constant.
51 */
Joe Perches78d64fc2008-05-12 15:44:39 +020052static __always_inline void *__constant_memcpy(void *to, const void *from,
53 size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070055 long esi, edi;
Joe Perches78d64fc2008-05-12 15:44:39 +020056 if (!n)
57 return to;
58
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070059 switch (n) {
Joe Perches78d64fc2008-05-12 15:44:39 +020060 case 1:
61 *(char *)to = *(char *)from;
62 return to;
63 case 2:
64 *(short *)to = *(short *)from;
65 return to;
66 case 4:
67 *(int *)to = *(int *)from;
68 return to;
Joe Perches78d64fc2008-05-12 15:44:39 +020069 case 3:
70 *(short *)to = *(short *)from;
71 *((char *)to + 2) = *((char *)from + 2);
72 return to;
73 case 5:
74 *(int *)to = *(int *)from;
75 *((char *)to + 4) = *((char *)from + 4);
76 return to;
77 case 6:
78 *(int *)to = *(int *)from;
79 *((short *)to + 2) = *((short *)from + 2);
80 return to;
81 case 8:
82 *(int *)to = *(int *)from;
83 *((int *)to + 1) = *((int *)from + 1);
84 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Joe Perches78d64fc2008-05-12 15:44:39 +020086
87 esi = (long)from;
88 edi = (long)to;
89 if (n >= 5 * 4) {
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070090 /* large block: use rep prefix */
91 int ecx;
Joe Perches78d64fc2008-05-12 15:44:39 +020092 asm volatile("rep ; movsl"
93 : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
94 : "0" (n / 4), "1" (edi), "2" (esi)
95 : "memory"
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070096 );
97 } else {
98 /* small block: don't clobber ecx + smaller code */
Joe Perches78d64fc2008-05-12 15:44:39 +020099 if (n >= 4 * 4)
100 asm volatile("movsl"
101 : "=&D"(edi), "=&S"(esi)
102 : "0"(edi), "1"(esi)
103 : "memory");
104 if (n >= 3 * 4)
105 asm volatile("movsl"
106 : "=&D"(edi), "=&S"(esi)
107 : "0"(edi), "1"(esi)
108 : "memory");
109 if (n >= 2 * 4)
110 asm volatile("movsl"
111 : "=&D"(edi), "=&S"(esi)
112 : "0"(edi), "1"(esi)
113 : "memory");
114 if (n >= 1 * 4)
115 asm volatile("movsl"
116 : "=&D"(edi), "=&S"(esi)
117 : "0"(edi), "1"(esi)
118 : "memory");
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700119 }
120 switch (n % 4) {
121 /* tail */
Joe Perches78d64fc2008-05-12 15:44:39 +0200122 case 0:
123 return to;
124 case 1:
125 asm volatile("movsb"
126 : "=&D"(edi), "=&S"(esi)
127 : "0"(edi), "1"(esi)
128 : "memory");
129 return to;
130 case 2:
131 asm volatile("movsw"
132 : "=&D"(edi), "=&S"(esi)
133 : "0"(edi), "1"(esi)
134 : "memory");
135 return to;
136 default:
137 asm volatile("movsw\n\tmovsb"
138 : "=&D"(edi), "=&S"(esi)
139 : "0"(edi), "1"(esi)
140 : "memory");
141 return to;
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145#define __HAVE_ARCH_MEMCPY
Daniel Micay6974f0c2017-07-12 14:36:10 -0700146extern void *memcpy(void *, const void *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Daniel Micay6974f0c2017-07-12 14:36:10 -0700148#ifndef CONFIG_FORTIFY_SOURCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef CONFIG_X86_USE_3DNOW
150
151#include <asm/mmx.h>
152
153/*
154 * This CPU favours 3DNow strongly (eg AMD Athlon)
155 */
156
Joe Perches78d64fc2008-05-12 15:44:39 +0200157static inline void *__constant_memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (len < 512)
160 return __constant_memcpy(to, from, len);
161 return _mmx_memcpy(to, from, len);
162}
163
Joe Perches78d64fc2008-05-12 15:44:39 +0200164static inline void *__memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 if (len < 512)
167 return __memcpy(to, from, len);
168 return _mmx_memcpy(to, from, len);
169}
170
Joe Perches78d64fc2008-05-12 15:44:39 +0200171#define memcpy(t, f, n) \
172 (__builtin_constant_p((n)) \
173 ? __constant_memcpy3d((t), (f), (n)) \
174 : __memcpy3d((t), (f), (n)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176#else
177
178/*
179 * No 3D Now!
180 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200181
Arjan van de Venff60fab2009-09-28 14:21:22 +0200182#define memcpy(t, f, n) __builtin_memcpy(t, f, n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#endif
Daniel Micay6974f0c2017-07-12 14:36:10 -0700185#endif /* !CONFIG_FORTIFY_SOURCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187#define __HAVE_ARCH_MEMMOVE
Joe Perches78d64fc2008-05-12 15:44:39 +0200188void *memmove(void *dest, const void *src, size_t n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Daniel Micay6974f0c2017-07-12 14:36:10 -0700190extern int memcmp(const void *, const void *, size_t);
191#ifndef CONFIG_FORTIFY_SOURCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define memcmp __builtin_memcmp
Daniel Micay6974f0c2017-07-12 14:36:10 -0700193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195#define __HAVE_ARCH_MEMCHR
Joe Perches78d64fc2008-05-12 15:44:39 +0200196extern void *memchr(const void *cs, int c, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Joe Perches78d64fc2008-05-12 15:44:39 +0200198static inline void *__memset_generic(void *s, char c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Joe Perches78d64fc2008-05-12 15:44:39 +0200200 int d0, d1;
201 asm volatile("rep\n\t"
202 "stosb"
203 : "=&c" (d0), "=&D" (d1)
204 : "a" (c), "1" (s), "0" (count)
205 : "memory");
206 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/* we might want to write optimized versions of these later */
Joe Perches78d64fc2008-05-12 15:44:39 +0200210#define __constant_count_memset(s, c, count) __memset_generic((s), (c), (count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* Added by Gertjan van Wingerde to make minix and sysv module work */
213#define __HAVE_ARCH_STRNLEN
Joe Perches78d64fc2008-05-12 15:44:39 +0200214extern size_t strnlen(const char *s, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* end of additional stuff */
216
217#define __HAVE_ARCH_STRSTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218extern char *strstr(const char *cs, const char *ct);
219
Joe Perches78d64fc2008-05-12 15:44:39 +0200220#define __memset(s, c, count) \
221 (__builtin_constant_p(count) \
222 ? __constant_count_memset((s), (c), (count)) \
223 : __memset_generic((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#define __HAVE_ARCH_MEMSET
Daniel Micay6974f0c2017-07-12 14:36:10 -0700226extern void *memset(void *, int, size_t);
227#ifndef CONFIG_FORTIFY_SOURCE
Arjan van de Venff60fab2009-09-28 14:21:22 +0200228#define memset(s, c, count) __builtin_memset(s, c, count)
Daniel Micay6974f0c2017-07-12 14:36:10 -0700229#endif /* !CONFIG_FORTIFY_SOURCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Matthew Wilcox4c512482017-09-08 16:13:56 -0700231#define __HAVE_ARCH_MEMSET16
232static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
233{
234 int d0, d1;
235 asm volatile("rep\n\t"
236 "stosw"
237 : "=&c" (d0), "=&D" (d1)
238 : "a" (v), "1" (s), "0" (n)
239 : "memory");
240 return s;
241}
242
243#define __HAVE_ARCH_MEMSET32
244static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
245{
246 int d0, d1;
247 asm volatile("rep\n\t"
248 "stosl"
249 : "=&c" (d0), "=&D" (d1)
250 : "a" (v), "1" (s), "0" (n)
251 : "memory");
252 return s;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * find the first occurrence of byte 'c', or 1 past the area if none
257 */
258#define __HAVE_ARCH_MEMSCAN
Joe Perches78d64fc2008-05-12 15:44:39 +0200259extern void *memscan(void *addr, int c, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261#endif /* __KERNEL__ */
262
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700263#endif /* _ASM_X86_STRING_32_H */