2016-04-27 00:46:06 +03:00
|
|
|
/*
|
|
|
|
* This provides an optimized implementation of memcpy, and a simplified
|
|
|
|
* implementation of memset and memmove. These are used here because the
|
|
|
|
* standard kernel runtime versions are not yet available and we don't
|
|
|
|
* trust the gcc built-in implementations as they may do unexpected things
|
|
|
|
* (e.g. FPU ops) in the minimal decompression stub execution environment.
|
|
|
|
*/
|
2016-05-03 01:51:00 +03:00
|
|
|
#include "error.h"
|
|
|
|
|
2010-08-03 03:21:22 +04:00
|
|
|
#include "../string.c"
|
2014-03-18 23:26:38 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86_32
|
2016-05-03 01:51:01 +03:00
|
|
|
static void *__memcpy(void *dest, const void *src, size_t n)
|
2014-03-18 23:26:38 +04:00
|
|
|
{
|
|
|
|
int d0, d1, d2;
|
|
|
|
asm volatile(
|
|
|
|
"rep ; movsl\n\t"
|
|
|
|
"movl %4,%%ecx\n\t"
|
|
|
|
"rep ; movsb\n\t"
|
|
|
|
: "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
|
|
|
: "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#else
|
2016-05-03 01:51:01 +03:00
|
|
|
static void *__memcpy(void *dest, const void *src, size_t n)
|
2014-03-18 23:26:38 +04:00
|
|
|
{
|
|
|
|
long d0, d1, d2;
|
|
|
|
asm volatile(
|
|
|
|
"rep ; movsq\n\t"
|
|
|
|
"movq %4,%%rcx\n\t"
|
|
|
|
"rep ; movsb\n\t"
|
|
|
|
: "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
|
|
|
: "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-18 23:26:40 +04:00
|
|
|
|
|
|
|
void *memset(void *s, int c, size_t n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *ss = s;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
ss[i] = c;
|
|
|
|
return s;
|
|
|
|
}
|
2016-04-20 23:55:45 +03:00
|
|
|
|
2016-04-27 00:46:06 +03:00
|
|
|
void *memmove(void *dest, const void *src, size_t n)
|
2016-04-20 23:55:45 +03:00
|
|
|
{
|
|
|
|
unsigned char *d = dest;
|
|
|
|
const unsigned char *s = src;
|
|
|
|
|
|
|
|
if (d <= s || d - s >= n)
|
2016-05-03 01:51:01 +03:00
|
|
|
return __memcpy(dest, src, n);
|
2016-04-20 23:55:45 +03:00
|
|
|
|
|
|
|
while (n-- > 0)
|
|
|
|
d[n] = s[n];
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2016-05-03 01:51:01 +03:00
|
|
|
|
|
|
|
/* Detect and warn about potential overlaps, but handle them with memmove. */
|
|
|
|
void *memcpy(void *dest, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
if (dest > src && dest - src < n) {
|
|
|
|
warn("Avoiding potentially unsafe overlapping memcpy()!");
|
|
|
|
return memmove(dest, src, n);
|
|
|
|
}
|
|
|
|
return __memcpy(dest, src, n);
|
|
|
|
}
|