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" \ |
| 273 | " .section .fixup,\"ax\" \n" \ |
| 274 | "3: li %0, %4 \n" \ |
| 275 | " j 2b \n" \ |
| 276 | " .previous \n" \ |
| 277 | " .section __ex_table,\"a\" \n" \ |
| 278 | " "__UA_ADDR "\t1b, 3b \n" \ |
| 279 | " .previous \n" \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 280 | : "=r" (__gu_err), "=r" (__gu_tmp) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 281 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 282 | \ |
Atsushi Nemoto | 8ecbbca | 2006-02-14 15:57:50 +0900 | [diff] [blame] | 283 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Get a long long 64 using 32 bit registers. |
| 288 | */ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 289 | #define __get_user_asm_ll32(val, addr) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 290 | { \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 291 | union { \ |
| 292 | unsigned long long l; \ |
| 293 | __typeof__(*(addr)) t; \ |
| 294 | } __gu_tmp; \ |
Ralf Baechle | cd1fb9e | 2007-02-12 23:12:38 +0000 | [diff] [blame] | 295 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | __asm__ __volatile__( \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 297 | "1: lw %1, (%3) \n" \ |
| 298 | "2: lw %D1, 4(%3) \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | "3: .section .fixup,\"ax\" \n" \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 300 | "4: li %0, %4 \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | " move %1, $0 \n" \ |
| 302 | " move %D1, $0 \n" \ |
| 303 | " j 3b \n" \ |
| 304 | " .previous \n" \ |
| 305 | " .section __ex_table,\"a\" \n" \ |
| 306 | " " __UA_ADDR " 1b, 4b \n" \ |
| 307 | " " __UA_ADDR " 2b, 4b \n" \ |
| 308 | " .previous \n" \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 309 | : "=r" (__gu_err), "=&r" (__gu_tmp.l) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 310 | : "0" (0), "r" (addr), "i" (-EFAULT)); \ |
Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 311 | \ |
| 312 | (val) = __gu_tmp.t; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Yuck. We need two variants, one for 64bit operation and one |
| 317 | * for 32 bit mode and old iron. |
| 318 | */ |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 319 | #ifdef CONFIG_32BIT |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 320 | #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | #endif |
Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 322 | #ifdef CONFIG_64BIT |
| 323 | #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr) |
| 324 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 326 | #define __put_user_nocheck(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | ({ \ |
| 328 | __typeof__(*(ptr)) __pu_val; \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 329 | int __pu_err = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 331 | __chk_user_ptr(ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | __pu_val = (x); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | switch (size) { \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 334 | case 1: __put_user_asm("sb", ptr); break; \ |
| 335 | case 2: __put_user_asm("sh", ptr); break; \ |
| 336 | case 4: __put_user_asm("sw", ptr); break; \ |
| 337 | case 8: __PUT_USER_DW(ptr); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | default: __put_user_unknown(); break; \ |
| 339 | } \ |
| 340 | __pu_err; \ |
| 341 | }) |
| 342 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 343 | #define __put_user_check(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 345 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 346 | __typeof__(*(ptr)) __pu_val = (x); \ |
Ralf Baechle | 8d2d91e | 2008-10-11 16:18:50 +0100 | [diff] [blame] | 347 | int __pu_err = -EFAULT; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 349 | might_fault(); \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 350 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | switch (size) { \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 352 | case 1: __put_user_asm("sb", __pu_addr); break; \ |
| 353 | case 2: __put_user_asm("sh", __pu_addr); break; \ |
| 354 | case 4: __put_user_asm("sw", __pu_addr); break; \ |
| 355 | case 8: __PUT_USER_DW(__pu_addr); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | default: __put_user_unknown(); break; \ |
| 357 | } \ |
| 358 | } \ |
| 359 | __pu_err; \ |
| 360 | }) |
| 361 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 362 | #define __put_user_asm(insn, ptr) \ |
| 363 | { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | __asm__ __volatile__( \ |
| 365 | "1: " insn " %z2, %3 # __put_user_asm\n" \ |
| 366 | "2: \n" \ |
| 367 | " .section .fixup,\"ax\" \n" \ |
| 368 | "3: li %0, %4 \n" \ |
| 369 | " j 2b \n" \ |
| 370 | " .previous \n" \ |
| 371 | " .section __ex_table,\"a\" \n" \ |
| 372 | " " __UA_ADDR " 1b, 3b \n" \ |
| 373 | " .previous \n" \ |
| 374 | : "=r" (__pu_err) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 375 | : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | "i" (-EFAULT)); \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 379 | #define __put_user_asm_ll32(ptr) \ |
| 380 | { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | __asm__ __volatile__( \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 382 | "1: sw %2, (%3) # __put_user_asm_ll32 \n" \ |
| 383 | "2: sw %D2, 4(%3) \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | "3: \n" \ |
| 385 | " .section .fixup,\"ax\" \n" \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 386 | "4: li %0, %4 \n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | " j 3b \n" \ |
| 388 | " .previous \n" \ |
| 389 | " .section __ex_table,\"a\" \n" \ |
| 390 | " " __UA_ADDR " 1b, 4b \n" \ |
| 391 | " " __UA_ADDR " 2b, 4b \n" \ |
| 392 | " .previous" \ |
| 393 | : "=r" (__pu_err) \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 394 | : "0" (0), "r" (__pu_val), "r" (ptr), \ |
| 395 | "i" (-EFAULT)); \ |
| 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | extern void __put_user_unknown(void); |
| 399 | |
| 400 | /* |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 401 | * put_user_unaligned: - Write a simple value into user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 402 | * @x: Value to copy to user space. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 403 | * @ptr: Destination address, in user space. |
| 404 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 405 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 406 | * |
| 407 | * This macro copies a single simple value from kernel space to user |
| 408 | * space. It supports simple types like char and int, but not larger |
| 409 | * data types like structures or arrays. |
| 410 | * |
| 411 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 412 | * to the result of dereferencing @ptr. |
| 413 | * |
| 414 | * Returns zero on success, or -EFAULT on error. |
| 415 | */ |
| 416 | #define put_user_unaligned(x,ptr) \ |
| 417 | __put_user_unaligned_check((x),(ptr),sizeof(*(ptr))) |
| 418 | |
| 419 | /* |
| 420 | * get_user_unaligned: - Get a simple variable from user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 421 | * @x: Variable to store result. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 422 | * @ptr: Source address, in user space. |
| 423 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 424 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 425 | * |
| 426 | * This macro copies a single simple variable from user space to kernel |
| 427 | * space. It supports simple types like char and int, but not larger |
| 428 | * data types like structures or arrays. |
| 429 | * |
| 430 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 431 | * dereferencing @ptr must be assignable to @x without a cast. |
| 432 | * |
| 433 | * Returns zero on success, or -EFAULT on error. |
| 434 | * On error, the variable @x is set to zero. |
| 435 | */ |
| 436 | #define get_user_unaligned(x,ptr) \ |
| 437 | __get_user_unaligned_check((x),(ptr),sizeof(*(ptr))) |
| 438 | |
| 439 | /* |
| 440 | * __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] | 441 | * @x: Value to copy to user space. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 442 | * @ptr: Destination address, in user space. |
| 443 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 444 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 445 | * |
| 446 | * This macro copies a single simple value from kernel space to user |
| 447 | * space. It supports simple types like char and int, but not larger |
| 448 | * data types like structures or arrays. |
| 449 | * |
| 450 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 451 | * to the result of dereferencing @ptr. |
| 452 | * |
| 453 | * Caller must check the pointer with access_ok() before calling this |
| 454 | * function. |
| 455 | * |
| 456 | * Returns zero on success, or -EFAULT on error. |
| 457 | */ |
| 458 | #define __put_user_unaligned(x,ptr) \ |
| 459 | __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr))) |
| 460 | |
| 461 | /* |
| 462 | * __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] | 463 | * @x: Variable to store result. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 464 | * @ptr: Source address, in user space. |
| 465 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 466 | * Context: User context only. This function may sleep. |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 467 | * |
| 468 | * This macro copies a single simple variable from user space to kernel |
| 469 | * space. It supports simple types like char and int, but not larger |
| 470 | * data types like structures or arrays. |
| 471 | * |
| 472 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 473 | * dereferencing @ptr must be assignable to @x without a cast. |
| 474 | * |
| 475 | * Caller must check the pointer with access_ok() before calling this |
| 476 | * function. |
| 477 | * |
| 478 | * Returns zero on success, or -EFAULT on error. |
| 479 | * On error, the variable @x is set to zero. |
| 480 | */ |
| 481 | #define __get_user_unaligned(x,ptr) \ |
| 482 | __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr))) |
| 483 | |
| 484 | /* |
| 485 | * Yuck. We need two variants, one for 64bit operation and one |
| 486 | * for 32 bit mode and old iron. |
| 487 | */ |
| 488 | #ifdef CONFIG_32BIT |
| 489 | #define __GET_USER_UNALIGNED_DW(val, ptr) \ |
| 490 | __get_user_unaligned_asm_ll32(val, ptr) |
| 491 | #endif |
| 492 | #ifdef CONFIG_64BIT |
| 493 | #define __GET_USER_UNALIGNED_DW(val, ptr) \ |
| 494 | __get_user_unaligned_asm(val, "uld", ptr) |
| 495 | #endif |
| 496 | |
| 497 | extern void __get_user_unaligned_unknown(void); |
| 498 | |
| 499 | #define __get_user_unaligned_common(val, size, ptr) \ |
| 500 | do { \ |
| 501 | switch (size) { \ |
| 502 | case 1: __get_user_asm(val, "lb", ptr); break; \ |
| 503 | case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \ |
| 504 | case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \ |
| 505 | case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \ |
| 506 | default: __get_user_unaligned_unknown(); break; \ |
| 507 | } \ |
| 508 | } while (0) |
| 509 | |
| 510 | #define __get_user_unaligned_nocheck(x,ptr,size) \ |
| 511 | ({ \ |
| 512 | int __gu_err; \ |
| 513 | \ |
| 514 | __get_user_unaligned_common((x), size, ptr); \ |
| 515 | __gu_err; \ |
| 516 | }) |
| 517 | |
| 518 | #define __get_user_unaligned_check(x,ptr,size) \ |
| 519 | ({ \ |
| 520 | int __gu_err = -EFAULT; \ |
| 521 | const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \ |
| 522 | \ |
| 523 | if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \ |
| 524 | __get_user_unaligned_common((x), size, __gu_ptr); \ |
| 525 | \ |
| 526 | __gu_err; \ |
| 527 | }) |
| 528 | |
| 529 | #define __get_user_unaligned_asm(val, insn, addr) \ |
| 530 | { \ |
| 531 | long __gu_tmp; \ |
| 532 | \ |
| 533 | __asm__ __volatile__( \ |
| 534 | "1: " insn " %1, %3 \n" \ |
| 535 | "2: \n" \ |
| 536 | " .section .fixup,\"ax\" \n" \ |
| 537 | "3: li %0, %4 \n" \ |
| 538 | " j 2b \n" \ |
| 539 | " .previous \n" \ |
| 540 | " .section __ex_table,\"a\" \n" \ |
| 541 | " "__UA_ADDR "\t1b, 3b \n" \ |
| 542 | " "__UA_ADDR "\t1b + 4, 3b \n" \ |
| 543 | " .previous \n" \ |
| 544 | : "=r" (__gu_err), "=r" (__gu_tmp) \ |
| 545 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \ |
| 546 | \ |
| 547 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Get a long long 64 using 32 bit registers. |
| 552 | */ |
| 553 | #define __get_user_unaligned_asm_ll32(val, addr) \ |
| 554 | { \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 555 | unsigned long long __gu_tmp; \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 556 | \ |
| 557 | __asm__ __volatile__( \ |
| 558 | "1: ulw %1, (%3) \n" \ |
| 559 | "2: ulw %D1, 4(%3) \n" \ |
| 560 | " move %0, $0 \n" \ |
| 561 | "3: .section .fixup,\"ax\" \n" \ |
| 562 | "4: li %0, %4 \n" \ |
| 563 | " move %1, $0 \n" \ |
| 564 | " move %D1, $0 \n" \ |
| 565 | " j 3b \n" \ |
| 566 | " .previous \n" \ |
| 567 | " .section __ex_table,\"a\" \n" \ |
| 568 | " " __UA_ADDR " 1b, 4b \n" \ |
| 569 | " " __UA_ADDR " 1b + 4, 4b \n" \ |
| 570 | " " __UA_ADDR " 2b, 4b \n" \ |
| 571 | " " __UA_ADDR " 2b + 4, 4b \n" \ |
| 572 | " .previous \n" \ |
| 573 | : "=r" (__gu_err), "=&r" (__gu_tmp) \ |
| 574 | : "0" (0), "r" (addr), "i" (-EFAULT)); \ |
| 575 | (val) = (__typeof__(*(addr))) __gu_tmp; \ |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Yuck. We need two variants, one for 64bit operation and one |
| 580 | * for 32 bit mode and old iron. |
| 581 | */ |
| 582 | #ifdef CONFIG_32BIT |
| 583 | #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr) |
| 584 | #endif |
| 585 | #ifdef CONFIG_64BIT |
| 586 | #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr) |
| 587 | #endif |
| 588 | |
| 589 | #define __put_user_unaligned_nocheck(x,ptr,size) \ |
| 590 | ({ \ |
| 591 | __typeof__(*(ptr)) __pu_val; \ |
| 592 | int __pu_err = 0; \ |
| 593 | \ |
| 594 | __pu_val = (x); \ |
| 595 | switch (size) { \ |
| 596 | case 1: __put_user_asm("sb", ptr); break; \ |
| 597 | case 2: __put_user_unaligned_asm("ush", ptr); break; \ |
| 598 | case 4: __put_user_unaligned_asm("usw", ptr); break; \ |
| 599 | case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \ |
| 600 | default: __put_user_unaligned_unknown(); break; \ |
| 601 | } \ |
| 602 | __pu_err; \ |
| 603 | }) |
| 604 | |
| 605 | #define __put_user_unaligned_check(x,ptr,size) \ |
| 606 | ({ \ |
| 607 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 608 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 609 | int __pu_err = -EFAULT; \ |
| 610 | \ |
| 611 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \ |
| 612 | switch (size) { \ |
| 613 | case 1: __put_user_asm("sb", __pu_addr); break; \ |
| 614 | case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \ |
| 615 | case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \ |
| 616 | case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \ |
| 617 | default: __put_user_unaligned_unknown(); break; \ |
| 618 | } \ |
| 619 | } \ |
| 620 | __pu_err; \ |
| 621 | }) |
| 622 | |
| 623 | #define __put_user_unaligned_asm(insn, ptr) \ |
| 624 | { \ |
| 625 | __asm__ __volatile__( \ |
| 626 | "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \ |
| 627 | "2: \n" \ |
| 628 | " .section .fixup,\"ax\" \n" \ |
| 629 | "3: li %0, %4 \n" \ |
| 630 | " j 2b \n" \ |
| 631 | " .previous \n" \ |
| 632 | " .section __ex_table,\"a\" \n" \ |
| 633 | " " __UA_ADDR " 1b, 3b \n" \ |
| 634 | " .previous \n" \ |
| 635 | : "=r" (__pu_err) \ |
| 636 | : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \ |
| 637 | "i" (-EFAULT)); \ |
| 638 | } |
| 639 | |
| 640 | #define __put_user_unaligned_asm_ll32(ptr) \ |
| 641 | { \ |
| 642 | __asm__ __volatile__( \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 643 | "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \ |
Ralf Baechle | 71ec6cc | 2006-10-31 02:52:56 +0000 | [diff] [blame] | 644 | "2: sw %D2, 4(%3) \n" \ |
| 645 | "3: \n" \ |
| 646 | " .section .fixup,\"ax\" \n" \ |
| 647 | "4: li %0, %4 \n" \ |
| 648 | " j 3b \n" \ |
| 649 | " .previous \n" \ |
| 650 | " .section __ex_table,\"a\" \n" \ |
| 651 | " " __UA_ADDR " 1b, 4b \n" \ |
| 652 | " " __UA_ADDR " 1b + 4, 4b \n" \ |
| 653 | " " __UA_ADDR " 2b, 4b \n" \ |
| 654 | " " __UA_ADDR " 2b + 4, 4b \n" \ |
| 655 | " .previous" \ |
| 656 | : "=r" (__pu_err) \ |
| 657 | : "0" (0), "r" (__pu_val), "r" (ptr), \ |
| 658 | "i" (-EFAULT)); \ |
| 659 | } |
| 660 | |
| 661 | extern void __put_user_unaligned_unknown(void); |
| 662 | |
| 663 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | * We're generating jump to subroutines which will be outside the range of |
| 665 | * jump instructions |
| 666 | */ |
| 667 | #ifdef MODULE |
| 668 | #define __MODULE_JAL(destination) \ |
| 669 | ".set\tnoat\n\t" \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 670 | __UA_LA "\t$1, " #destination "\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | "jalr\t$1\n\t" \ |
| 672 | ".set\tat\n\t" |
| 673 | #else |
| 674 | #define __MODULE_JAL(destination) \ |
| 675 | "jal\t" #destination "\n\t" |
| 676 | #endif |
| 677 | |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 678 | #ifndef CONFIG_CPU_DADDI_WORKAROUNDS |
| 679 | #define DADDI_SCRATCH "$0" |
| 680 | #else |
| 681 | #define DADDI_SCRATCH "$3" |
| 682 | #endif |
| 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | extern size_t __copy_user(void *__to, const void *__from, size_t __n); |
| 685 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 686 | #define __invoke_copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 688 | register void __user *__cu_to_r __asm__("$4"); \ |
| 689 | register const void *__cu_from_r __asm__("$5"); \ |
| 690 | register long __cu_len_r __asm__("$6"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | \ |
| 692 | __cu_to_r = (to); \ |
| 693 | __cu_from_r = (from); \ |
| 694 | __cu_len_r = (n); \ |
| 695 | __asm__ __volatile__( \ |
| 696 | __MODULE_JAL(__copy_user) \ |
| 697 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 698 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 699 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 700 | DADDI_SCRATCH, "memory"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | __cu_len_r; \ |
| 702 | }) |
| 703 | |
| 704 | /* |
| 705 | * __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] | 706 | * @to: Destination address, in user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | * @from: Source address, in kernel space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 708 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 710 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | * |
| 712 | * Copy data from kernel space to user space. Caller must check |
| 713 | * the specified block with access_ok() before calling this function. |
| 714 | * |
| 715 | * Returns number of bytes that could not be copied. |
| 716 | * On success, this will be zero. |
| 717 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 718 | #define __copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 720 | void __user *__cu_to; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | const void *__cu_from; \ |
| 722 | long __cu_len; \ |
| 723 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | __cu_to = (to); \ |
| 725 | __cu_from = (from); \ |
| 726 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 727 | might_fault(); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 728 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | __cu_len; \ |
| 730 | }) |
| 731 | |
Ralf Baechle | d0c91ae | 2007-03-05 15:54:20 +0000 | [diff] [blame] | 732 | extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n); |
| 733 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 734 | #define __copy_to_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 735 | ({ \ |
| 736 | void __user *__cu_to; \ |
| 737 | const void *__cu_from; \ |
| 738 | long __cu_len; \ |
| 739 | \ |
| 740 | __cu_to = (to); \ |
| 741 | __cu_from = (from); \ |
| 742 | __cu_len = (n); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 743 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 744 | __cu_len; \ |
| 745 | }) |
| 746 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 747 | #define __copy_from_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 748 | ({ \ |
| 749 | void *__cu_to; \ |
| 750 | const void __user *__cu_from; \ |
| 751 | long __cu_len; \ |
| 752 | \ |
| 753 | __cu_to = (to); \ |
| 754 | __cu_from = (from); \ |
| 755 | __cu_len = (n); \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 756 | __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \ |
| 757 | __cu_len); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 758 | __cu_len; \ |
| 759 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * copy_to_user: - Copy a block of data into user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 763 | * @to: Destination address, in user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * @from: Source address, in kernel space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 765 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 767 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | * |
| 769 | * Copy data from kernel space to user space. |
| 770 | * |
| 771 | * Returns number of bytes that could not be copied. |
| 772 | * On success, this will be zero. |
| 773 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 774 | #define copy_to_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 776 | void __user *__cu_to; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | const void *__cu_from; \ |
| 778 | long __cu_len; \ |
| 779 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | __cu_to = (to); \ |
| 781 | __cu_from = (from); \ |
| 782 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 783 | if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \ |
| 784 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 786 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 787 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | __cu_len; \ |
| 789 | }) |
| 790 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 791 | #define __invoke_copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 793 | register void *__cu_to_r __asm__("$4"); \ |
| 794 | register const void __user *__cu_from_r __asm__("$5"); \ |
| 795 | register long __cu_len_r __asm__("$6"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | \ |
| 797 | __cu_to_r = (to); \ |
| 798 | __cu_from_r = (from); \ |
| 799 | __cu_len_r = (n); \ |
| 800 | __asm__ __volatile__( \ |
| 801 | ".set\tnoreorder\n\t" \ |
| 802 | __MODULE_JAL(__copy_user) \ |
| 803 | ".set\tnoat\n\t" \ |
| 804 | __UA_ADDU "\t$1, %1, %2\n\t" \ |
| 805 | ".set\tat\n\t" \ |
| 806 | ".set\treorder" \ |
| 807 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 808 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 809 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 810 | DADDI_SCRATCH, "memory"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | __cu_len_r; \ |
| 812 | }) |
| 813 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 814 | #define __invoke_copy_from_user_inatomic(to, from, n) \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 815 | ({ \ |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 816 | register void *__cu_to_r __asm__("$4"); \ |
| 817 | register const void __user *__cu_from_r __asm__("$5"); \ |
| 818 | register long __cu_len_r __asm__("$6"); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 819 | \ |
| 820 | __cu_to_r = (to); \ |
| 821 | __cu_from_r = (from); \ |
| 822 | __cu_len_r = (n); \ |
| 823 | __asm__ __volatile__( \ |
| 824 | ".set\tnoreorder\n\t" \ |
| 825 | __MODULE_JAL(__copy_user_inatomic) \ |
| 826 | ".set\tnoat\n\t" \ |
| 827 | __UA_ADDU "\t$1, %1, %2\n\t" \ |
| 828 | ".set\tat\n\t" \ |
| 829 | ".set\treorder" \ |
| 830 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \ |
| 831 | : \ |
David Daney | bb0757e | 2012-06-06 23:00:31 +0100 | [diff] [blame] | 832 | : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \ |
Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 833 | DADDI_SCRATCH, "memory"); \ |
Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 834 | __cu_len_r; \ |
| 835 | }) |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* |
Chris Dearman | 131c1a2 | 2007-02-01 19:54:13 +0000 | [diff] [blame] | 838 | * __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] | 839 | * @to: Destination address, in kernel space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | * @from: Source address, in user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 841 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 843 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | * |
| 845 | * Copy data from user space to kernel space. Caller must check |
| 846 | * the specified block with access_ok() before calling this function. |
| 847 | * |
| 848 | * Returns number of bytes that could not be copied. |
| 849 | * On success, this will be zero. |
| 850 | * |
| 851 | * If some data could not be copied, this function will pad the copied |
| 852 | * data to the requested size using zero bytes. |
| 853 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 854 | #define __copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | ({ \ |
| 856 | void *__cu_to; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 857 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | long __cu_len; \ |
| 859 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | __cu_to = (to); \ |
| 861 | __cu_from = (from); \ |
| 862 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 863 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 865 | __cu_len); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | __cu_len; \ |
| 867 | }) |
| 868 | |
| 869 | /* |
| 870 | * copy_from_user: - Copy a block of data from user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 871 | * @to: Destination address, in kernel space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | * @from: Source address, in user space. |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 873 | * @n: Number of bytes to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 875 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | * |
| 877 | * Copy data from user space to kernel space. |
| 878 | * |
| 879 | * Returns number of bytes that could not be copied. |
| 880 | * On success, this will be zero. |
| 881 | * |
| 882 | * If some data could not be copied, this function will pad the copied |
| 883 | * data to the requested size using zero bytes. |
| 884 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 885 | #define copy_from_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | ({ \ |
| 887 | void *__cu_to; \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 888 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | long __cu_len; \ |
| 890 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | __cu_to = (to); \ |
| 892 | __cu_from = (from); \ |
| 893 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 894 | if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \ |
| 895 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 897 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 898 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | __cu_len; \ |
| 900 | }) |
| 901 | |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 902 | #define __copy_in_user(to, from, n) \ |
| 903 | ({ \ |
| 904 | void __user *__cu_to; \ |
| 905 | const void __user *__cu_from; \ |
| 906 | long __cu_len; \ |
| 907 | \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 908 | __cu_to = (to); \ |
| 909 | __cu_from = (from); \ |
| 910 | __cu_len = (n); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 911 | might_fault(); \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 912 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 913 | __cu_len); \ |
Ralf Baechle | ed01b3d | 2009-04-27 16:46:21 +0200 | [diff] [blame] | 914 | __cu_len; \ |
| 915 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 917 | #define copy_in_user(to, from, n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 919 | void __user *__cu_to; \ |
| 920 | const void __user *__cu_from; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | long __cu_len; \ |
| 922 | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | __cu_to = (to); \ |
| 924 | __cu_from = (from); \ |
| 925 | __cu_len = (n); \ |
| 926 | if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 927 | access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 928 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 930 | __cu_len); \ |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 931 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | __cu_len; \ |
| 933 | }) |
| 934 | |
| 935 | /* |
| 936 | * __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] | 937 | * @to: Destination address, in user space. |
| 938 | * @n: Number of bytes to zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | * |
| 940 | * Zero a block of memory in user space. Caller must check |
| 941 | * the specified block with access_ok() before calling this function. |
| 942 | * |
| 943 | * Returns number of bytes that could not be cleared. |
| 944 | * On success, this will be zero. |
| 945 | */ |
| 946 | static inline __kernel_size_t |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 947 | __clear_user(void __user *addr, __kernel_size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
| 949 | __kernel_size_t res; |
| 950 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 951 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | __asm__ __volatile__( |
| 953 | "move\t$4, %1\n\t" |
| 954 | "move\t$5, $0\n\t" |
| 955 | "move\t$6, %2\n\t" |
| 956 | __MODULE_JAL(__bzero) |
| 957 | "move\t%0, $6" |
| 958 | : "=r" (res) |
| 959 | : "r" (addr), "r" (size) |
| 960 | : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31"); |
| 961 | |
| 962 | return res; |
| 963 | } |
| 964 | |
| 965 | #define clear_user(addr,n) \ |
| 966 | ({ \ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 967 | void __user * __cl_addr = (addr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | unsigned long __cl_size = (n); \ |
| 969 | if (__cl_size && access_ok(VERIFY_WRITE, \ |
Wu Zhangjin | 63d3892 | 2009-05-21 05:50:01 +0800 | [diff] [blame] | 970 | __cl_addr, __cl_size)) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | __cl_size = __clear_user(__cl_addr, __cl_size); \ |
| 972 | __cl_size; \ |
| 973 | }) |
| 974 | |
| 975 | /* |
| 976 | * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking. |
| 977 | * @dst: Destination address, in kernel space. This buffer must be at |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 978 | * least @count bytes long. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | * @src: Source address, in user space. |
| 980 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 981 | * |
| 982 | * Copies a NUL-terminated string from userspace to kernel space. |
| 983 | * Caller must check the specified block with access_ok() before calling |
| 984 | * this function. |
| 985 | * |
| 986 | * On success, returns the length of the string (not including the trailing |
| 987 | * NUL). |
| 988 | * |
| 989 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 990 | * copied). |
| 991 | * |
| 992 | * If @count is smaller than the length of the string, copies @count bytes |
| 993 | * and returns @count. |
| 994 | */ |
| 995 | static inline long |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 996 | __strncpy_from_user(char *__to, const char __user *__from, long __len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
| 998 | long res; |
| 999 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1000 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | __asm__ __volatile__( |
| 1002 | "move\t$4, %1\n\t" |
| 1003 | "move\t$5, %2\n\t" |
| 1004 | "move\t$6, %3\n\t" |
| 1005 | __MODULE_JAL(__strncpy_from_user_nocheck_asm) |
| 1006 | "move\t%0, $2" |
| 1007 | : "=r" (res) |
| 1008 | : "r" (__to), "r" (__from), "r" (__len) |
| 1009 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); |
| 1010 | |
| 1011 | return res; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * strncpy_from_user: - Copy a NUL terminated string from userspace. |
| 1016 | * @dst: Destination address, in kernel space. This buffer must be at |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1017 | * least @count bytes long. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | * @src: Source address, in user space. |
| 1019 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 1020 | * |
| 1021 | * Copies a NUL-terminated string from userspace to kernel space. |
| 1022 | * |
| 1023 | * On success, returns the length of the string (not including the trailing |
| 1024 | * NUL). |
| 1025 | * |
| 1026 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 1027 | * copied). |
| 1028 | * |
| 1029 | * If @count is smaller than the length of the string, copies @count bytes |
| 1030 | * and returns @count. |
| 1031 | */ |
| 1032 | static inline long |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1033 | strncpy_from_user(char *__to, const char __user *__from, long __len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
| 1035 | long res; |
| 1036 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1037 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | __asm__ __volatile__( |
| 1039 | "move\t$4, %1\n\t" |
| 1040 | "move\t$5, %2\n\t" |
| 1041 | "move\t$6, %3\n\t" |
| 1042 | __MODULE_JAL(__strncpy_from_user_asm) |
| 1043 | "move\t%0, $2" |
| 1044 | : "=r" (res) |
| 1045 | : "r" (__to), "r" (__from), "r" (__len) |
| 1046 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); |
| 1047 | |
| 1048 | return res; |
| 1049 | } |
| 1050 | |
| 1051 | /* 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] | 1052 | static inline long __strlen_user(const char __user *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
| 1054 | long res; |
| 1055 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1056 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | __asm__ __volatile__( |
| 1058 | "move\t$4, %1\n\t" |
| 1059 | __MODULE_JAL(__strlen_user_nocheck_asm) |
| 1060 | "move\t%0, $2" |
| 1061 | : "=r" (res) |
| 1062 | : "r" (s) |
| 1063 | : "$2", "$4", __UA_t0, "$31"); |
| 1064 | |
| 1065 | return res; |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * strlen_user: - Get the size of a string in user space. |
| 1070 | * @str: The string to measure. |
| 1071 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1072 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | * |
| 1074 | * Get the size of a NUL-terminated string in user space. |
| 1075 | * |
| 1076 | * Returns the size of the string INCLUDING the terminating NUL. |
| 1077 | * On exception, returns 0. |
| 1078 | * |
| 1079 | * If there is a limit on the length of a valid string, you may wish to |
| 1080 | * consider using strnlen_user() instead. |
| 1081 | */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1082 | static inline long strlen_user(const char __user *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | long res; |
| 1085 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1086 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | __asm__ __volatile__( |
| 1088 | "move\t$4, %1\n\t" |
| 1089 | __MODULE_JAL(__strlen_user_asm) |
| 1090 | "move\t%0, $2" |
| 1091 | : "=r" (res) |
| 1092 | : "r" (s) |
| 1093 | : "$2", "$4", __UA_t0, "$31"); |
| 1094 | |
| 1095 | return res; |
| 1096 | } |
| 1097 | |
| 1098 | /* 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] | 1099 | static inline long __strnlen_user(const char __user *s, long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
| 1101 | long res; |
| 1102 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1103 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | __asm__ __volatile__( |
| 1105 | "move\t$4, %1\n\t" |
| 1106 | "move\t$5, %2\n\t" |
| 1107 | __MODULE_JAL(__strnlen_user_nocheck_asm) |
| 1108 | "move\t%0, $2" |
| 1109 | : "=r" (res) |
| 1110 | : "r" (s), "r" (n) |
| 1111 | : "$2", "$4", "$5", __UA_t0, "$31"); |
| 1112 | |
| 1113 | return res; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * strlen_user: - Get the size of a string in user space. |
| 1118 | * @str: The string to measure. |
| 1119 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 1120 | * Context: User context only. This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | * |
| 1122 | * Get the size of a NUL-terminated string in user space. |
| 1123 | * |
| 1124 | * Returns the size of the string INCLUDING the terminating NUL. |
| 1125 | * On exception, returns 0. |
| 1126 | * |
| 1127 | * If there is a limit on the length of a valid string, you may wish to |
| 1128 | * consider using strnlen_user() instead. |
| 1129 | */ |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 1130 | static inline long strnlen_user(const char __user *s, long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
| 1132 | long res; |
| 1133 | |
Ralf Baechle | ef41f46 | 2009-04-28 14:17:54 +0200 | [diff] [blame] | 1134 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | __asm__ __volatile__( |
| 1136 | "move\t$4, %1\n\t" |
| 1137 | "move\t$5, %2\n\t" |
| 1138 | __MODULE_JAL(__strnlen_user_asm) |
| 1139 | "move\t%0, $2" |
| 1140 | : "=r" (res) |
| 1141 | : "r" (s), "r" (n) |
| 1142 | : "$2", "$4", "$5", __UA_t0, "$31"); |
| 1143 | |
| 1144 | return res; |
| 1145 | } |
| 1146 | |
| 1147 | struct exception_table_entry |
| 1148 | { |
| 1149 | unsigned long insn; |
| 1150 | unsigned long nextinsn; |
| 1151 | }; |
| 1152 | |
| 1153 | extern int fixup_exception(struct pt_regs *regs); |
| 1154 | |
| 1155 | #endif /* _ASM_UACCESS_H */ |