Chris Metcalf | 18aecc2 | 2011-05-04 14:38:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Tilera Corporation. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation, version 2. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 11 | * NON INFRINGEMENT. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * Do memcpy(), but trap and return "n" when a load or store faults. |
| 15 | * |
| 16 | * Note: this idiom only works when memcpy() compiles to a leaf function. |
Chris Metcalf | efb734d | 2012-03-29 14:05:04 -0400 | [diff] [blame] | 17 | * Here leaf function not only means it does not have calls, but also |
| 18 | * requires no stack operations (sp, stack frame pointer) and no |
| 19 | * use of callee-saved registers, else "jrp lr" will be incorrect since |
| 20 | * unwinding stack frame is bypassed. Since memcpy() is not complex so |
| 21 | * these conditions are satisfied here, but we need to be careful when |
| 22 | * modifying this file. This is not a clean solution but is the best |
| 23 | * one so far. |
Chris Metcalf | 18aecc2 | 2011-05-04 14:38:26 -0400 | [diff] [blame] | 24 | * |
| 25 | * Also note that we are capturing "n" from the containing scope here. |
| 26 | */ |
| 27 | |
| 28 | #define _ST(p, inst, v) \ |
| 29 | ({ \ |
| 30 | asm("1: " #inst " %0, %1;" \ |
| 31 | ".pushsection .coldtext.memcpy,\"ax\";" \ |
| 32 | "2: { move r0, %2; jrp lr };" \ |
| 33 | ".section __ex_table,\"a\";" \ |
| 34 | ".quad 1b, 2b;" \ |
| 35 | ".popsection" \ |
| 36 | : "=m" (*(p)) : "r" (v), "r" (n)); \ |
| 37 | }) |
| 38 | |
| 39 | #define _LD(p, inst) \ |
| 40 | ({ \ |
| 41 | unsigned long __v; \ |
| 42 | asm("1: " #inst " %0, %1;" \ |
| 43 | ".pushsection .coldtext.memcpy,\"ax\";" \ |
| 44 | "2: { move r0, %2; jrp lr };" \ |
| 45 | ".section __ex_table,\"a\";" \ |
| 46 | ".quad 1b, 2b;" \ |
| 47 | ".popsection" \ |
| 48 | : "=r" (__v) : "m" (*(p)), "r" (n)); \ |
| 49 | __v; \ |
| 50 | }) |
| 51 | |
| 52 | #define USERCOPY_FUNC __copy_to_user_inatomic |
| 53 | #define ST1(p, v) _ST((p), st1, (v)) |
| 54 | #define ST2(p, v) _ST((p), st2, (v)) |
| 55 | #define ST4(p, v) _ST((p), st4, (v)) |
| 56 | #define ST8(p, v) _ST((p), st, (v)) |
| 57 | #define LD1 LD |
| 58 | #define LD2 LD |
| 59 | #define LD4 LD |
| 60 | #define LD8 LD |
| 61 | #include "memcpy_64.c" |
| 62 | |
| 63 | #define USERCOPY_FUNC __copy_from_user_inatomic |
| 64 | #define ST1 ST |
| 65 | #define ST2 ST |
| 66 | #define ST4 ST |
| 67 | #define ST8 ST |
| 68 | #define LD1(p) _LD((p), ld1u) |
| 69 | #define LD2(p) _LD((p), ld2u) |
| 70 | #define LD4(p) _LD((p), ld4u) |
| 71 | #define LD8(p) _LD((p), ld) |
| 72 | #include "memcpy_64.c" |
| 73 | |
| 74 | #define USERCOPY_FUNC __copy_in_user_inatomic |
| 75 | #define ST1(p, v) _ST((p), st1, (v)) |
| 76 | #define ST2(p, v) _ST((p), st2, (v)) |
| 77 | #define ST4(p, v) _ST((p), st4, (v)) |
| 78 | #define ST8(p, v) _ST((p), st, (v)) |
| 79 | #define LD1(p) _LD((p), ld1u) |
| 80 | #define LD2(p) _LD((p), ld2u) |
| 81 | #define LD4(p) _LD((p), ld4u) |
| 82 | #define LD8(p) _LD((p), ld) |
| 83 | #include "memcpy_64.c" |
| 84 | |
| 85 | unsigned long __copy_from_user_zeroing(void *to, const void __user *from, |
| 86 | unsigned long n) |
| 87 | { |
| 88 | unsigned long rc = __copy_from_user_inatomic(to, from, n); |
| 89 | if (unlikely(rc)) |
| 90 | memset(to + n - rc, 0, rc); |
| 91 | return rc; |
| 92 | } |