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