blob: cea140ce6b42302deac808ba0c643cf619753781 [file] [log] [blame]
Kees Cook81b785f2016-04-26 14:46:06 -07001/*
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 */
Kees Cookdc425a62016-05-02 15:51:00 -07008#include "error.h"
9
Yinghai Lu8fee13a42010-08-02 16:21:22 -070010#include "../string.c"
Vivek Goyal820e8fe2014-03-18 15:26:38 -040011
Vivek Goyal820e8fe2014-03-18 15:26:38 -040012#ifdef CONFIG_X86_32
Kees Cook00ec2c32016-05-02 15:51:01 -070013static void *__memcpy(void *dest, const void *src, size_t n)
Vivek Goyal820e8fe2014-03-18 15:26:38 -040014{
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");
23
24 return dest;
25}
26#else
Kees Cook00ec2c32016-05-02 15:51:01 -070027static void *__memcpy(void *dest, const void *src, size_t n)
Vivek Goyal820e8fe2014-03-18 15:26:38 -040028{
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");
37
38 return dest;
39}
40#endif
Vivek Goyal04999552014-03-18 15:26:40 -040041
42void *memset(void *s, int c, size_t n)
43{
44 int i;
45 char *ss = s;
46
47 for (i = 0; i < n; i++)
48 ss[i] = c;
49 return s;
50}
Kees Cookbf0118d2016-04-20 13:55:45 -070051
Kees Cook81b785f2016-04-26 14:46:06 -070052void *memmove(void *dest, const void *src, size_t n)
Kees Cookbf0118d2016-04-20 13:55:45 -070053{
54 unsigned char *d = dest;
55 const unsigned char *s = src;
56
57 if (d <= s || d - s >= n)
Kees Cook00ec2c32016-05-02 15:51:01 -070058 return __memcpy(dest, src, n);
Kees Cookbf0118d2016-04-20 13:55:45 -070059
60 while (n-- > 0)
61 d[n] = s[n];
62
63 return dest;
64}
Kees Cook00ec2c32016-05-02 15:51:01 -070065
66/* Detect and warn about potential overlaps, but handle them with memmove. */
67void *memcpy(void *dest, const void *src, size_t n)
68{
69 if (dest > src && dest - src < n) {
70 warn("Avoiding potentially unsafe overlapping memcpy()!");
71 return memmove(dest, src, n);
72 }
73 return __memcpy(dest, src, n);
74}