Linux 4.9.243
[linux/fpc-iii.git] / arch / x86 / boot / compressed / string.c
blobcea140ce6b42302deac808ba0c643cf619753781
1 /*
2 * This provides an optimized implementation of memcpy, and a simplified
3 * implementation of memset and memmove. These are used here because the
4 * standard kernel runtime versions are not yet available and we don't
5 * trust the gcc built-in implementations as they may do unexpected things
6 * (e.g. FPU ops) in the minimal decompression stub execution environment.
7 */
8 #include "error.h"
10 #include "../string.c"
12 #ifdef CONFIG_X86_32
13 static void *__memcpy(void *dest, const void *src, size_t n)
15 int d0, d1, d2;
16 asm volatile(
17 "rep ; movsl\n\t"
18 "movl %4,%%ecx\n\t"
19 "rep ; movsb\n\t"
20 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
21 : "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
22 : "memory");
24 return dest;
26 #else
27 static void *__memcpy(void *dest, const void *src, size_t n)
29 long d0, d1, d2;
30 asm volatile(
31 "rep ; movsq\n\t"
32 "movq %4,%%rcx\n\t"
33 "rep ; movsb\n\t"
34 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
35 : "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
36 : "memory");
38 return dest;
40 #endif
42 void *memset(void *s, int c, size_t n)
44 int i;
45 char *ss = s;
47 for (i = 0; i < n; i++)
48 ss[i] = c;
49 return s;
52 void *memmove(void *dest, const void *src, size_t n)
54 unsigned char *d = dest;
55 const unsigned char *s = src;
57 if (d <= s || d - s >= n)
58 return __memcpy(dest, src, n);
60 while (n-- > 0)
61 d[n] = s[n];
63 return dest;
66 /* Detect and warn about potential overlaps, but handle them with memmove. */
67 void *memcpy(void *dest, const void *src, size_t n)
69 if (dest > src && dest - src < n) {
70 warn("Avoiding potentially unsafe overlapping memcpy()!");
71 return memmove(dest, src, n);
73 return __memcpy(dest, src, n);