Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 1 | #ifndef __ASM_GENERIC_UACCESS_H |
| 2 | #define __ASM_GENERIC_UACCESS_H |
| 3 | |
| 4 | /* |
| 5 | * User space memory access functions, these should work |
Geert Uytterhoeven | 0a4a664 | 2013-12-30 10:06:33 +0100 | [diff] [blame] | 6 | * on any machine that has kernel and user data in the same |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 7 | * address space, e.g. all NOMMU machines. |
| 8 | */ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 9 | #include <linux/string.h> |
| 10 | |
| 11 | #include <asm/segment.h> |
| 12 | |
| 13 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) |
| 14 | |
| 15 | #ifndef KERNEL_DS |
| 16 | #define KERNEL_DS MAKE_MM_SEG(~0UL) |
| 17 | #endif |
| 18 | |
| 19 | #ifndef USER_DS |
| 20 | #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1) |
| 21 | #endif |
| 22 | |
| 23 | #ifndef get_fs |
| 24 | #define get_ds() (KERNEL_DS) |
| 25 | #define get_fs() (current_thread_info()->addr_limit) |
| 26 | |
| 27 | static inline void set_fs(mm_segment_t fs) |
| 28 | { |
| 29 | current_thread_info()->addr_limit = fs; |
| 30 | } |
| 31 | #endif |
| 32 | |
Vineet Gupta | 10a6007 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 33 | #ifndef segment_eq |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 34 | #define segment_eq(a, b) ((a).seg == (b).seg) |
Vineet Gupta | 10a6007 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 35 | #endif |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 36 | |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 37 | #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size)) |
| 38 | |
| 39 | /* |
| 40 | * The architecture should really override this if possible, at least |
| 41 | * doing a check on the get_fs() |
| 42 | */ |
| 43 | #ifndef __access_ok |
| 44 | static inline int __access_ok(unsigned long addr, unsigned long size) |
| 45 | { |
| 46 | return 1; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | /* |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 51 | * These are the main single-value transfer routines. They automatically |
| 52 | * use the right size if we just have the right pointer type. |
| 53 | * This version just falls back to copy_{from,to}_user, which should |
| 54 | * provide a fast-path for small values. |
| 55 | */ |
| 56 | #define __put_user(x, ptr) \ |
| 57 | ({ \ |
| 58 | __typeof__(*(ptr)) __x = (x); \ |
| 59 | int __pu_err = -EFAULT; \ |
| 60 | __chk_user_ptr(ptr); \ |
| 61 | switch (sizeof (*(ptr))) { \ |
| 62 | case 1: \ |
| 63 | case 2: \ |
| 64 | case 4: \ |
| 65 | case 8: \ |
| 66 | __pu_err = __put_user_fn(sizeof (*(ptr)), \ |
| 67 | ptr, &__x); \ |
| 68 | break; \ |
| 69 | default: \ |
| 70 | __put_user_bad(); \ |
| 71 | break; \ |
| 72 | } \ |
| 73 | __pu_err; \ |
| 74 | }) |
| 75 | |
| 76 | #define put_user(x, ptr) \ |
| 77 | ({ \ |
Yoshinori Sato | a02613a | 2015-07-16 13:56:06 +0900 | [diff] [blame] | 78 | void *__p = (ptr); \ |
Michael S. Tsirkin | e0acd0b | 2013-05-26 17:30:36 +0300 | [diff] [blame] | 79 | might_fault(); \ |
Yoshinori Sato | a02613a | 2015-07-16 13:56:06 +0900 | [diff] [blame] | 80 | access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \ |
| 81 | __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 82 | -EFAULT; \ |
| 83 | }) |
| 84 | |
Vineet Gupta | 05d88a4 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 85 | #ifndef __put_user_fn |
| 86 | |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 87 | static inline int __put_user_fn(size_t size, void __user *ptr, void *x) |
| 88 | { |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 89 | return unlikely(__copy_to_user(ptr, x, size)) ? -EFAULT : 0; |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Vineet Gupta | 05d88a4 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 92 | #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k) |
| 93 | |
| 94 | #endif |
| 95 | |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 96 | extern int __put_user_bad(void) __attribute__((noreturn)); |
| 97 | |
| 98 | #define __get_user(x, ptr) \ |
| 99 | ({ \ |
| 100 | int __gu_err = -EFAULT; \ |
| 101 | __chk_user_ptr(ptr); \ |
| 102 | switch (sizeof(*(ptr))) { \ |
| 103 | case 1: { \ |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 104 | unsigned char __x = 0; \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 105 | __gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 106 | ptr, &__x); \ |
| 107 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 108 | break; \ |
| 109 | }; \ |
| 110 | case 2: { \ |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 111 | unsigned short __x = 0; \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 112 | __gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 113 | ptr, &__x); \ |
| 114 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 115 | break; \ |
| 116 | }; \ |
| 117 | case 4: { \ |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 118 | unsigned int __x = 0; \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 119 | __gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 120 | ptr, &__x); \ |
| 121 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 122 | break; \ |
| 123 | }; \ |
| 124 | case 8: { \ |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 125 | unsigned long long __x = 0; \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 126 | __gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 127 | ptr, &__x); \ |
| 128 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 129 | break; \ |
| 130 | }; \ |
| 131 | default: \ |
| 132 | __get_user_bad(); \ |
| 133 | break; \ |
| 134 | } \ |
| 135 | __gu_err; \ |
| 136 | }) |
| 137 | |
| 138 | #define get_user(x, ptr) \ |
| 139 | ({ \ |
Yoshinori Sato | a02613a | 2015-07-16 13:56:06 +0900 | [diff] [blame] | 140 | const void *__p = (ptr); \ |
Michael S. Tsirkin | e0acd0b | 2013-05-26 17:30:36 +0300 | [diff] [blame] | 141 | might_fault(); \ |
Yoshinori Sato | a02613a | 2015-07-16 13:56:06 +0900 | [diff] [blame] | 142 | access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \ |
| 143 | __get_user((x), (__typeof__(*(ptr)) *)__p) : \ |
Al Viro | 9ad18b7 | 2016-08-17 23:19:01 -0400 | [diff] [blame] | 144 | ((x) = (__typeof__(*(ptr)))0,-EFAULT); \ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 145 | }) |
| 146 | |
Vineet Gupta | 05d88a4 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 147 | #ifndef __get_user_fn |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 148 | static inline int __get_user_fn(size_t size, const void __user *ptr, void *x) |
| 149 | { |
Al Viro | c1aad8d | 2017-03-28 01:02:40 -0400 | [diff] [blame^] | 150 | return unlikely(__copy_from_user(x, ptr, size)) ? -EFAULT : 0; |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Vineet Gupta | 05d88a4 | 2013-01-18 15:12:16 +0530 | [diff] [blame] | 153 | #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k) |
| 154 | |
| 155 | #endif |
| 156 | |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 157 | extern int __get_user_bad(void) __attribute__((noreturn)); |
| 158 | |
| 159 | #ifndef __copy_from_user_inatomic |
| 160 | #define __copy_from_user_inatomic __copy_from_user |
| 161 | #endif |
| 162 | |
| 163 | #ifndef __copy_to_user_inatomic |
| 164 | #define __copy_to_user_inatomic __copy_to_user |
| 165 | #endif |
| 166 | |
| 167 | static inline long copy_from_user(void *to, |
| 168 | const void __user * from, unsigned long n) |
| 169 | { |
Al Viro | 2545e5d | 2016-08-17 16:36:37 -0400 | [diff] [blame] | 170 | unsigned long res = n; |
Michael S. Tsirkin | e0acd0b | 2013-05-26 17:30:36 +0300 | [diff] [blame] | 171 | might_fault(); |
Al Viro | 2545e5d | 2016-08-17 16:36:37 -0400 | [diff] [blame] | 172 | if (likely(access_ok(VERIFY_READ, from, n))) |
| 173 | res = __copy_from_user(to, from, n); |
| 174 | if (unlikely(res)) |
| 175 | memset(to + (n - res), 0, res); |
| 176 | return res; |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static inline long copy_to_user(void __user *to, |
| 180 | const void *from, unsigned long n) |
| 181 | { |
Michael S. Tsirkin | e0acd0b | 2013-05-26 17:30:36 +0300 | [diff] [blame] | 182 | might_fault(); |
Mike Frysinger | a9ede5b | 2009-06-14 02:00:03 -0400 | [diff] [blame] | 183 | if (access_ok(VERIFY_WRITE, to, n)) |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 184 | return __copy_to_user(to, from, n); |
| 185 | else |
| 186 | return n; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Copy a null terminated string from userspace. |
| 191 | */ |
| 192 | #ifndef __strncpy_from_user |
| 193 | static inline long |
| 194 | __strncpy_from_user(char *dst, const char __user *src, long count) |
| 195 | { |
| 196 | char *tmp; |
| 197 | strncpy(dst, (const char __force *)src, count); |
| 198 | for (tmp = dst; *tmp && count > 0; tmp++, count--) |
| 199 | ; |
| 200 | return (tmp - dst); |
| 201 | } |
| 202 | #endif |
| 203 | |
| 204 | static inline long |
| 205 | strncpy_from_user(char *dst, const char __user *src, long count) |
| 206 | { |
Mike Frysinger | a9ede5b | 2009-06-14 02:00:03 -0400 | [diff] [blame] | 207 | if (!access_ok(VERIFY_READ, src, 1)) |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 208 | return -EFAULT; |
| 209 | return __strncpy_from_user(dst, src, count); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Return the size of a string (including the ending 0) |
| 214 | * |
| 215 | * Return 0 on exception, a value greater than N if too long |
| 216 | */ |
GuanXuetao | 7f509a9 | 2011-01-15 18:08:09 +0800 | [diff] [blame] | 217 | #ifndef __strnlen_user |
Mark Salter | 830f580 | 2011-10-04 09:17:36 -0400 | [diff] [blame] | 218 | #define __strnlen_user(s, n) (strnlen((s), (n)) + 1) |
GuanXuetao | 7f509a9 | 2011-01-15 18:08:09 +0800 | [diff] [blame] | 219 | #endif |
| 220 | |
Mark Salter | 830f580 | 2011-10-04 09:17:36 -0400 | [diff] [blame] | 221 | /* |
| 222 | * Unlike strnlen, strnlen_user includes the nul terminator in |
| 223 | * its returned count. Callers should check for a returned value |
| 224 | * greater than N as an indication the string is too long. |
| 225 | */ |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 226 | static inline long strnlen_user(const char __user *src, long n) |
| 227 | { |
Mike Frysinger | 9844813 | 2009-06-14 02:00:02 -0400 | [diff] [blame] | 228 | if (!access_ok(VERIFY_READ, src, 1)) |
| 229 | return 0; |
GuanXuetao | 7f509a9 | 2011-01-15 18:08:09 +0800 | [diff] [blame] | 230 | return __strnlen_user(src, n); |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 231 | } |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 232 | |
| 233 | static inline long strlen_user(const char __user *src) |
| 234 | { |
| 235 | return strnlen_user(src, 32767); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Zero Userspace |
| 240 | */ |
| 241 | #ifndef __clear_user |
| 242 | static inline __must_check unsigned long |
| 243 | __clear_user(void __user *to, unsigned long n) |
| 244 | { |
| 245 | memset((void __force *)to, 0, n); |
| 246 | return 0; |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | static inline __must_check unsigned long |
| 251 | clear_user(void __user *to, unsigned long n) |
| 252 | { |
Michael S. Tsirkin | e0acd0b | 2013-05-26 17:30:36 +0300 | [diff] [blame] | 253 | might_fault(); |
Mike Frysinger | a9ede5b | 2009-06-14 02:00:03 -0400 | [diff] [blame] | 254 | if (!access_ok(VERIFY_WRITE, to, n)) |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 255 | return n; |
| 256 | |
| 257 | return __clear_user(to, n); |
| 258 | } |
| 259 | |
Al Viro | aaa2e7a | 2016-12-25 01:22:09 -0500 | [diff] [blame] | 260 | #include <asm/extable.h> |
| 261 | |
Arnd Bergmann | eed417d | 2009-05-13 22:56:37 +0000 | [diff] [blame] | 262 | #endif /* __ASM_GENERIC_UACCESS_H */ |