| 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> | 
|  | 16 | #include <asm-generic/uaccess.h> | 
|  | 17 |  | 
|  | 18 | /* | 
|  | 19 | * The fs value determines whether argument validity checking should be | 
|  | 20 | * performed or not.  If get_fs() == USER_DS, checking is performed, with | 
|  | 21 | * get_fs() == KERNEL_DS, checking is bypassed. | 
|  | 22 | * | 
|  | 23 | * For historical reasons, these macros are grossly misnamed. | 
|  | 24 | */ | 
| Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_32BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | #define __UA_LIMIT	0x80000000UL | 
|  | 28 |  | 
|  | 29 | #define __UA_ADDR	".word" | 
|  | 30 | #define __UA_LA		"la" | 
|  | 31 | #define __UA_ADDU	"addu" | 
|  | 32 | #define __UA_t0		"$8" | 
|  | 33 | #define __UA_t1		"$9" | 
|  | 34 |  | 
| Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 35 | #endif /* CONFIG_32BIT */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
| Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 37 | #ifdef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | #define __UA_LIMIT	(- TASK_SIZE) | 
|  | 40 |  | 
|  | 41 | #define __UA_ADDR	".dword" | 
|  | 42 | #define __UA_LA		"dla" | 
|  | 43 | #define __UA_ADDU	"daddu" | 
|  | 44 | #define __UA_t0		"$12" | 
|  | 45 | #define __UA_t1		"$13" | 
|  | 46 |  | 
| Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 47 | #endif /* CONFIG_64BIT */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | /* | 
|  | 50 | * USER_DS is a bitmask that has the bits set that may not be set in a valid | 
|  | 51 | * userspace address.  Note that we limit 32-bit userspace to 0x7fff8000 but | 
|  | 52 | * the arithmetic we're doing only works if the limit is a power of two, so | 
|  | 53 | * we use 0x80000000 here on 32-bit kernels.  If a process passes an invalid | 
|  | 54 | * address in this range it's the process's problem, not ours :-) | 
|  | 55 | */ | 
|  | 56 |  | 
|  | 57 | #define KERNEL_DS	((mm_segment_t) { 0UL }) | 
|  | 58 | #define USER_DS		((mm_segment_t) { __UA_LIMIT }) | 
|  | 59 |  | 
|  | 60 | #define VERIFY_READ    0 | 
|  | 61 | #define VERIFY_WRITE   1 | 
|  | 62 |  | 
|  | 63 | #define get_ds()	(KERNEL_DS) | 
|  | 64 | #define get_fs()	(current_thread_info()->addr_limit) | 
|  | 65 | #define set_fs(x)	(current_thread_info()->addr_limit = (x)) | 
|  | 66 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 67 | #define segment_eq(a, b)	((a).seg == (b).seg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 |  | 
|  | 69 |  | 
|  | 70 | /* | 
|  | 71 | * Is a address valid? This does a straighforward calculation rather | 
|  | 72 | * than tests. | 
|  | 73 | * | 
|  | 74 | * Address valid if: | 
|  | 75 | *  - "addr" doesn't have any high-bits set | 
|  | 76 | *  - AND "size" doesn't have any high-bits set | 
|  | 77 | *  - AND "addr+size" doesn't have any high-bits set | 
|  | 78 | *  - OR we are in kernel mode. | 
|  | 79 | * | 
|  | 80 | * __ua_size() is a trick to avoid runtime checking of positive constant | 
|  | 81 | * sizes; for those we already know at compile time that the size is ok. | 
|  | 82 | */ | 
|  | 83 | #define __ua_size(size)							\ | 
|  | 84 | ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size)) | 
|  | 85 |  | 
|  | 86 | /* | 
|  | 87 | * access_ok: - Checks if a user space pointer is valid | 
|  | 88 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that | 
|  | 89 | *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe | 
|  | 90 | *        to write to a block, it is always safe to read from it. | 
|  | 91 | * @addr: User space pointer to start of block to check | 
|  | 92 | * @size: Size of block to check | 
|  | 93 | * | 
|  | 94 | * Context: User context only.  This function may sleep. | 
|  | 95 | * | 
|  | 96 | * Checks if a pointer to a block of memory in user space is valid. | 
|  | 97 | * | 
|  | 98 | * Returns true (nonzero) if the memory block may be valid, false (zero) | 
|  | 99 | * if it is definitely invalid. | 
|  | 100 | * | 
|  | 101 | * Note that, depending on architecture, this function probably just | 
|  | 102 | * checks that the pointer is in the user space range - after calling | 
|  | 103 | * this function, memory access functions may still return -EFAULT. | 
|  | 104 | */ | 
|  | 105 |  | 
|  | 106 | #define __access_mask get_fs().seg | 
|  | 107 |  | 
|  | 108 | #define __access_ok(addr, size, mask)					\ | 
|  | 109 | (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0) | 
|  | 110 |  | 
|  | 111 | #define access_ok(type, addr, size)					\ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 112 | likely(__access_ok((unsigned long)(addr), (size), __access_mask)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * put_user: - Write a simple value into user space. | 
|  | 116 | * @x:   Value to copy to user space. | 
|  | 117 | * @ptr: Destination address, in user space. | 
|  | 118 | * | 
|  | 119 | * Context: User context only.  This function may sleep. | 
|  | 120 | * | 
|  | 121 | * This macro copies a single simple value from kernel space to user | 
|  | 122 | * space.  It supports simple types like char and int, but not larger | 
|  | 123 | * data types like structures or arrays. | 
|  | 124 | * | 
|  | 125 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable | 
|  | 126 | * to the result of dereferencing @ptr. | 
|  | 127 | * | 
|  | 128 | * Returns zero on success, or -EFAULT on error. | 
|  | 129 | */ | 
|  | 130 | #define put_user(x,ptr)	\ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 131 | __put_user_check((x), (ptr), sizeof(*(ptr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | /* | 
|  | 134 | * get_user: - Get a simple variable from user space. | 
|  | 135 | * @x:   Variable to store result. | 
|  | 136 | * @ptr: Source address, in user space. | 
|  | 137 | * | 
|  | 138 | * Context: User context only.  This function may sleep. | 
|  | 139 | * | 
|  | 140 | * This macro copies a single simple variable from user space to kernel | 
|  | 141 | * space.  It supports simple types like char and int, but not larger | 
|  | 142 | * data types like structures or arrays. | 
|  | 143 | * | 
|  | 144 | * @ptr must have pointer-to-simple-variable type, and the result of | 
|  | 145 | * dereferencing @ptr must be assignable to @x without a cast. | 
|  | 146 | * | 
|  | 147 | * Returns zero on success, or -EFAULT on error. | 
|  | 148 | * On error, the variable @x is set to zero. | 
|  | 149 | */ | 
|  | 150 | #define get_user(x,ptr) \ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 151 | __get_user_check((x), (ptr), sizeof(*(ptr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 |  | 
|  | 153 | /* | 
|  | 154 | * __put_user: - Write a simple value into user space, with less checking. | 
|  | 155 | * @x:   Value to copy to user space. | 
|  | 156 | * @ptr: Destination address, in user space. | 
|  | 157 | * | 
|  | 158 | * Context: User context only.  This function may sleep. | 
|  | 159 | * | 
|  | 160 | * This macro copies a single simple value from kernel space to user | 
|  | 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 @x must be assignable | 
|  | 165 | * to the result of dereferencing @ptr. | 
|  | 166 | * | 
|  | 167 | * Caller must check the pointer with access_ok() before calling this | 
|  | 168 | * function. | 
|  | 169 | * | 
|  | 170 | * Returns zero on success, or -EFAULT on error. | 
|  | 171 | */ | 
|  | 172 | #define __put_user(x,ptr) \ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 173 | __put_user_nocheck((x), (ptr), sizeof(*(ptr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | /* | 
|  | 176 | * __get_user: - Get a simple variable from user space, with less checking. | 
|  | 177 | * @x:   Variable to store result. | 
|  | 178 | * @ptr: Source address, in user space. | 
|  | 179 | * | 
|  | 180 | * Context: User context only.  This function may sleep. | 
|  | 181 | * | 
|  | 182 | * This macro copies a single simple variable from user space to kernel | 
|  | 183 | * space.  It supports simple types like char and int, but not larger | 
|  | 184 | * data types like structures or arrays. | 
|  | 185 | * | 
|  | 186 | * @ptr must have pointer-to-simple-variable type, and the result of | 
|  | 187 | * dereferencing @ptr must be assignable to @x without a cast. | 
|  | 188 | * | 
|  | 189 | * Caller must check the pointer with access_ok() before calling this | 
|  | 190 | * function. | 
|  | 191 | * | 
|  | 192 | * Returns zero on success, or -EFAULT on error. | 
|  | 193 | * On error, the variable @x is set to zero. | 
|  | 194 | */ | 
|  | 195 | #define __get_user(x,ptr) \ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 196 | __get_user_nocheck((x), (ptr), sizeof(*(ptr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 |  | 
|  | 198 | struct __large_struct { unsigned long buf[100]; }; | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 199 | #define __m(x) (*(struct __large_struct __user *)(x)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | /* | 
|  | 202 | * Yuck.  We need two variants, one for 64bit operation and one | 
|  | 203 | * for 32 bit mode and old iron. | 
|  | 204 | */ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_32BIT | 
|  | 206 | #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #endif | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 208 | #ifdef CONFIG_64BIT | 
|  | 209 | #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr) | 
|  | 210 | #endif | 
|  | 211 |  | 
|  | 212 | extern void __get_user_unknown(void); | 
|  | 213 |  | 
|  | 214 | #define __get_user_common(val, size, ptr)				\ | 
|  | 215 | do {									\ | 
|  | 216 | switch (size) {							\ | 
|  | 217 | case 1: __get_user_asm(val, "lb", ptr); break;			\ | 
|  | 218 | case 2: __get_user_asm(val, "lh", ptr); break;			\ | 
|  | 219 | case 4: __get_user_asm(val, "lw", ptr); break;			\ | 
|  | 220 | case 8: __GET_USER_DW(val, ptr); break;				\ | 
|  | 221 | default: __get_user_unknown(); break;				\ | 
|  | 222 | }								\ | 
|  | 223 | } while (0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 225 | #define __get_user_nocheck(x, ptr, size)				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | ({									\ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 227 | long __gu_err;							\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | \ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 229 | __get_user_common((x), size, ptr);				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | __gu_err;							\ | 
|  | 231 | }) | 
|  | 232 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 233 | #define __get_user_check(x, ptr, size)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 235 | long __gu_err = -EFAULT;					\ | 
| Atsushi Nemoto | 8ecbbca | 2006-02-14 15:57:50 +0900 | [diff] [blame] | 236 | const __typeof__(*(ptr)) __user * __gu_ptr = (ptr);		\ | 
| 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 | if (likely(access_ok(VERIFY_READ,  __gu_ptr, size)))		\ | 
|  | 239 | __get_user_common((x), size, __gu_ptr);			\ | 
|  | 240 | \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | __gu_err;							\ | 
|  | 242 | }) | 
|  | 243 |  | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 244 | #define __get_user_asm(val, insn, addr)					\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 245 | {									\ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 246 | long __gu_tmp;							\ | 
|  | 247 | \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | __asm__ __volatile__(						\ | 
|  | 249 | "1:	" insn "	%1, %3				\n"	\ | 
|  | 250 | "2:							\n"	\ | 
|  | 251 | "	.section .fixup,\"ax\"				\n"	\ | 
|  | 252 | "3:	li	%0, %4					\n"	\ | 
|  | 253 | "	j	2b					\n"	\ | 
|  | 254 | "	.previous					\n"	\ | 
|  | 255 | "	.section __ex_table,\"a\"			\n"	\ | 
|  | 256 | "	"__UA_ADDR "\t1b, 3b				\n"	\ | 
|  | 257 | "	.previous					\n"	\ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 258 | : "=r" (__gu_err), "=r" (__gu_tmp)				\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 259 | : "0" (0), "o" (__m(addr)), "i" (-EFAULT));			\ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 260 | \ | 
| Atsushi Nemoto | 8ecbbca | 2006-02-14 15:57:50 +0900 | [diff] [blame] | 261 | (val) = (__typeof__(*(addr))) __gu_tmp;				\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 262 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 |  | 
|  | 264 | /* | 
|  | 265 | * Get a long long 64 using 32 bit registers. | 
|  | 266 | */ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 267 | #define __get_user_asm_ll32(val, addr)					\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 268 | {									\ | 
| Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 269 | union {								\ | 
|  | 270 | unsigned long long	l;				\ | 
|  | 271 | __typeof__(*(addr))	t;				\ | 
|  | 272 | } __gu_tmp;							\ | 
| Ralf Baechle | cd1fb9e | 2007-02-12 23:12:38 +0000 | [diff] [blame] | 273 | \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | __asm__ __volatile__(						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 275 | "1:	lw	%1, (%3)				\n"	\ | 
|  | 276 | "2:	lw	%D1, 4(%3)				\n"	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | "3:	.section	.fixup,\"ax\"			\n"	\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 278 | "4:	li	%0, %4					\n"	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | "	move	%1, $0					\n"	\ | 
|  | 280 | "	move	%D1, $0					\n"	\ | 
|  | 281 | "	j	3b					\n"	\ | 
|  | 282 | "	.previous					\n"	\ | 
|  | 283 | "	.section	__ex_table,\"a\"		\n"	\ | 
|  | 284 | "	" __UA_ADDR "	1b, 4b				\n"	\ | 
|  | 285 | "	" __UA_ADDR "	2b, 4b				\n"	\ | 
|  | 286 | "	.previous					\n"	\ | 
| Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 287 | : "=r" (__gu_err), "=&r" (__gu_tmp.l)				\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 288 | : "0" (0), "r" (addr), "i" (-EFAULT));				\ | 
| Ralf Baechle | cb66fb3 | 2007-02-13 11:45:24 +0000 | [diff] [blame] | 289 | \ | 
|  | 290 | (val) = __gu_tmp.t;						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 291 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* | 
|  | 294 | * Yuck.  We need two variants, one for 64bit operation and one | 
|  | 295 | * for 32 bit mode and old iron. | 
|  | 296 | */ | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 297 | #ifdef CONFIG_32BIT | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 298 | #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | #endif | 
| Ralf Baechle | 4feb8f8 | 2006-01-23 16:15:30 +0000 | [diff] [blame] | 300 | #ifdef CONFIG_64BIT | 
|  | 301 | #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr) | 
|  | 302 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 304 | #define __put_user_nocheck(x, ptr, size)				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | ({									\ | 
|  | 306 | __typeof__(*(ptr)) __pu_val;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | long __pu_err = 0;						\ | 
|  | 308 | \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | __pu_val = (x);							\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | switch (size) {							\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 311 | case 1: __put_user_asm("sb", ptr); break;			\ | 
|  | 312 | case 2: __put_user_asm("sh", ptr); break;			\ | 
|  | 313 | case 4: __put_user_asm("sw", ptr); break;			\ | 
|  | 314 | case 8: __PUT_USER_DW(ptr); break;				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | default: __put_user_unknown(); break;				\ | 
|  | 316 | }								\ | 
|  | 317 | __pu_err;							\ | 
|  | 318 | }) | 
|  | 319 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 320 | #define __put_user_check(x, ptr, size)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 322 | __typeof__(*(ptr)) __user *__pu_addr = (ptr);			\ | 
|  | 323 | __typeof__(*(ptr)) __pu_val = (x);				\ | 
|  | 324 | long __pu_err = -EFAULT;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | \ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 326 | if (likely(access_ok(VERIFY_WRITE,  __pu_addr, size))) {	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | switch (size) {						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 328 | case 1: __put_user_asm("sb", __pu_addr); break;		\ | 
|  | 329 | case 2: __put_user_asm("sh", __pu_addr); break;		\ | 
|  | 330 | case 4: __put_user_asm("sw", __pu_addr); break;		\ | 
|  | 331 | case 8: __PUT_USER_DW(__pu_addr); break;		\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | default: __put_user_unknown(); break;			\ | 
|  | 333 | }							\ | 
|  | 334 | }								\ | 
|  | 335 | __pu_err;							\ | 
|  | 336 | }) | 
|  | 337 |  | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 338 | #define __put_user_asm(insn, ptr)					\ | 
|  | 339 | {									\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | __asm__ __volatile__(						\ | 
|  | 341 | "1:	" insn "	%z2, %3		# __put_user_asm\n"	\ | 
|  | 342 | "2:							\n"	\ | 
|  | 343 | "	.section	.fixup,\"ax\"			\n"	\ | 
|  | 344 | "3:	li	%0, %4					\n"	\ | 
|  | 345 | "	j	2b					\n"	\ | 
|  | 346 | "	.previous					\n"	\ | 
|  | 347 | "	.section	__ex_table,\"a\"		\n"	\ | 
|  | 348 | "	" __UA_ADDR "	1b, 3b				\n"	\ | 
|  | 349 | "	.previous					\n"	\ | 
|  | 350 | : "=r" (__pu_err)						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 351 | : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)),			\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | "i" (-EFAULT));						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 353 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 |  | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 355 | #define __put_user_asm_ll32(ptr)					\ | 
|  | 356 | {									\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | __asm__ __volatile__(						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 358 | "1:	sw	%2, (%3)	# __put_user_asm_ll32	\n"	\ | 
|  | 359 | "2:	sw	%D2, 4(%3)				\n"	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | "3:							\n"	\ | 
|  | 361 | "	.section	.fixup,\"ax\"			\n"	\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 362 | "4:	li	%0, %4					\n"	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | "	j	3b					\n"	\ | 
|  | 364 | "	.previous					\n"	\ | 
|  | 365 | "	.section	__ex_table,\"a\"		\n"	\ | 
|  | 366 | "	" __UA_ADDR "	1b, 4b				\n"	\ | 
|  | 367 | "	" __UA_ADDR "	2b, 4b				\n"	\ | 
|  | 368 | "	.previous"						\ | 
|  | 369 | : "=r" (__pu_err)						\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 370 | : "0" (0), "r" (__pu_val), "r" (ptr),				\ | 
|  | 371 | "i" (-EFAULT));						\ | 
|  | 372 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 |  | 
|  | 374 | extern void __put_user_unknown(void); | 
|  | 375 |  | 
|  | 376 | /* | 
|  | 377 | * We're generating jump to subroutines which will be outside the range of | 
|  | 378 | * jump instructions | 
|  | 379 | */ | 
|  | 380 | #ifdef MODULE | 
|  | 381 | #define __MODULE_JAL(destination)					\ | 
|  | 382 | ".set\tnoat\n\t"						\ | 
|  | 383 | __UA_LA "\t$1, " #destination "\n\t" 				\ | 
|  | 384 | "jalr\t$1\n\t"							\ | 
|  | 385 | ".set\tat\n\t" | 
|  | 386 | #else | 
|  | 387 | #define __MODULE_JAL(destination)					\ | 
|  | 388 | "jal\t" #destination "\n\t" | 
|  | 389 | #endif | 
|  | 390 |  | 
| Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 391 | #ifndef CONFIG_CPU_DADDI_WORKAROUNDS | 
|  | 392 | #define DADDI_SCRATCH "$0" | 
|  | 393 | #else | 
|  | 394 | #define DADDI_SCRATCH "$3" | 
|  | 395 | #endif | 
|  | 396 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | extern size_t __copy_user(void *__to, const void *__from, size_t __n); | 
|  | 398 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 399 | #define __invoke_copy_to_user(to, from, n)				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | ({									\ | 
| Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 401 | register void __user *__cu_to_r __asm__("$4");			\ | 
|  | 402 | register const void *__cu_from_r __asm__("$5");			\ | 
|  | 403 | register long __cu_len_r __asm__("$6");				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | \ | 
|  | 405 | __cu_to_r = (to);						\ | 
|  | 406 | __cu_from_r = (from);						\ | 
|  | 407 | __cu_len_r = (n);						\ | 
|  | 408 | __asm__ __volatile__(						\ | 
|  | 409 | __MODULE_JAL(__copy_user)					\ | 
|  | 410 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)	\ | 
|  | 411 | :								\ | 
|  | 412 | : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",		\ | 
| Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 413 | DADDI_SCRATCH, "memory");					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | __cu_len_r;							\ | 
|  | 415 | }) | 
|  | 416 |  | 
|  | 417 | /* | 
|  | 418 | * __copy_to_user: - Copy a block of data into user space, with less checking. | 
|  | 419 | * @to:   Destination address, in user space. | 
|  | 420 | * @from: Source address, in kernel space. | 
|  | 421 | * @n:    Number of bytes to copy. | 
|  | 422 | * | 
|  | 423 | * Context: User context only.  This function may sleep. | 
|  | 424 | * | 
|  | 425 | * Copy data from kernel space to user space.  Caller must check | 
|  | 426 | * the specified block with access_ok() before calling this function. | 
|  | 427 | * | 
|  | 428 | * Returns number of bytes that could not be copied. | 
|  | 429 | * On success, this will be zero. | 
|  | 430 | */ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 431 | #define __copy_to_user(to, from, n)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 433 | void __user *__cu_to;						\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | const void *__cu_from;						\ | 
|  | 435 | long __cu_len;							\ | 
|  | 436 | \ | 
|  | 437 | might_sleep();							\ | 
|  | 438 | __cu_to = (to);							\ | 
|  | 439 | __cu_from = (from);						\ | 
|  | 440 | __cu_len = (n);							\ | 
|  | 441 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len);	\ | 
|  | 442 | __cu_len;							\ | 
|  | 443 | }) | 
|  | 444 |  | 
| Ralf Baechle | d0c91ae | 2007-03-05 15:54:20 +0000 | [diff] [blame] | 445 | extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n); | 
|  | 446 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 447 | #define __copy_to_user_inatomic(to, from, n)				\ | 
| Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 448 | ({									\ | 
|  | 449 | void __user *__cu_to;						\ | 
|  | 450 | const void *__cu_from;						\ | 
|  | 451 | long __cu_len;							\ | 
|  | 452 | \ | 
|  | 453 | __cu_to = (to);							\ | 
|  | 454 | __cu_from = (from);						\ | 
|  | 455 | __cu_len = (n);							\ | 
|  | 456 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len);	\ | 
|  | 457 | __cu_len;							\ | 
|  | 458 | }) | 
|  | 459 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 460 | #define __copy_from_user_inatomic(to, from, n)				\ | 
| Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 461 | ({									\ | 
|  | 462 | void *__cu_to;							\ | 
|  | 463 | const void __user *__cu_from;					\ | 
|  | 464 | long __cu_len;							\ | 
|  | 465 | \ | 
|  | 466 | __cu_to = (to);							\ | 
|  | 467 | __cu_from = (from);						\ | 
|  | 468 | __cu_len = (n);							\ | 
|  | 469 | __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from,	\ | 
|  | 470 | __cu_len);		\ | 
|  | 471 | __cu_len;							\ | 
|  | 472 | }) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 |  | 
|  | 474 | /* | 
|  | 475 | * copy_to_user: - Copy a block of data into user space. | 
|  | 476 | * @to:   Destination address, in user space. | 
|  | 477 | * @from: Source address, in kernel space. | 
|  | 478 | * @n:    Number of bytes to copy. | 
|  | 479 | * | 
|  | 480 | * Context: User context only.  This function may sleep. | 
|  | 481 | * | 
|  | 482 | * Copy data from kernel space to user space. | 
|  | 483 | * | 
|  | 484 | * Returns number of bytes that could not be copied. | 
|  | 485 | * On success, this will be zero. | 
|  | 486 | */ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 487 | #define copy_to_user(to, from, n)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 489 | void __user *__cu_to;						\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | const void *__cu_from;						\ | 
|  | 491 | long __cu_len;							\ | 
|  | 492 | \ | 
|  | 493 | might_sleep();							\ | 
|  | 494 | __cu_to = (to);							\ | 
|  | 495 | __cu_from = (from);						\ | 
|  | 496 | __cu_len = (n);							\ | 
|  | 497 | if (access_ok(VERIFY_WRITE, __cu_to, __cu_len))			\ | 
|  | 498 | __cu_len = __invoke_copy_to_user(__cu_to, __cu_from,	\ | 
|  | 499 | __cu_len);		\ | 
|  | 500 | __cu_len;							\ | 
|  | 501 | }) | 
|  | 502 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 503 | #define __invoke_copy_from_user(to, from, n)				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | ({									\ | 
| Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 505 | register void *__cu_to_r __asm__("$4");				\ | 
|  | 506 | register const void __user *__cu_from_r __asm__("$5");		\ | 
|  | 507 | register long __cu_len_r __asm__("$6");				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | \ | 
|  | 509 | __cu_to_r = (to);						\ | 
|  | 510 | __cu_from_r = (from);						\ | 
|  | 511 | __cu_len_r = (n);						\ | 
|  | 512 | __asm__ __volatile__(						\ | 
|  | 513 | ".set\tnoreorder\n\t"						\ | 
|  | 514 | __MODULE_JAL(__copy_user)					\ | 
|  | 515 | ".set\tnoat\n\t"						\ | 
|  | 516 | __UA_ADDU "\t$1, %1, %2\n\t"					\ | 
|  | 517 | ".set\tat\n\t"							\ | 
|  | 518 | ".set\treorder"							\ | 
|  | 519 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)	\ | 
|  | 520 | :								\ | 
|  | 521 | : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",		\ | 
| Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 522 | DADDI_SCRATCH, "memory");					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | __cu_len_r;							\ | 
|  | 524 | }) | 
|  | 525 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 526 | #define __invoke_copy_from_user_inatomic(to, from, n)			\ | 
| Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 527 | ({									\ | 
| Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 528 | register void *__cu_to_r __asm__("$4");				\ | 
|  | 529 | register const void __user *__cu_from_r __asm__("$5");		\ | 
|  | 530 | register long __cu_len_r __asm__("$6");				\ | 
| Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 531 | \ | 
|  | 532 | __cu_to_r = (to);						\ | 
|  | 533 | __cu_from_r = (from);						\ | 
|  | 534 | __cu_len_r = (n);						\ | 
|  | 535 | __asm__ __volatile__(						\ | 
|  | 536 | ".set\tnoreorder\n\t"						\ | 
|  | 537 | __MODULE_JAL(__copy_user_inatomic)				\ | 
|  | 538 | ".set\tnoat\n\t"						\ | 
|  | 539 | __UA_ADDU "\t$1, %1, %2\n\t"					\ | 
|  | 540 | ".set\tat\n\t"							\ | 
|  | 541 | ".set\treorder"							\ | 
|  | 542 | : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)	\ | 
|  | 543 | :								\ | 
|  | 544 | : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",		\ | 
| Maciej W. Rozycki | 619b6e1 | 2007-10-23 12:43:25 +0100 | [diff] [blame] | 545 | DADDI_SCRATCH, "memory");					\ | 
| Ralf Baechle | e03b526 | 2007-02-19 16:59:24 +0000 | [diff] [blame] | 546 | __cu_len_r;							\ | 
|  | 547 | }) | 
|  | 548 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* | 
| Chris Dearman | 131c1a2 | 2007-02-01 19:54:13 +0000 | [diff] [blame] | 550 | * __copy_from_user: - Copy a block of data from user space, with less checking. | 
|  | 551 | * @to:   Destination address, in kernel space. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | * @from: Source address, in user space. | 
|  | 553 | * @n:    Number of bytes to copy. | 
|  | 554 | * | 
|  | 555 | * Context: User context only.  This function may sleep. | 
|  | 556 | * | 
|  | 557 | * Copy data from user space to kernel space.  Caller must check | 
|  | 558 | * the specified block with access_ok() before calling this function. | 
|  | 559 | * | 
|  | 560 | * Returns number of bytes that could not be copied. | 
|  | 561 | * On success, this will be zero. | 
|  | 562 | * | 
|  | 563 | * If some data could not be copied, this function will pad the copied | 
|  | 564 | * data to the requested size using zero bytes. | 
|  | 565 | */ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 566 | #define __copy_from_user(to, from, n)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | ({									\ | 
|  | 568 | void *__cu_to;							\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 569 | const void __user *__cu_from;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | long __cu_len;							\ | 
|  | 571 | \ | 
|  | 572 | might_sleep();							\ | 
|  | 573 | __cu_to = (to);							\ | 
|  | 574 | __cu_from = (from);						\ | 
|  | 575 | __cu_len = (n);							\ | 
|  | 576 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,		\ | 
|  | 577 | __cu_len);			\ | 
|  | 578 | __cu_len;							\ | 
|  | 579 | }) | 
|  | 580 |  | 
|  | 581 | /* | 
|  | 582 | * copy_from_user: - Copy a block of data from user space. | 
|  | 583 | * @to:   Destination address, in kernel space. | 
|  | 584 | * @from: Source address, in user space. | 
|  | 585 | * @n:    Number of bytes to copy. | 
|  | 586 | * | 
|  | 587 | * Context: User context only.  This function may sleep. | 
|  | 588 | * | 
|  | 589 | * Copy data from user space to kernel space. | 
|  | 590 | * | 
|  | 591 | * Returns number of bytes that could not be copied. | 
|  | 592 | * On success, this will be zero. | 
|  | 593 | * | 
|  | 594 | * If some data could not be copied, this function will pad the copied | 
|  | 595 | * data to the requested size using zero bytes. | 
|  | 596 | */ | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 597 | #define copy_from_user(to, from, n)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | ({									\ | 
|  | 599 | void *__cu_to;							\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 600 | const void __user *__cu_from;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | long __cu_len;							\ | 
|  | 602 | \ | 
|  | 603 | might_sleep();							\ | 
|  | 604 | __cu_to = (to);							\ | 
|  | 605 | __cu_from = (from);						\ | 
|  | 606 | __cu_len = (n);							\ | 
|  | 607 | if (access_ok(VERIFY_READ, __cu_from, __cu_len))		\ | 
|  | 608 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,	\ | 
|  | 609 | __cu_len);		\ | 
|  | 610 | __cu_len;							\ | 
|  | 611 | }) | 
|  | 612 |  | 
|  | 613 | #define __copy_in_user(to, from, n)	__copy_from_user(to, from, n) | 
|  | 614 |  | 
| Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 615 | #define copy_in_user(to, from, n)					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 617 | void __user *__cu_to;						\ | 
|  | 618 | const void __user *__cu_from;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | long __cu_len;							\ | 
|  | 620 | \ | 
|  | 621 | might_sleep();							\ | 
|  | 622 | __cu_to = (to);							\ | 
|  | 623 | __cu_from = (from);						\ | 
|  | 624 | __cu_len = (n);							\ | 
|  | 625 | if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&	\ | 
|  | 626 | access_ok(VERIFY_WRITE, __cu_to, __cu_len)))		\ | 
|  | 627 | __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,	\ | 
|  | 628 | __cu_len);		\ | 
|  | 629 | __cu_len;							\ | 
|  | 630 | }) | 
|  | 631 |  | 
|  | 632 | /* | 
|  | 633 | * __clear_user: - Zero a block of memory in user space, with less checking. | 
|  | 634 | * @to:   Destination address, in user space. | 
|  | 635 | * @n:    Number of bytes to zero. | 
|  | 636 | * | 
|  | 637 | * Zero a block of memory in user space.  Caller must check | 
|  | 638 | * the specified block with access_ok() before calling this function. | 
|  | 639 | * | 
|  | 640 | * Returns number of bytes that could not be cleared. | 
|  | 641 | * On success, this will be zero. | 
|  | 642 | */ | 
|  | 643 | static inline __kernel_size_t | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 644 | __clear_user(void __user *addr, __kernel_size_t size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { | 
|  | 646 | __kernel_size_t res; | 
|  | 647 |  | 
|  | 648 | might_sleep(); | 
|  | 649 | __asm__ __volatile__( | 
|  | 650 | "move\t$4, %1\n\t" | 
|  | 651 | "move\t$5, $0\n\t" | 
|  | 652 | "move\t$6, %2\n\t" | 
|  | 653 | __MODULE_JAL(__bzero) | 
|  | 654 | "move\t%0, $6" | 
|  | 655 | : "=r" (res) | 
|  | 656 | : "r" (addr), "r" (size) | 
|  | 657 | : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31"); | 
|  | 658 |  | 
|  | 659 | return res; | 
|  | 660 | } | 
|  | 661 |  | 
|  | 662 | #define clear_user(addr,n)						\ | 
|  | 663 | ({									\ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 664 | void __user * __cl_addr = (addr);				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | unsigned long __cl_size = (n);					\ | 
|  | 666 | if (__cl_size && access_ok(VERIFY_WRITE,			\ | 
|  | 667 | ((unsigned long)(__cl_addr)), __cl_size))		\ | 
|  | 668 | __cl_size = __clear_user(__cl_addr, __cl_size);		\ | 
|  | 669 | __cl_size;							\ | 
|  | 670 | }) | 
|  | 671 |  | 
|  | 672 | /* | 
|  | 673 | * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking. | 
|  | 674 | * @dst:   Destination address, in kernel space.  This buffer must be at | 
|  | 675 | *         least @count bytes long. | 
|  | 676 | * @src:   Source address, in user space. | 
|  | 677 | * @count: Maximum number of bytes to copy, including the trailing NUL. | 
|  | 678 | * | 
|  | 679 | * Copies a NUL-terminated string from userspace to kernel space. | 
|  | 680 | * Caller must check the specified block with access_ok() before calling | 
|  | 681 | * this function. | 
|  | 682 | * | 
|  | 683 | * On success, returns the length of the string (not including the trailing | 
|  | 684 | * NUL). | 
|  | 685 | * | 
|  | 686 | * If access to userspace fails, returns -EFAULT (some data may have been | 
|  | 687 | * copied). | 
|  | 688 | * | 
|  | 689 | * If @count is smaller than the length of the string, copies @count bytes | 
|  | 690 | * and returns @count. | 
|  | 691 | */ | 
|  | 692 | static inline long | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 693 | __strncpy_from_user(char *__to, const char __user *__from, long __len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { | 
|  | 695 | long res; | 
|  | 696 |  | 
|  | 697 | might_sleep(); | 
|  | 698 | __asm__ __volatile__( | 
|  | 699 | "move\t$4, %1\n\t" | 
|  | 700 | "move\t$5, %2\n\t" | 
|  | 701 | "move\t$6, %3\n\t" | 
|  | 702 | __MODULE_JAL(__strncpy_from_user_nocheck_asm) | 
|  | 703 | "move\t%0, $2" | 
|  | 704 | : "=r" (res) | 
|  | 705 | : "r" (__to), "r" (__from), "r" (__len) | 
|  | 706 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); | 
|  | 707 |  | 
|  | 708 | return res; | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | /* | 
|  | 712 | * strncpy_from_user: - Copy a NUL terminated string from userspace. | 
|  | 713 | * @dst:   Destination address, in kernel space.  This buffer must be at | 
|  | 714 | *         least @count bytes long. | 
|  | 715 | * @src:   Source address, in user space. | 
|  | 716 | * @count: Maximum number of bytes to copy, including the trailing NUL. | 
|  | 717 | * | 
|  | 718 | * Copies a NUL-terminated string from userspace to kernel space. | 
|  | 719 | * | 
|  | 720 | * On success, returns the length of the string (not including the trailing | 
|  | 721 | * NUL). | 
|  | 722 | * | 
|  | 723 | * If access to userspace fails, returns -EFAULT (some data may have been | 
|  | 724 | * copied). | 
|  | 725 | * | 
|  | 726 | * If @count is smaller than the length of the string, copies @count bytes | 
|  | 727 | * and returns @count. | 
|  | 728 | */ | 
|  | 729 | static inline long | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 730 | strncpy_from_user(char *__to, const char __user *__from, long __len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { | 
|  | 732 | long res; | 
|  | 733 |  | 
|  | 734 | might_sleep(); | 
|  | 735 | __asm__ __volatile__( | 
|  | 736 | "move\t$4, %1\n\t" | 
|  | 737 | "move\t$5, %2\n\t" | 
|  | 738 | "move\t$6, %3\n\t" | 
|  | 739 | __MODULE_JAL(__strncpy_from_user_asm) | 
|  | 740 | "move\t%0, $2" | 
|  | 741 | : "=r" (res) | 
|  | 742 | : "r" (__to), "r" (__from), "r" (__len) | 
|  | 743 | : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory"); | 
|  | 744 |  | 
|  | 745 | return res; | 
|  | 746 | } | 
|  | 747 |  | 
|  | 748 | /* 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] | 749 | static inline long __strlen_user(const char __user *s) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { | 
|  | 751 | long res; | 
|  | 752 |  | 
|  | 753 | might_sleep(); | 
|  | 754 | __asm__ __volatile__( | 
|  | 755 | "move\t$4, %1\n\t" | 
|  | 756 | __MODULE_JAL(__strlen_user_nocheck_asm) | 
|  | 757 | "move\t%0, $2" | 
|  | 758 | : "=r" (res) | 
|  | 759 | : "r" (s) | 
|  | 760 | : "$2", "$4", __UA_t0, "$31"); | 
|  | 761 |  | 
|  | 762 | return res; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | /* | 
|  | 766 | * strlen_user: - Get the size of a string in user space. | 
|  | 767 | * @str: The string to measure. | 
|  | 768 | * | 
|  | 769 | * Context: User context only.  This function may sleep. | 
|  | 770 | * | 
|  | 771 | * Get the size of a NUL-terminated string in user space. | 
|  | 772 | * | 
|  | 773 | * Returns the size of the string INCLUDING the terminating NUL. | 
|  | 774 | * On exception, returns 0. | 
|  | 775 | * | 
|  | 776 | * If there is a limit on the length of a valid string, you may wish to | 
|  | 777 | * consider using strnlen_user() instead. | 
|  | 778 | */ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 779 | static inline long strlen_user(const char __user *s) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { | 
|  | 781 | long res; | 
|  | 782 |  | 
|  | 783 | might_sleep(); | 
|  | 784 | __asm__ __volatile__( | 
|  | 785 | "move\t$4, %1\n\t" | 
|  | 786 | __MODULE_JAL(__strlen_user_asm) | 
|  | 787 | "move\t%0, $2" | 
|  | 788 | : "=r" (res) | 
|  | 789 | : "r" (s) | 
|  | 790 | : "$2", "$4", __UA_t0, "$31"); | 
|  | 791 |  | 
|  | 792 | return res; | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | /* 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] | 796 | static inline long __strnlen_user(const char __user *s, long n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { | 
|  | 798 | long res; | 
|  | 799 |  | 
|  | 800 | might_sleep(); | 
|  | 801 | __asm__ __volatile__( | 
|  | 802 | "move\t$4, %1\n\t" | 
|  | 803 | "move\t$5, %2\n\t" | 
|  | 804 | __MODULE_JAL(__strnlen_user_nocheck_asm) | 
|  | 805 | "move\t%0, $2" | 
|  | 806 | : "=r" (res) | 
|  | 807 | : "r" (s), "r" (n) | 
|  | 808 | : "$2", "$4", "$5", __UA_t0, "$31"); | 
|  | 809 |  | 
|  | 810 | return res; | 
|  | 811 | } | 
|  | 812 |  | 
|  | 813 | /* | 
|  | 814 | * strlen_user: - Get the size of a string in user space. | 
|  | 815 | * @str: The string to measure. | 
|  | 816 | * | 
|  | 817 | * Context: User context only.  This function may sleep. | 
|  | 818 | * | 
|  | 819 | * Get the size of a NUL-terminated string in user space. | 
|  | 820 | * | 
|  | 821 | * Returns the size of the string INCLUDING the terminating NUL. | 
|  | 822 | * On exception, returns 0. | 
|  | 823 | * | 
|  | 824 | * If there is a limit on the length of a valid string, you may wish to | 
|  | 825 | * consider using strnlen_user() instead. | 
|  | 826 | */ | 
| Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 827 | static inline long strnlen_user(const char __user *s, long n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { | 
|  | 829 | long res; | 
|  | 830 |  | 
|  | 831 | might_sleep(); | 
|  | 832 | __asm__ __volatile__( | 
|  | 833 | "move\t$4, %1\n\t" | 
|  | 834 | "move\t$5, %2\n\t" | 
|  | 835 | __MODULE_JAL(__strnlen_user_asm) | 
|  | 836 | "move\t%0, $2" | 
|  | 837 | : "=r" (res) | 
|  | 838 | : "r" (s), "r" (n) | 
|  | 839 | : "$2", "$4", "$5", __UA_t0, "$31"); | 
|  | 840 |  | 
|  | 841 | return res; | 
|  | 842 | } | 
|  | 843 |  | 
|  | 844 | struct exception_table_entry | 
|  | 845 | { | 
|  | 846 | unsigned long insn; | 
|  | 847 | unsigned long nextinsn; | 
|  | 848 | }; | 
|  | 849 |  | 
|  | 850 | extern int fixup_exception(struct pt_regs *regs); | 
|  | 851 |  | 
|  | 852 | #endif /* _ASM_UACCESS_H */ |