Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 1 | #ifndef __SCORE_UACCESS_H |
| 2 | #define __SCORE_UACCESS_H |
| 3 | |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/errno.h> |
| 6 | #include <linux/thread_info.h> |
| 7 | |
| 8 | #define VERIFY_READ 0 |
| 9 | #define VERIFY_WRITE 1 |
| 10 | |
| 11 | #define get_ds() (KERNEL_DS) |
| 12 | #define get_fs() (current_thread_info()->addr_limit) |
| 13 | #define segment_eq(a, b) ((a).seg == (b).seg) |
| 14 | |
Chen Liqin | 6bc9a39 | 2009-06-12 22:01:00 +0800 | [diff] [blame] | 15 | /* |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 16 | * Is a address valid? This does a straighforward calculation rather |
| 17 | * than tests. |
Chen Liqin | 6bc9a39 | 2009-06-12 22:01:00 +0800 | [diff] [blame] | 18 | * |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 19 | * Address valid if: |
| 20 | * - "addr" doesn't have any high-bits set |
| 21 | * - AND "size" doesn't have any high-bits set |
| 22 | * - AND "addr+size" doesn't have any high-bits set |
| 23 | * - OR we are in kernel mode. |
| 24 | * |
| 25 | * __ua_size() is a trick to avoid runtime checking of positive constant |
| 26 | * sizes; for those we already know at compile time that the size is ok. |
Chen Liqin | 6bc9a39 | 2009-06-12 22:01:00 +0800 | [diff] [blame] | 27 | */ |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 28 | #define __ua_size(size) \ |
| 29 | ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size)) |
| 30 | |
| 31 | /* |
| 32 | * access_ok: - Checks if a user space pointer is valid |
| 33 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that |
| 34 | * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe |
| 35 | * to write to a block, it is always safe to read from it. |
| 36 | * @addr: User space pointer to start of block to check |
| 37 | * @size: Size of block to check |
| 38 | * |
David Hildenbrand | b3c395e | 2015-05-11 17:52:08 +0200 | [diff] [blame^] | 39 | * Context: User context only. This function may sleep if pagefaults are |
| 40 | * enabled. |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 41 | * |
| 42 | * Checks if a pointer to a block of memory in user space is valid. |
| 43 | * |
| 44 | * Returns true (nonzero) if the memory block may be valid, false (zero) |
| 45 | * if it is definitely invalid. |
| 46 | * |
| 47 | * Note that, depending on architecture, this function probably just |
| 48 | * checks that the pointer is in the user space range - after calling |
| 49 | * this function, memory access functions may still return -EFAULT. |
| 50 | */ |
| 51 | |
| 52 | #define __access_ok(addr, size) \ |
| 53 | (((long)((get_fs().seg) & \ |
| 54 | ((addr) | ((addr) + (size)) | \ |
| 55 | __ua_size(size)))) == 0) |
| 56 | |
| 57 | #define access_ok(type, addr, size) \ |
| 58 | likely(__access_ok((unsigned long)(addr), (size))) |
| 59 | |
| 60 | /* |
| 61 | * put_user: - Write a simple value into user space. |
| 62 | * @x: Value to copy to user space. |
| 63 | * @ptr: Destination address, in user space. |
| 64 | * |
David Hildenbrand | b3c395e | 2015-05-11 17:52:08 +0200 | [diff] [blame^] | 65 | * Context: User context only. This function may sleep if pagefaults are |
| 66 | * enabled. |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 67 | * |
| 68 | * This macro copies a single simple value from kernel space to user |
| 69 | * space. It supports simple types like char and int, but not larger |
| 70 | * data types like structures or arrays. |
| 71 | * |
| 72 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 73 | * to the result of dereferencing @ptr. |
| 74 | * |
| 75 | * Returns zero on success, or -EFAULT on error. |
| 76 | */ |
| 77 | #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr))) |
| 78 | |
| 79 | /* |
| 80 | * get_user: - Get a simple variable from user space. |
| 81 | * @x: Variable to store result. |
| 82 | * @ptr: Source address, in user space. |
| 83 | * |
David Hildenbrand | b3c395e | 2015-05-11 17:52:08 +0200 | [diff] [blame^] | 84 | * Context: User context only. This function may sleep if pagefaults are |
| 85 | * enabled. |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 86 | * |
| 87 | * This macro copies a single simple variable from user space to kernel |
| 88 | * space. It supports simple types like char and int, but not larger |
| 89 | * data types like structures or arrays. |
| 90 | * |
| 91 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 92 | * dereferencing @ptr must be assignable to @x without a cast. |
| 93 | * |
| 94 | * Returns zero on success, or -EFAULT on error. |
| 95 | * On error, the variable @x is set to zero. |
| 96 | */ |
| 97 | #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr))) |
| 98 | |
| 99 | /* |
| 100 | * __put_user: - Write a simple value into user space, with less checking. |
| 101 | * @x: Value to copy to user space. |
| 102 | * @ptr: Destination address, in user space. |
| 103 | * |
David Hildenbrand | b3c395e | 2015-05-11 17:52:08 +0200 | [diff] [blame^] | 104 | * Context: User context only. This function may sleep if pagefaults are |
| 105 | * enabled. |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 106 | * |
| 107 | * This macro copies a single simple value from kernel space to user |
| 108 | * space. It supports simple types like char and int, but not larger |
| 109 | * data types like structures or arrays. |
| 110 | * |
| 111 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 112 | * to the result of dereferencing @ptr. |
| 113 | * |
| 114 | * Caller must check the pointer with access_ok() before calling this |
| 115 | * function. |
| 116 | * |
| 117 | * Returns zero on success, or -EFAULT on error. |
| 118 | */ |
| 119 | #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr))) |
| 120 | |
| 121 | /* |
| 122 | * __get_user: - Get a simple variable from user space, with less checking. |
| 123 | * @x: Variable to store result. |
| 124 | * @ptr: Source address, in user space. |
| 125 | * |
David Hildenbrand | b3c395e | 2015-05-11 17:52:08 +0200 | [diff] [blame^] | 126 | * Context: User context only. This function may sleep if pagefaults are |
| 127 | * enabled. |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 128 | * |
| 129 | * This macro copies a single simple variable from user space to kernel |
| 130 | * space. It supports simple types like char and int, but not larger |
| 131 | * data types like structures or arrays. |
| 132 | * |
| 133 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 134 | * dereferencing @ptr must be assignable to @x without a cast. |
| 135 | * |
| 136 | * Caller must check the pointer with access_ok() before calling this |
| 137 | * function. |
| 138 | * |
| 139 | * Returns zero on success, or -EFAULT on error. |
| 140 | * On error, the variable @x is set to zero. |
| 141 | */ |
| 142 | #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr))) |
| 143 | |
| 144 | struct __large_struct { unsigned long buf[100]; }; |
| 145 | #define __m(x) (*(struct __large_struct __user *)(x)) |
| 146 | |
| 147 | /* |
| 148 | * Yuck. We need two variants, one for 64bit operation and one |
| 149 | * for 32 bit mode and old iron. |
| 150 | */ |
| 151 | extern void __get_user_unknown(void); |
| 152 | |
| 153 | #define __get_user_common(val, size, ptr) \ |
| 154 | do { \ |
| 155 | switch (size) { \ |
| 156 | case 1: \ |
| 157 | __get_user_asm(val, "lb", ptr); \ |
| 158 | break; \ |
| 159 | case 2: \ |
| 160 | __get_user_asm(val, "lh", ptr); \ |
| 161 | break; \ |
| 162 | case 4: \ |
| 163 | __get_user_asm(val, "lw", ptr); \ |
| 164 | break; \ |
| 165 | case 8: \ |
| 166 | if ((copy_from_user((void *)&val, ptr, 8)) == 0) \ |
| 167 | __gu_err = 0; \ |
| 168 | else \ |
| 169 | __gu_err = -EFAULT; \ |
| 170 | break; \ |
| 171 | default: \ |
| 172 | __get_user_unknown(); \ |
| 173 | break; \ |
| 174 | } \ |
| 175 | } while (0) |
| 176 | |
| 177 | #define __get_user_nocheck(x, ptr, size) \ |
| 178 | ({ \ |
| 179 | long __gu_err = 0; \ |
| 180 | __get_user_common((x), size, ptr); \ |
| 181 | __gu_err; \ |
| 182 | }) |
| 183 | |
| 184 | #define __get_user_check(x, ptr, size) \ |
| 185 | ({ \ |
| 186 | long __gu_err = -EFAULT; \ |
| 187 | const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ |
| 188 | \ |
| 189 | if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \ |
| 190 | __get_user_common((x), size, __gu_ptr); \ |
| 191 | \ |
| 192 | __gu_err; \ |
| 193 | }) |
| 194 | |
| 195 | #define __get_user_asm(val, insn, addr) \ |
| 196 | { \ |
| 197 | long __gu_tmp; \ |
| 198 | \ |
| 199 | __asm__ __volatile__( \ |
| 200 | "1:" insn " %1, %3\n" \ |
| 201 | "2:\n" \ |
| 202 | ".section .fixup,\"ax\"\n" \ |
| 203 | "3:li %0, %4\n" \ |
| 204 | "j 2b\n" \ |
| 205 | ".previous\n" \ |
| 206 | ".section __ex_table,\"a\"\n" \ |
| 207 | ".word 1b, 3b\n" \ |
| 208 | ".previous\n" \ |
| 209 | : "=r" (__gu_err), "=r" (__gu_tmp) \ |
| 210 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \ |
| 211 | \ |
| 212 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Yuck. We need two variants, one for 64bit operation and one |
| 217 | * for 32 bit mode and old iron. |
| 218 | */ |
| 219 | #define __put_user_nocheck(val, ptr, size) \ |
| 220 | ({ \ |
| 221 | __typeof__(*(ptr)) __pu_val; \ |
| 222 | long __pu_err = 0; \ |
| 223 | \ |
| 224 | __pu_val = (val); \ |
| 225 | switch (size) { \ |
| 226 | case 1: \ |
| 227 | __put_user_asm("sb", ptr); \ |
| 228 | break; \ |
| 229 | case 2: \ |
| 230 | __put_user_asm("sh", ptr); \ |
| 231 | break; \ |
| 232 | case 4: \ |
| 233 | __put_user_asm("sw", ptr); \ |
| 234 | break; \ |
| 235 | case 8: \ |
| 236 | if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \ |
| 237 | __pu_err = 0; \ |
| 238 | else \ |
| 239 | __pu_err = -EFAULT; \ |
| 240 | break; \ |
| 241 | default: \ |
| 242 | __put_user_unknown(); \ |
| 243 | break; \ |
| 244 | } \ |
| 245 | __pu_err; \ |
| 246 | }) |
| 247 | |
| 248 | |
| 249 | #define __put_user_check(val, ptr, size) \ |
| 250 | ({ \ |
| 251 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 252 | __typeof__(*(ptr)) __pu_val = (val); \ |
| 253 | long __pu_err = -EFAULT; \ |
| 254 | \ |
| 255 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \ |
| 256 | switch (size) { \ |
| 257 | case 1: \ |
| 258 | __put_user_asm("sb", __pu_addr); \ |
| 259 | break; \ |
| 260 | case 2: \ |
| 261 | __put_user_asm("sh", __pu_addr); \ |
| 262 | break; \ |
| 263 | case 4: \ |
| 264 | __put_user_asm("sw", __pu_addr); \ |
| 265 | break; \ |
| 266 | case 8: \ |
| 267 | if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\ |
| 268 | __pu_err = 0; \ |
| 269 | else \ |
| 270 | __pu_err = -EFAULT; \ |
| 271 | break; \ |
| 272 | default: \ |
| 273 | __put_user_unknown(); \ |
| 274 | break; \ |
| 275 | } \ |
| 276 | } \ |
| 277 | __pu_err; \ |
| 278 | }) |
| 279 | |
| 280 | #define __put_user_asm(insn, ptr) \ |
| 281 | __asm__ __volatile__( \ |
| 282 | "1:" insn " %2, %3\n" \ |
| 283 | "2:\n" \ |
| 284 | ".section .fixup,\"ax\"\n" \ |
| 285 | "3:li %0, %4\n" \ |
| 286 | "j 2b\n" \ |
| 287 | ".previous\n" \ |
| 288 | ".section __ex_table,\"a\"\n" \ |
| 289 | ".word 1b, 3b\n" \ |
| 290 | ".previous\n" \ |
| 291 | : "=r" (__pu_err) \ |
| 292 | : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \ |
| 293 | "i" (-EFAULT)); |
| 294 | |
| 295 | extern void __put_user_unknown(void); |
| 296 | extern int __copy_tofrom_user(void *to, const void *from, unsigned long len); |
| 297 | |
| 298 | static inline unsigned long |
| 299 | copy_from_user(void *to, const void *from, unsigned long len) |
| 300 | { |
| 301 | unsigned long over; |
| 302 | |
| 303 | if (access_ok(VERIFY_READ, from, len)) |
| 304 | return __copy_tofrom_user(to, from, len); |
| 305 | |
| 306 | if ((unsigned long)from < TASK_SIZE) { |
| 307 | over = (unsigned long)from + len - TASK_SIZE; |
| 308 | return __copy_tofrom_user(to, from, len - over) + over; |
| 309 | } |
| 310 | return len; |
| 311 | } |
| 312 | |
| 313 | static inline unsigned long |
| 314 | copy_to_user(void *to, const void *from, unsigned long len) |
| 315 | { |
| 316 | unsigned long over; |
| 317 | |
| 318 | if (access_ok(VERIFY_WRITE, to, len)) |
| 319 | return __copy_tofrom_user(to, from, len); |
| 320 | |
| 321 | if ((unsigned long)to < TASK_SIZE) { |
| 322 | over = (unsigned long)to + len - TASK_SIZE; |
| 323 | return __copy_tofrom_user(to, from, len - over) + over; |
| 324 | } |
| 325 | return len; |
| 326 | } |
| 327 | |
| 328 | #define __copy_from_user(to, from, len) \ |
| 329 | __copy_tofrom_user((to), (from), (len)) |
| 330 | |
| 331 | #define __copy_to_user(to, from, len) \ |
| 332 | __copy_tofrom_user((to), (from), (len)) |
| 333 | |
| 334 | static inline unsigned long |
| 335 | __copy_to_user_inatomic(void *to, const void *from, unsigned long len) |
| 336 | { |
| 337 | return __copy_to_user(to, from, len); |
| 338 | } |
| 339 | |
| 340 | static inline unsigned long |
| 341 | __copy_from_user_inatomic(void *to, const void *from, unsigned long len) |
| 342 | { |
| 343 | return __copy_from_user(to, from, len); |
| 344 | } |
| 345 | |
| 346 | #define __copy_in_user(to, from, len) __copy_from_user(to, from, len) |
| 347 | |
| 348 | static inline unsigned long |
| 349 | copy_in_user(void *to, const void *from, unsigned long len) |
| 350 | { |
| 351 | if (access_ok(VERIFY_READ, from, len) && |
| 352 | access_ok(VERFITY_WRITE, to, len)) |
| 353 | return copy_from_user(to, from, len); |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * __clear_user: - Zero a block of memory in user space, with less checking. |
| 358 | * @to: Destination address, in user space. |
| 359 | * @n: Number of bytes to zero. |
| 360 | * |
| 361 | * Zero a block of memory in user space. Caller must check |
| 362 | * the specified block with access_ok() before calling this function. |
| 363 | * |
| 364 | * Returns number of bytes that could not be cleared. |
| 365 | * On success, this will be zero. |
| 366 | */ |
| 367 | extern unsigned long __clear_user(void __user *src, unsigned long size); |
| 368 | |
| 369 | static inline unsigned long clear_user(char *src, unsigned long size) |
| 370 | { |
| 371 | if (access_ok(VERIFY_WRITE, src, size)) |
| 372 | return __clear_user(src, size); |
| 373 | |
| 374 | return -EFAULT; |
| 375 | } |
| 376 | /* |
| 377 | * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking. |
| 378 | * @dst: Destination address, in kernel space. This buffer must be at |
| 379 | * least @count bytes long. |
| 380 | * @src: Source address, in user space. |
| 381 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 382 | * |
| 383 | * Copies a NUL-terminated string from userspace to kernel space. |
| 384 | * Caller must check the specified block with access_ok() before calling |
| 385 | * this function. |
| 386 | * |
| 387 | * On success, returns the length of the string (not including the trailing |
| 388 | * NUL). |
| 389 | * |
| 390 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 391 | * copied). |
| 392 | * |
| 393 | * If @count is smaller than the length of the string, copies @count bytes |
| 394 | * and returns @count. |
| 395 | */ |
| 396 | extern int __strncpy_from_user(char *dst, const char *src, long len); |
| 397 | |
| 398 | static inline int strncpy_from_user(char *dst, const char *src, long len) |
| 399 | { |
| 400 | if (access_ok(VERIFY_READ, src, 1)) |
| 401 | return __strncpy_from_user(dst, src, len); |
| 402 | |
| 403 | return -EFAULT; |
| 404 | } |
| 405 | |
| 406 | extern int __strlen_user(const char *src); |
| 407 | static inline long strlen_user(const char __user *src) |
| 408 | { |
| 409 | return __strlen_user(src); |
| 410 | } |
| 411 | |
| 412 | extern int __strnlen_user(const char *str, long len); |
| 413 | static inline long strnlen_user(const char __user *str, long len) |
| 414 | { |
| 415 | if (!access_ok(VERIFY_READ, str, 0)) |
| 416 | return 0; |
| 417 | else |
| 418 | return __strnlen_user(str, len); |
| 419 | } |
| 420 | |
| 421 | struct exception_table_entry { |
| 422 | unsigned long insn; |
| 423 | unsigned long fixup; |
| 424 | }; |
| 425 | |
Chen Liqin | 6bc9a39 | 2009-06-12 22:01:00 +0800 | [diff] [blame] | 426 | extern int fixup_exception(struct pt_regs *regs); |
| 427 | |
Chen Liqin | ffa818b | 2009-08-30 12:30:16 +0800 | [diff] [blame] | 428 | #endif /* __SCORE_UACCESS_H */ |
Chen Liqin | 6bc9a39 | 2009-06-12 22:01:00 +0800 | [diff] [blame] | 429 | |