Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle |
| 7 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 8 | * Copyright (C) 2007 Maciej W. Rozycki |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | #ifndef _ASM_UACCESS_H |
| 11 | #define _ASM_UACCESS_H |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/thread_info.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * The fs value determines whether argument validity checking should be |
| 19 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 20 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 21 | * |
| 22 | * For historical reasons, these macros are grossly misnamed. |
| 23 | */ |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_32BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Sanjay Lal | 9843b03 | 2012-11-21 18:34:03 -0800 | [diff] [blame] | 26 | #ifdef CONFIG_KVM_GUEST |
| 27 | #define __UA_LIMIT 0x40000000UL |
| 28 | #else |
| 29 | #define __UA_LIMIT 0x80000000UL |
| 30 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #define __UA_ADDR ".word" |
| 33 | #define __UA_LA "la" |
| 34 | #define __UA_ADDU "addu" |
| 35 | #define __UA_t0 "$8" |
| 36 | #define __UA_t1 "$9" |
| 37 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 38 | #endif /* CONFIG_32BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 40 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
David Daney | 949e51b | 2010-10-14 11:32:33 -0700 | [diff] [blame] | 42 | extern u64 __ua_limit; |
| 43 | |
| 44 | #define __UA_LIMIT __ua_limit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #define __UA_ADDR ".dword" |
| 47 | #define __UA_LA "dla" |
| 48 | #define __UA_ADDU "daddu" |
| 49 | #define __UA_t0 "$12" |
| 50 | #define __UA_t1 "$13" |
| 51 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 52 | #endif /* CONFIG_64BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * USER_DS is a bitmask that has the bits set that may not be set in a valid |
| 56 | * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but |
| 57 | * the arithmetic we're doing only works if the limit is a power of two, so |
| 58 | * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid |
| 59 | * address in this range it's the process's problem, not ours :-) |
| 60 | */ |
| 61 | |
Sanjay Lal | 9843b03 | 2012-11-21 18:34:03 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_KVM_GUEST |
| 63 | #define KERNEL_DS ((mm_segment_t) { 0x80000000UL }) |
| 64 | #define USER_DS ((mm_segment_t) { 0xC0000000UL }) |
| 65 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #define KERNEL_DS ((mm_segment_t) { 0UL }) |
| 67 | #define USER_DS ((mm_segment_t) { __UA_LIMIT }) |
Sanjay Lal | 9843b03 | 2012-11-21 18:34:03 -0800 | [diff] [blame] | 68 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #define VERIFY_READ 0 |
| 71 | #define VERIFY_WRITE 1 |
| 72 | |
| 73 | #define get_ds() (KERNEL_DS) |
| 74 | #define get_fs() (current_thread_info()->addr_limit) |
| 75 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) |
| 76 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 77 | #define segment_eq(a, b) ((a).seg == (b).seg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | /* |
| 81 | * Is a address valid? This does a straighforward calculation rather |
| 82 | * than tests. |
| 83 | * |
| 84 | * Address valid if: |
| 85 | * - "addr" doesn't have any high-bits set |
| 86 | * - AND "size" doesn't have any high-bits set |
| 87 | * - AND "addr+size" doesn't have any high-bits set |
| 88 | * - OR we are in kernel mode. |
| 89 | * |
| 90 | * __ua_size() is a trick to avoid runtime checking of positive constant |
| 91 | * sizes; for those we already know at compile time that the size is ok. |
| 92 | */ |
| 93 | #define __ua_size(size) \ |
| 94 | ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size)) |
| 95 | |
| 96 | /* |
| 97 | * access_ok: - Checks if a user space pointer is valid |
| 98 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 99 | * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe |
| 100 | * to write to a block, it is always safe to read from it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | * @addr: User space pointer to start of block to check |
| 102 | * @size: Size of block to check |
| 103 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 104 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * |
| 106 | * Checks if a pointer to a block of memory in user space is valid. |
| 107 | * |
| 108 | * Returns true (nonzero) if the memory block may be valid, false (zero) |
| 109 | * if it is definitely invalid. |
| 110 | * |
| 111 | * Note that, depending on architecture, this function probably just |
| 112 | * checks that the pointer is in the user space range - after calling |
| 113 | * this function, memory access functions may still return -EFAULT. |
| 114 | */ |
| 115 | |
| 116 | #define __access_mask get_fs().seg |
| 117 | |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 118 | #define __access_ok(addr, size, mask) \ |
| 119 | ({ \ |
| 120 | unsigned long __addr = (unsigned long) (addr); \ |
| 121 | unsigned long __size = size; \ |
| 122 | unsigned long __mask = mask; \ |
| 123 | unsigned long __ok; \ |
| 124 | \ |
| 125 | __chk_user_ptr(addr); \ |
| 126 | __ok = (signed long)(__mask & (__addr | (__addr + __size) | \ |
| 127 | __ua_size(__size))); \ |
| 128 | __ok == 0; \ |
Ralf Baechle | d0aab92 | 2009-04-27 15:31:34 +0200 | [diff] [blame] | 129 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | #define access_ok(type, addr, size) \ |
Ralf Baechle | d0aab92 | 2009-04-27 15:31:34 +0200 | [diff] [blame] | 132 | likely(__access_ok((addr), (size), __access_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * put_user: - Write a simple value into user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 136 | * @x: Value to copy to user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * @ptr: Destination address, in user space. |
| 138 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 139 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * |
| 141 | * This macro copies a single simple value from kernel space to user |
| 142 | * space. It supports simple types like char and int, but not larger |
| 143 | * data types like structures or arrays. |
| 144 | * |
| 145 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 146 | * to the result of dereferencing @ptr. |
| 147 | * |
| 148 | * Returns zero on success, or -EFAULT on error. |
| 149 | */ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 150 | #define put_user(x,ptr) \ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 151 | __put_user_check((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * get_user: - Get a simple variable from user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 155 | * @x: Variable to store result. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * @ptr: Source address, in user space. |
| 157 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 158 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | * |
| 160 | * This macro copies a single simple variable from user space to kernel |
| 161 | * space. It supports simple types like char and int, but not larger |
| 162 | * data types like structures or arrays. |
| 163 | * |
| 164 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 165 | * dereferencing @ptr must be assignable to @x without a cast. |
| 166 | * |
| 167 | * Returns zero on success, or -EFAULT on error. |
| 168 | * On error, the variable @x is set to zero. |
| 169 | */ |
| 170 | #define get_user(x,ptr) \ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 171 | __get_user_check((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * __put_user: - Write a simple value into user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 175 | * @x: Value to copy to user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * @ptr: Destination address, in user space. |
| 177 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 178 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * |
| 180 | * This macro copies a single simple value from kernel space to user |
| 181 | * space. It supports simple types like char and int, but not larger |
| 182 | * data types like structures or arrays. |
| 183 | * |
| 184 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 185 | * to the result of dereferencing @ptr. |
| 186 | * |
| 187 | * Caller must check the pointer with access_ok() before calling this |
| 188 | * function. |
| 189 | * |
| 190 | * Returns zero on success, or -EFAULT on error. |
| 191 | */ |
| 192 | #define __put_user(x,ptr) \ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 193 | __put_user_nocheck((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * __get_user: - Get a simple variable from user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 197 | * @x: Variable to store result. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | * @ptr: Source address, in user space. |
| 199 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 200 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * |
| 202 | * This macro copies a single simple variable from user space to kernel |
| 203 | * space. It supports simple types like char and int, but not larger |
| 204 | * data types like structures or arrays. |
| 205 | * |
| 206 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 207 | * dereferencing @ptr must be assignable to @x without a cast. |
| 208 | * |
| 209 | * Caller must check the pointer with access_ok() before calling this |
| 210 | * function. |
| 211 | * |
| 212 | * Returns zero on success, or -EFAULT on error. |
| 213 | * On error, the variable @x is set to zero. |
| 214 | */ |
| 215 | #define __get_user(x,ptr) \ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 216 | __get_user_nocheck((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | struct __large_struct { unsigned long buf[100]; }; |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 219 | #define __m(x) (*(struct __large_struct __user *)(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * Yuck. We need two variants, one for 64bit operation and one |
| 223 | * for 32 bit mode and old iron. |
| 224 | */ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 225 | #ifdef CONFIG_32BIT |
| 226 | #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | #endif |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 228 | #ifdef CONFIG_64BIT |
| 229 | #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr) |
| 230 | #endif |
| 231 | |
| 232 | extern void __get_user_unknown(void); |
| 233 | |
| 234 | #define __get_user_common(val, size, ptr) \ |
| 235 | do { \ |
| 236 | switch (size) { \ |
| 237 | case 1: __get_user_asm(val, "lb", ptr); break; \ |
| 238 | case 2: __get_user_asm(val, "lh", ptr); break; \ |
| 239 | case 4: __get_user_asm(val, "lw", ptr); break; \ |
| 240 | case 8: __GET_USER_DW(val, ptr); break; \ |
| 241 | default: __get_user_unknown(); break; \ |
| 242 | } \ |
| 243 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 245 | #define __get_user_nocheck(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | ({ \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 247 | int __gu_err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 249 | __chk_user_ptr(ptr); \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 250 | __get_user_common((x), size, ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | __gu_err; \ |
| 252 | }) |
| 253 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 254 | #define __get_user_check(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | ({ \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 256 | int __gu_err = -EFAULT; \ |
Atsushi Nemoto | 8ecbbca | 2006-02-14 15:57:50 +0900 | [diff] [blame] | 257 | const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 259 | might_fault(); \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 260 | if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \ |
| 261 | __get_user_common((x), size, __gu_ptr); \ |
| 262 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | __gu_err; \ |
| 264 | }) |
| 265 | |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 266 | #define __get_user_asm(val, insn, addr) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 267 | { \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 268 | long __gu_tmp; \ |
| 269 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | __asm__ __volatile__( \ |
| 271 | "1: " insn " %1, %3 \n" \ |
| 272 | "2: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 273 | " .insn \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | " .section .fixup,\"ax\" \n" \ |
| 275 | "3: li %0, %4 \n" \ |
| 276 | " j 2b \n" \ |
| 277 | " .previous \n" \ |
| 278 | " .section __ex_table,\"a\" \n" \ |
| 279 | " "__UA_ADDR "\t1b, 3b \n" \ |
| 280 | " .previous \n" \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 281 | : "=r" (__gu_err), "=r" (__gu_tmp) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 282 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 283 | \ |
Atsushi Nemoto | 8ecbbca | 2006-02-14 15:57:50 +0900 | [diff] [blame] | 284 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * Get a long long 64 using 32 bit registers. |
| 289 | */ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 290 | #define __get_user_asm_ll32(val, addr) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 291 | { \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 292 | union { \ |
| 293 | unsigned long long l; \ |
| 294 | __typeof__(*(addr)) t; \ |
| 295 | } __gu_tmp; \ |
Ralf Baechle | cd1fb9e | 2007-02-12 23:12:38 +0000 | [diff] [blame] | 296 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | __asm__ __volatile__( \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 298 | "1: lw %1, (%3) \n" \ |
| 299 | "2: lw %D1, 4(%3) \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 300 | "3: \n" \ |
| 301 | " .insn \n" \ |
| 302 | " .section .fixup,\"ax\" \n" \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 303 | "4: li %0, %4 \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | " move %1, $0 \n" \ |
| 305 | " move %D1, $0 \n" \ |
| 306 | " j 3b \n" \ |
| 307 | " .previous \n" \ |
| 308 | " .section __ex_table,\"a\" \n" \ |
| 309 | " " __UA_ADDR " 1b, 4b \n" \ |
| 310 | " " __UA_ADDR " 2b, 4b \n" \ |
| 311 | " .previous \n" \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 312 | : "=r" (__gu_err), "=&r" (__gu_tmp.l) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 313 | : "0" (0), "r" (addr), "i" (-EFAULT)); \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 314 | \ |
| 315 | (val) = __gu_tmp.t; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* |
| 319 | * Yuck. We need two variants, one for 64bit operation and one |
| 320 | * for 32 bit mode and old iron. |
| 321 | */ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 322 | #ifdef CONFIG_32BIT |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 323 | #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | #endif |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 325 | #ifdef CONFIG_64BIT |
| 326 | #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr) |
| 327 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 329 | #define __put_user_nocheck(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | ({ \ |
| 331 | __typeof__(*(ptr)) __pu_val; \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 332 | int __pu_err = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 334 | __chk_user_ptr(ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | __pu_val = (x); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | switch (size) { \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 337 | case 1: __put_user_asm("sb", ptr); break; \ |
| 338 | case 2: __put_user_asm("sh", ptr); break; \ |
| 339 | case 4: __put_user_asm("sw", ptr); break; \ |
| 340 | case 8: __PUT_USER_DW(ptr); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | default: __put_user_unknown(); break; \ |
| 342 | } \ |
| 343 | __pu_err; \ |
| 344 | }) |
| 345 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 346 | #define __put_user_check(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 348 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 349 | __typeof__(*(ptr)) __pu_val = (x); \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 350 | int __pu_err = -EFAULT; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 352 | might_fault(); \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 353 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | switch (size) { \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 355 | case 1: __put_user_asm("sb", __pu_addr); break; \ |
| 356 | case 2: __put_user_asm("sh", __pu_addr); break; \ |
| 357 | case 4: __put_user_asm("sw", __pu_addr); break; \ |
| 358 | case 8: __PUT_USER_DW(__pu_addr); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | default: __put_user_unknown(); break; \ |
| 360 | } \ |
| 361 | } \ |
| 362 | __pu_err; \ |
| 363 | }) |
| 364 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 365 | #define __put_user_asm(insn, ptr) \ |
| 366 | { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | __asm__ __volatile__( \ |
| 368 | "1: " insn " %z2, %3 # __put_user_asm\n" \ |
| 369 | "2: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 370 | " .insn \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | " .section .fixup,\"ax\" \n" \ |
| 372 | "3: li %0, %4 \n" \ |
| 373 | " j 2b \n" \ |
| 374 | " .previous \n" \ |
| 375 | " .section __ex_table,\"a\" \n" \ |
| 376 | " " __UA_ADDR " 1b, 3b \n" \ |
| 377 | " .previous \n" \ |
| 378 | : "=r" (__pu_err) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 379 | : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | "i" (-EFAULT)); \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 383 | #define __put_user_asm_ll32(ptr) \ |
| 384 | { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | __asm__ __volatile__( \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 386 | "1: sw %2, (%3) # __put_user_asm_ll32 \n" \ |
| 387 | "2: sw %D2, 4(%3) \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | "3: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 389 | " .insn \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | " .section .fixup,\"ax\" \n" \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 391 | "4: li %0, %4 \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | " j 3b \n" \ |
| 393 | " .previous \n" \ |
| 394 | " .section __ex_table,\"a\" \n" \ |
| 395 | " " __UA_ADDR " 1b, 4b \n" \ |
| 396 | " " __UA_ADDR " 2b, 4b \n" \ |
| 397 | " .previous" \ |
| 398 | : "=r" (__pu_err) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 399 | : "0" (0), "r" (__pu_val), "r" (ptr), \ |
| 400 | "i" (-EFAULT)); \ |
| 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | extern void __put_user_unknown(void); |
| 404 | |
| 405 | /* |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 406 | * put_user_unaligned: - Write a simple value into user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 407 | * @x: Value to copy to user space. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 408 | * @ptr: Destination address, in user space. |
| 409 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 410 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 411 | * |
| 412 | * This macro copies a single simple value from kernel space to user |
| 413 | * space. It supports simple types like char and int, but not larger |
| 414 | * data types like structures or arrays. |
| 415 | * |
| 416 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 417 | * to the result of dereferencing @ptr. |
| 418 | * |
| 419 | * Returns zero on success, or -EFAULT on error. |
| 420 | */ |
| 421 | #define put_user_unaligned(x,ptr) \ |
| 422 | __put_user_unaligned_check((x),(ptr),sizeof(*(ptr))) |
| 423 | |
| 424 | /* |
| 425 | * get_user_unaligned: - Get a simple variable from user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 426 | * @x: Variable to store result. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 427 | * @ptr: Source address, in user space. |
| 428 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 429 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 430 | * |
| 431 | * This macro copies a single simple variable from user space to kernel |
| 432 | * space. It supports simple types like char and int, but not larger |
| 433 | * data types like structures or arrays. |
| 434 | * |
| 435 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 436 | * dereferencing @ptr must be assignable to @x without a cast. |
| 437 | * |
| 438 | * Returns zero on success, or -EFAULT on error. |
| 439 | * On error, the variable @x is set to zero. |
| 440 | */ |
| 441 | #define get_user_unaligned(x,ptr) \ |
| 442 | __get_user_unaligned_check((x),(ptr),sizeof(*(ptr))) |
| 443 | |
| 444 | /* |
| 445 | * __put_user_unaligned: - Write a simple value into user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 446 | * @x: Value to copy to user space. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 447 | * @ptr: Destination address, in user space. |
| 448 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 449 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 450 | * |
| 451 | * This macro copies a single simple value from kernel space to user |
| 452 | * space. It supports simple types like char and int, but not larger |
| 453 | * data types like structures or arrays. |
| 454 | * |
| 455 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 456 | * to the result of dereferencing @ptr. |
| 457 | * |
| 458 | * Caller must check the pointer with access_ok() before calling this |
| 459 | * function. |
| 460 | * |
| 461 | * Returns zero on success, or -EFAULT on error. |
| 462 | */ |
| 463 | #define __put_user_unaligned(x,ptr) \ |
| 464 | __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr))) |
| 465 | |
| 466 | /* |
| 467 | * __get_user_unaligned: - Get a simple variable from user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 468 | * @x: Variable to store result. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 469 | * @ptr: Source address, in user space. |
| 470 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 471 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 472 | * |
| 473 | * This macro copies a single simple variable from user space to kernel |
| 474 | * space. It supports simple types like char and int, but not larger |
| 475 | * data types like structures or arrays. |
| 476 | * |
| 477 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 478 | * dereferencing @ptr must be assignable to @x without a cast. |
| 479 | * |
| 480 | * Caller must check the pointer with access_ok() before calling this |
| 481 | * function. |
| 482 | * |
| 483 | * Returns zero on success, or -EFAULT on error. |
| 484 | * On error, the variable @x is set to zero. |
| 485 | */ |
| 486 | #define __get_user_unaligned(x,ptr) \ |
| 487 | __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr))) |
| 488 | |
| 489 | /* |
| 490 | * Yuck. We need two variants, one for 64bit operation and one |
| 491 | * for 32 bit mode and old iron. |
| 492 | */ |
| 493 | #ifdef CONFIG_32BIT |
| 494 | #define __GET_USER_UNALIGNED_DW(val, ptr) \ |
| 495 | __get_user_unaligned_asm_ll32(val, ptr) |
| 496 | #endif |
| 497 | #ifdef CONFIG_64BIT |
| 498 | #define __GET_USER_UNALIGNED_DW(val, ptr) \ |
| 499 | __get_user_unaligned_asm(val, "uld", ptr) |
| 500 | #endif |
| 501 | |
| 502 | extern void __get_user_unaligned_unknown(void); |
| 503 | |
| 504 | #define __get_user_unaligned_common(val, size, ptr) \ |
| 505 | do { \ |
| 506 | switch (size) { \ |
| 507 | case 1: __get_user_asm(val, "lb", ptr); break; \ |
| 508 | case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \ |
| 509 | case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \ |
| 510 | case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \ |
| 511 | default: __get_user_unaligned_unknown(); break; \ |
| 512 | } \ |
| 513 | } while (0) |
| 514 | |
| 515 | #define __get_user_unaligned_nocheck(x,ptr,size) \ |
| 516 | ({ \ |
| 517 | int __gu_err; \ |
| 518 | \ |
| 519 | __get_user_unaligned_common((x), size, ptr); \ |
| 520 | __gu_err; \ |
| 521 | }) |
| 522 | |
| 523 | #define __get_user_unaligned_check(x,ptr,size) \ |
| 524 | ({ \ |
| 525 | int __gu_err = -EFAULT; \ |
| 526 | const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \ |
| 527 | \ |
| 528 | if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \ |
| 529 | __get_user_unaligned_common((x), size, __gu_ptr); \ |
| 530 | \ |
| 531 | __gu_err; \ |
| 532 | }) |
| 533 | |
| 534 | #define __get_user_unaligned_asm(val, insn, addr) \ |
| 535 | { \ |
| 536 | long __gu_tmp; \ |
| 537 | \ |
| 538 | __asm__ __volatile__( \ |
| 539 | "1: " insn " %1, %3 \n" \ |
| 540 | "2: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 541 | " .insn \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 542 | " .section .fixup,\"ax\" \n" \ |
| 543 | "3: li %0, %4 \n" \ |
| 544 | " j 2b \n" \ |
| 545 | " .previous \n" \ |
| 546 | " .section __ex_table,\"a\" \n" \ |
| 547 | " "__UA_ADDR "\t1b, 3b \n" \ |
| 548 | " "__UA_ADDR "\t1b + 4, 3b \n" \ |
| 549 | " .previous \n" \ |
| 550 | : "=r" (__gu_err), "=r" (__gu_tmp) \ |
| 551 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \ |
| 552 | \ |
| 553 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Get a long long 64 using 32 bit registers. |
| 558 | */ |
| 559 | #define __get_user_unaligned_asm_ll32(val, addr) \ |
| 560 | { \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 561 | unsigned long long __gu_tmp; \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 562 | \ |
| 563 | __asm__ __volatile__( \ |
| 564 | "1: ulw %1, (%3) \n" \ |
| 565 | "2: ulw %D1, 4(%3) \n" \ |
| 566 | " move %0, $0 \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 567 | "3: \n" \ |
| 568 | " .insn \n" \ |
| 569 | " .section .fixup,\"ax\" \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 570 | "4: li %0, %4 \n" \ |
| 571 | " move %1, $0 \n" \ |
| 572 | " move %D1, $0 \n" \ |
| 573 | " j 3b \n" \ |
| 574 | " .previous \n" \ |
| 575 | " .section __ex_table,\"a\" \n" \ |
| 576 | " " __UA_ADDR " 1b, 4b \n" \ |
| 577 | " " __UA_ADDR " 1b + 4, 4b \n" \ |
| 578 | " " __UA_ADDR " 2b, 4b \n" \ |
| 579 | " " __UA_ADDR " 2b + 4, 4b \n" \ |
| 580 | " .previous \n" \ |
| 581 | : "=r" (__gu_err), "=&r" (__gu_tmp) \ |
| 582 | : "0" (0), "r" (addr), "i" (-EFAULT)); \ |
| 583 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Yuck. We need two variants, one for 64bit operation and one |
| 588 | * for 32 bit mode and old iron. |
| 589 | */ |
| 590 | #ifdef CONFIG_32BIT |
| 591 | #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr) |
| 592 | #endif |
| 593 | #ifdef CONFIG_64BIT |
| 594 | #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr) |
| 595 | #endif |
| 596 | |
| 597 | #define __put_user_unaligned_nocheck(x,ptr,size) \ |
| 598 | ({ \ |
| 599 | __typeof__(*(ptr)) __pu_val; \ |
| 600 | int __pu_err = 0; \ |
| 601 | \ |
| 602 | __pu_val = (x); \ |
| 603 | switch (size) { \ |
| 604 | case 1: __put_user_asm("sb", ptr); break; \ |
| 605 | case 2: __put_user_unaligned_asm("ush", ptr); break; \ |
| 606 | case 4: __put_user_unaligned_asm("usw", ptr); break; \ |
| 607 | case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \ |
| 608 | default: __put_user_unaligned_unknown(); break; \ |
| 609 | } \ |
| 610 | __pu_err; \ |
| 611 | }) |
| 612 | |
| 613 | #define __put_user_unaligned_check(x,ptr,size) \ |
| 614 | ({ \ |
| 615 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 616 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 617 | int __pu_err = -EFAULT; \ |
| 618 | \ |
| 619 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \ |
| 620 | switch (size) { \ |
| 621 | case 1: __put_user_asm("sb", __pu_addr); break; \ |
| 622 | case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \ |
| 623 | case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \ |
| 624 | case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \ |
| 625 | default: __put_user_unaligned_unknown(); break; \ |
| 626 | } \ |
| 627 | } \ |
| 628 | __pu_err; \ |
| 629 | }) |
| 630 | |
| 631 | #define __put_user_unaligned_asm(insn, ptr) \ |
| 632 | { \ |
| 633 | __asm__ __volatile__( \ |
| 634 | "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \ |
| 635 | "2: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 636 | " .insn \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 637 | " .section .fixup,\"ax\" \n" \ |
| 638 | "3: li %0, %4 \n" \ |
| 639 | " j 2b \n" \ |
| 640 | " .previous \n" \ |
| 641 | " .section __ex_table,\"a\" \n" \ |
| 642 | " " __UA_ADDR " 1b, 3b \n" \ |
| 643 | " .previous \n" \ |
| 644 | : "=r" (__pu_err) \ |
| 645 | : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \ |
| 646 | "i" (-EFAULT)); \ |
| 647 | } |
| 648 | |
| 649 | #define __put_user_unaligned_asm_ll32(ptr) \ |
| 650 | { \ |
| 651 | __asm__ __volatile__( \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 652 | "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 653 | "2: sw %D2, 4(%3) \n" \ |
| 654 | "3: \n" \ |
Steven J. Hill | 1658f91 | 2013-03-25 13:22:59 -0500 | [diff] [blame] | 655 | " .insn \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 656 | " .section .fixup,\"ax\" \n" \ |
| 657 | "4: li %0, %4 \n" \ |
| 658 | " j 3b \n" \ |
| 659 | " .previous \n" \ |
| 660 | " .section __ex_table,\"a\" \n" \ |
| 661 | " " __UA_ADDR " 1b, 4b \n" \ |
| 662 | " " __UA_ADDR " 1b + 4, 4b \n" \ |
| 663 | " " __UA_ADDR " 2b, 4b \n" \ |
| 664 | " " __UA_ADDR " 2b + 4, 4b \n" \ |
| 665 | " .previous" \ |
| 666 | : "=r" (__pu_err) \ |
| 667 | : "0" (0), "r" (__pu_val), "r" (ptr), \ |
| 668 | "i" (-EFAULT)); \ |
| 669 | } |
| 670 | |
| 671 | extern void __put_user_unaligned_unknown(void); |
| 672 | |
| 673 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * We're generating jump to subroutines which will be outside the range of |
| 675 | * jump instructions |
| 676 | */ |
| 677 | #ifdef MODULE |
| 678 | #define __MODULE_JAL(destination) \ |
| 679 | ".set\tnoat\n\t" \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 680 | __UA_LA "\t$1, " #destination "\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | "jalr\t$1\n\t" \ |
| 682 | ".set\tat\n\t" |
| 683 | #else |
| 684 | #define __MODULE_JAL(destination) \ |
| 685 | "jal\t" #destination "\n\t" |
| 686 | #endif |
| 687 | |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 688 | #ifndef CONFIG_CPU_DADDI_WORKAROUNDS |
| 689 | #define DADDI_SCRATCH "$0" |
| 690 | #else |
| 691 | #define DADDI_SCRATCH "$3" |
| 692 | #endif |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | extern size_t __copy_user(void *__to, const void *__from, size_t __n); |
| 695 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 696 | #define __invoke_copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 698 | register void __user *__cu_to_r __asm__("$4"); \ |
| 699 | register const void *__cu_from_r __asm__("$5"); \ |
| 700 | register long __cu_len_r __asm__("$6"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | \ |
| 702 | __cu_to_r = (to); \ |
| 703 | __cu_from_r = (from); \ |
| 704 | __cu_len_r = (n); \ |
| 705 | __asm__ __volatile__( \ |
| 706 | __MODULE_JAL(__copy_user) \ |
| 707 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 708 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 709 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 710 | DADDI_SCRATCH, "memory"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | __cu_len_r; \ |
| 712 | }) |
| 713 | |
| 714 | /* |
| 715 | * __copy_to_user: - Copy a block of data into user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 716 | * @to: Destination address, in user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | * @from: Source address, in kernel space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 718 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 720 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | * |
| 722 | * Copy data from kernel space to user space. Caller must check |
| 723 | * the specified block with access_ok() before calling this function. |
| 724 | * |
| 725 | * Returns number of bytes that could not be copied. |
| 726 | * On success, this will be zero. |
| 727 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 728 | #define __copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 730 | void __user *__cu_to; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | const void *__cu_from; \ |
| 732 | long __cu_len; \ |
| 733 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | __cu_to = (to); \ |
| 735 | __cu_from = (from); \ |
| 736 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 737 | might_fault(); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 738 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | __cu_len; \ |
| 740 | }) |
| 741 | |
Ralf Baechle | d0c91ae | 2007-03-05 15:54:20 +0000 | [diff] [blame] | 742 | extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n); |
| 743 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 744 | #define __copy_to_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 745 | ({ \ |
| 746 | void __user *__cu_to; \ |
| 747 | const void *__cu_from; \ |
| 748 | long __cu_len; \ |
| 749 | \ |
| 750 | __cu_to = (to); \ |
| 751 | __cu_from = (from); \ |
| 752 | __cu_len = (n); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 753 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 754 | __cu_len; \ |
| 755 | }) |
| 756 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 757 | #define __copy_from_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 758 | ({ \ |
| 759 | void *__cu_to; \ |
| 760 | const void __user *__cu_from; \ |
| 761 | long __cu_len; \ |
| 762 | \ |
| 763 | __cu_to = (to); \ |
| 764 | __cu_from = (from); \ |
| 765 | __cu_len = (n); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 766 | __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \ |
| 767 | __cu_len); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 768 | __cu_len; \ |
| 769 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * copy_to_user: - Copy a block of data into user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 773 | * @to: Destination address, in user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * @from: Source address, in kernel space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 775 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 777 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | * |
| 779 | * Copy data from kernel space to user space. |
| 780 | * |
| 781 | * Returns number of bytes that could not be copied. |
| 782 | * On success, this will be zero. |
| 783 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 784 | #define copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 786 | void __user *__cu_to; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | const void *__cu_from; \ |
| 788 | long __cu_len; \ |
| 789 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | __cu_to = (to); \ |
| 791 | __cu_from = (from); \ |
| 792 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 793 | if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \ |
| 794 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 796 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 797 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | __cu_len; \ |
| 799 | }) |
| 800 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 801 | #define __invoke_copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 803 | register void *__cu_to_r __asm__("$4"); \ |
| 804 | register const void __user *__cu_from_r __asm__("$5"); \ |
| 805 | register long __cu_len_r __asm__("$6"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | \ |
| 807 | __cu_to_r = (to); \ |
| 808 | __cu_from_r = (from); \ |
| 809 | __cu_len_r = (n); \ |
| 810 | __asm__ __volatile__( \ |
| 811 | ".set\tnoreorder\n\t" \ |
| 812 | __MODULE_JAL(__copy_user) \ |
| 813 | ".set\tnoat\n\t" \ |
| 814 | __UA_ADDU "\t$1, %1, %2\n\t" \ |
| 815 | ".set\tat\n\t" \ |
| 816 | ".set\treorder" \ |
| 817 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 818 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 819 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 820 | DADDI_SCRATCH, "memory"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | __cu_len_r; \ |
| 822 | }) |
| 823 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 824 | #define __invoke_copy_from_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 825 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 826 | register void *__cu_to_r __asm__("$4"); \ |
| 827 | register const void __user *__cu_from_r __asm__("$5"); \ |
| 828 | register long __cu_len_r __asm__("$6"); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 829 | \ |
| 830 | __cu_to_r = (to); \ |
| 831 | __cu_from_r = (from); \ |
| 832 | __cu_len_r = (n); \ |
| 833 | __asm__ __volatile__( \ |
| 834 | ".set\tnoreorder\n\t" \ |
| 835 | __MODULE_JAL(__copy_user_inatomic) \ |
| 836 | ".set\tnoat\n\t" \ |
| 837 | __UA_ADDU "\t$1, %1, %2\n\t" \ |
| 838 | ".set\tat\n\t" \ |
| 839 | ".set\treorder" \ |
| 840 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 841 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 842 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 843 | DADDI_SCRATCH, "memory"); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 844 | __cu_len_r; \ |
| 845 | }) |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | /* |
Chris Dearman | 131c1a2 | 2007-02-01 19:54:13 +0000 | [diff] [blame] | 848 | * __copy_from_user: - Copy a block of data from user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 849 | * @to: Destination address, in kernel space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | * @from: Source address, in user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 851 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 853 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | * |
| 855 | * Copy data from user space to kernel space. Caller must check |
| 856 | * the specified block with access_ok() before calling this function. |
| 857 | * |
| 858 | * Returns number of bytes that could not be copied. |
| 859 | * On success, this will be zero. |
| 860 | * |
| 861 | * If some data could not be copied, this function will pad the copied |
| 862 | * data to the requested size using zero bytes. |
| 863 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 864 | #define __copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | ({ \ |
| 866 | void *__cu_to; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 867 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | long __cu_len; \ |
| 869 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | __cu_to = (to); \ |
| 871 | __cu_from = (from); \ |
| 872 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 873 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 875 | __cu_len); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | __cu_len; \ |
| 877 | }) |
| 878 | |
| 879 | /* |
| 880 | * copy_from_user: - Copy a block of data from user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 881 | * @to: Destination address, in kernel space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | * @from: Source address, in user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 883 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 885 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | * |
| 887 | * Copy data from user space to kernel space. |
| 888 | * |
| 889 | * Returns number of bytes that could not be copied. |
| 890 | * On success, this will be zero. |
| 891 | * |
| 892 | * If some data could not be copied, this function will pad the copied |
| 893 | * data to the requested size using zero bytes. |
| 894 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 895 | #define copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | ({ \ |
| 897 | void *__cu_to; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 898 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | long __cu_len; \ |
| 900 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | __cu_to = (to); \ |
| 902 | __cu_from = (from); \ |
| 903 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 904 | if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \ |
| 905 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 907 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 908 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | __cu_len; \ |
| 910 | }) |
| 911 | |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 912 | #define __copy_in_user(to, from, n) \ |
| 913 | ({ \ |
| 914 | void __user *__cu_to; \ |
| 915 | const void __user *__cu_from; \ |
| 916 | long __cu_len; \ |
| 917 | \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 918 | __cu_to = (to); \ |
| 919 | __cu_from = (from); \ |
| 920 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 921 | might_fault(); \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 922 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 923 | __cu_len); \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 924 | __cu_len; \ |
| 925 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 927 | #define copy_in_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 929 | void __user *__cu_to; \ |
| 930 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | long __cu_len; \ |
| 932 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | __cu_to = (to); \ |
| 934 | __cu_from = (from); \ |
| 935 | __cu_len = (n); \ |
| 936 | if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 937 | access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 938 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 940 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 941 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | __cu_len; \ |
| 943 | }) |
| 944 | |
| 945 | /* |
| 946 | * __clear_user: - Zero a block of memory in user space, with less checking. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 947 | * @to: Destination address, in user space. |
| 948 | * @n: Number of bytes to zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | * |
| 950 | * Zero a block of memory in user space. Caller must check |
| 951 | * the specified block with access_ok() before calling this function. |
| 952 | * |
| 953 | * Returns number of bytes that could not be cleared. |
| 954 | * On success, this will be zero. |
| 955 | */ |
| 956 | static inline __kernel_size_t |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 957 | __clear_user(void __user *addr, __kernel_size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
| 959 | __kernel_size_t res; |
| 960 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 961 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | __asm__ __volatile__( |
| 963 | "move\t$4, %1\n\t" |
| 964 | "move\t$5, $0\n\t" |
| 965 | "move\t$6, %2\n\t" |
| 966 | __MODULE_JAL(__bzero) |
| 967 | "move\t%0, $6" |
| 968 | : "=r" (res) |
| 969 | : "r" (addr), "r" (size) |
| 970 | : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31"); |
| 971 | |
| 972 | return res; |
| 973 | } |
| 974 | |
| 975 | #define clear_user(addr,n) \ |
| 976 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 977 | void __user * __cl_addr = (addr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | unsigned long __cl_size = (n); \ |
| 979 | if (__cl_size && access_ok(VERIFY_WRITE, \ |
Wu Zhangjin | 63d3892 | 2009-05-21 05:50:01 +0800 | [diff] [blame] | 980 | __cl_addr, __cl_size)) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | __cl_size = __clear_user(__cl_addr, __cl_size); \ |
| 982 | __cl_size; \ |
| 983 | }) |
| 984 | |
| 985 | /* |
| 986 | * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking. |
| 987 | * @dst: Destination address, in kernel space. This buffer must be at |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 988 | * least @count bytes long. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | * @src: Source address, in user space. |
| 990 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 991 | * |
| 992 | * Copies a NUL-terminated string from userspace to kernel space. |
| 993 | * Caller must check the specified block with access_ok() before calling |
| 994 | * this function. |
| 995 | * |
| 996 | * On success, returns the length of the string (not including the trailing |
| 997 | * NUL). |
| 998 | * |
| 999 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 1000 | * copied). |
| 1001 | * |
| 1002 | * If @count is smaller than the length of the string, copies @count bytes |
| 1003 | * and returns @count. |
| 1004 | */ |
| 1005 | static inline long |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1006 | __strncpy_from_user(char *__to, const char __user *__from, long __len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | { |
| 1008 | long res; |
| 1009 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1010 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | __asm__ __volatile__( |
| 1012 | "move\t$4, %1\n\t" |
| 1013 | "move\t$5, %2\n\t" |
| 1014 | "move\t$6, %3\n\t" |
| 1015 | __MODULE_JAL(__strncpy_from_user_nocheck_asm) |
| 1016 | "move\t%0, $2" |
| 1017 | : "=r" (res) |
| 1018 | : "r" (__to), "r" (__from), "r" (__len) |
| 1019 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); |
| 1020 | |
| 1021 | return res; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * strncpy_from_user: - Copy a NUL terminated string from userspace. |
| 1026 | * @dst: Destination address, in kernel space. This buffer must be at |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1027 | * least @count bytes long. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * @src: Source address, in user space. |
| 1029 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 1030 | * |
| 1031 | * Copies a NUL-terminated string from userspace to kernel space. |
| 1032 | * |
| 1033 | * On success, returns the length of the string (not including the trailing |
| 1034 | * NUL). |
| 1035 | * |
| 1036 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 1037 | * copied). |
| 1038 | * |
| 1039 | * If @count is smaller than the length of the string, copies @count bytes |
| 1040 | * and returns @count. |
| 1041 | */ |
| 1042 | static inline long |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1043 | strncpy_from_user(char *__to, const char __user *__from, long __len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
| 1045 | long res; |
| 1046 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1047 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | __asm__ __volatile__( |
| 1049 | "move\t$4, %1\n\t" |
| 1050 | "move\t$5, %2\n\t" |
| 1051 | "move\t$6, %3\n\t" |
| 1052 | __MODULE_JAL(__strncpy_from_user_asm) |
| 1053 | "move\t%0, $2" |
| 1054 | : "=r" (res) |
| 1055 | : "r" (__to), "r" (__from), "r" (__len) |
| 1056 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); |
| 1057 | |
| 1058 | return res; |
| 1059 | } |
| 1060 | |
| 1061 | /* Returns: 0 if bad, string length+1 (memory size) of string if ok */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1062 | static inline long __strlen_user(const char __user *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
| 1064 | long res; |
| 1065 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1066 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | __asm__ __volatile__( |
| 1068 | "move\t$4, %1\n\t" |
| 1069 | __MODULE_JAL(__strlen_user_nocheck_asm) |
| 1070 | "move\t%0, $2" |
| 1071 | : "=r" (res) |
| 1072 | : "r" (s) |
| 1073 | : "$2", "$4", __UA_t0, "$31"); |
| 1074 | |
| 1075 | return res; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * strlen_user: - Get the size of a string in user space. |
| 1080 | * @str: The string to measure. |
| 1081 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1082 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | * |
| 1084 | * Get the size of a NUL-terminated string in user space. |
| 1085 | * |
| 1086 | * Returns the size of the string INCLUDING the terminating NUL. |
| 1087 | * On exception, returns 0. |
| 1088 | * |
| 1089 | * If there is a limit on the length of a valid string, you may wish to |
| 1090 | * consider using strnlen_user() instead. |
| 1091 | */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1092 | static inline long strlen_user(const char __user *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
| 1094 | long res; |
| 1095 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1096 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | __asm__ __volatile__( |
| 1098 | "move\t$4, %1\n\t" |
| 1099 | __MODULE_JAL(__strlen_user_asm) |
| 1100 | "move\t%0, $2" |
| 1101 | : "=r" (res) |
| 1102 | : "r" (s) |
| 1103 | : "$2", "$4", __UA_t0, "$31"); |
| 1104 | |
| 1105 | return res; |
| 1106 | } |
| 1107 | |
| 1108 | /* Returns: 0 if bad, string length+1 (memory size) of string if ok */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1109 | static inline long __strnlen_user(const char __user *s, long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { |
| 1111 | long res; |
| 1112 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1113 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | __asm__ __volatile__( |
| 1115 | "move\t$4, %1\n\t" |
| 1116 | "move\t$5, %2\n\t" |
| 1117 | __MODULE_JAL(__strnlen_user_nocheck_asm) |
| 1118 | "move\t%0, $2" |
| 1119 | : "=r" (res) |
| 1120 | : "r" (s), "r" (n) |
| 1121 | : "$2", "$4", "$5", __UA_t0, "$31"); |
| 1122 | |
| 1123 | return res; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * strlen_user: - Get the size of a string in user space. |
| 1128 | * @str: The string to measure. |
| 1129 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1130 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | * |
| 1132 | * Get the size of a NUL-terminated string in user space. |
| 1133 | * |
| 1134 | * Returns the size of the string INCLUDING the terminating NUL. |
| 1135 | * On exception, returns 0. |
| 1136 | * |
| 1137 | * If there is a limit on the length of a valid string, you may wish to |
| 1138 | * consider using strnlen_user() instead. |
| 1139 | */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1140 | static inline long strnlen_user(const char __user *s, long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | { |
| 1142 | long res; |
| 1143 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1144 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | __asm__ __volatile__( |
| 1146 | "move\t$4, %1\n\t" |
| 1147 | "move\t$5, %2\n\t" |
| 1148 | __MODULE_JAL(__strnlen_user_asm) |
| 1149 | "move\t%0, $2" |
| 1150 | : "=r" (res) |
| 1151 | : "r" (s), "r" (n) |
| 1152 | : "$2", "$4", "$5", __UA_t0, "$31"); |
| 1153 | |
| 1154 | return res; |
| 1155 | } |
| 1156 | |
| 1157 | struct exception_table_entry |
| 1158 | { |
| 1159 | unsigned long insn; |
| 1160 | unsigned long nextinsn; |
| 1161 | }; |
| 1162 | |
| 1163 | extern int fixup_exception(struct pt_regs *regs); |
| 1164 | |
| 1165 | #endif /* _ASM_UACCESS_H */ |