Kees Cook | 81b785f | 2016-04-26 14:46:06 -0700 | [diff] [blame] | 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 | */ |
Kees Cook | dc425a6 | 2016-05-02 15:51:00 -0700 | [diff] [blame^] | 8 | #include "error.h" |
| 9 | |
Yinghai Lu | 8fee13a4 | 2010-08-02 16:21:22 -0700 | [diff] [blame] | 10 | #include "../string.c" |
Vivek Goyal | 820e8fe | 2014-03-18 15:26:38 -0400 | [diff] [blame] | 11 | |
Vivek Goyal | 820e8fe | 2014-03-18 15:26:38 -0400 | [diff] [blame] | 12 | #ifdef CONFIG_X86_32 |
Kees Cook | 81b785f | 2016-04-26 14:46:06 -0700 | [diff] [blame] | 13 | void *memcpy(void *dest, const void *src, size_t n) |
Vivek Goyal | 820e8fe | 2014-03-18 15:26:38 -0400 | [diff] [blame] | 14 | { |
| 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 Cook | 81b785f | 2016-04-26 14:46:06 -0700 | [diff] [blame] | 27 | void *memcpy(void *dest, const void *src, size_t n) |
Vivek Goyal | 820e8fe | 2014-03-18 15:26:38 -0400 | [diff] [blame] | 28 | { |
| 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 Goyal | 0499955 | 2014-03-18 15:26:40 -0400 | [diff] [blame] | 41 | |
| 42 | void *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 Cook | bf0118d | 2016-04-20 13:55:45 -0700 | [diff] [blame] | 51 | |
Kees Cook | 81b785f | 2016-04-26 14:46:06 -0700 | [diff] [blame] | 52 | void *memmove(void *dest, const void *src, size_t n) |
Kees Cook | bf0118d | 2016-04-20 13:55:45 -0700 | [diff] [blame] | 53 | { |
| 54 | unsigned char *d = dest; |
| 55 | const unsigned char *s = src; |
| 56 | |
| 57 | if (d <= s || d - s >= n) |
Kees Cook | 81b785f | 2016-04-26 14:46:06 -0700 | [diff] [blame] | 58 | return memcpy(dest, src, n); |
Kees Cook | bf0118d | 2016-04-20 13:55:45 -0700 | [diff] [blame] | 59 | |
| 60 | while (n-- > 0) |
| 61 | d[n] = s[n]; |
| 62 | |
| 63 | return dest; |
| 64 | } |