1 // SPDX-License-Identifier: GPL-2.0
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.
11 #include "../string.c"
14 static void *__memcpy(void *dest
, const void *src
, size_t n
)
21 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
22 : "0" (n
>> 2), "g" (n
& 3), "1" (dest
), "2" (src
)
28 static void *__memcpy(void *dest
, const void *src
, size_t n
)
35 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
36 : "0" (n
>> 3), "g" (n
& 7), "1" (dest
), "2" (src
)
43 void *memset(void *s
, int c
, size_t n
)
48 for (i
= 0; i
< n
; i
++)
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
);
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
);