locking/refcounts: Include fewer headers in <linux/refcount.h>
[linux/fpc-iii.git] / arch / x86 / boot / compressed / string.c
blob19dbbcdd1a53d9d90c388722e1920455b5adbe43
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This provides an optimized implementation of memcpy, and a simplified
4 * implementation of memset and memmove. These are used here because the
5 * standard kernel runtime versions are not yet available and we don't
6 * trust the gcc built-in implementations as they may do unexpected things
7 * (e.g. FPU ops) in the minimal decompression stub execution environment.
8 */
9 #include "error.h"
11 #include "../string.c"
13 #ifdef CONFIG_X86_32
14 static void *__memcpy(void *dest, const void *src, size_t n)
16 int d0, d1, d2;
17 asm volatile(
18 "rep ; movsl\n\t"
19 "movl %4,%%ecx\n\t"
20 "rep ; movsb\n\t"
21 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
22 : "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
23 : "memory");
25 return dest;
27 #else
28 static void *__memcpy(void *dest, const void *src, size_t n)
30 long d0, d1, d2;
31 asm volatile(
32 "rep ; movsq\n\t"
33 "movq %4,%%rcx\n\t"
34 "rep ; movsb\n\t"
35 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
36 : "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
37 : "memory");
39 return dest;
41 #endif
43 void *memset(void *s, int c, size_t n)
45 int i;
46 char *ss = s;
48 for (i = 0; i < n; i++)
49 ss[i] = c;
50 return s;
53 void *memmove(void *dest, const void *src, size_t n)
55 unsigned char *d = dest;
56 const unsigned char *s = src;
58 if (d <= s || d - s >= n)
59 return __memcpy(dest, src, n);
61 while (n-- > 0)
62 d[n] = s[n];
64 return dest;
67 /* Detect and warn about potential overlaps, but handle them with memmove. */
68 void *memcpy(void *dest, const void *src, size_t n)
70 if (dest > src && dest - src < n) {
71 warn("Avoiding potentially unsafe overlapping memcpy()!");
72 return memmove(dest, src, n);
74 return __memcpy(dest, src, n);