blob: 9c6f5cec3e82a8cb4c5e68933a07267bb0507da0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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. Rozycki619b6e12007-10-23 12:43:25 +01008 * Copyright (C) 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#ifndef _ASM_UACCESS_H
11#define _ASM_UACCESS_H
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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 Baechle875d43e2005-09-03 15:56:16 -070024#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Sanjay Lal9843b032012-11-21 18:34:03 -080026#ifdef CONFIG_KVM_GUEST
27#define __UA_LIMIT 0x40000000UL
28#else
29#define __UA_LIMIT 0x80000000UL
30#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define __UA_ADDR ".word"
33#define __UA_LA "la"
34#define __UA_ADDU "addu"
35#define __UA_t0 "$8"
36#define __UA_t1 "$9"
37
Ralf Baechle875d43e2005-09-03 15:56:16 -070038#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ralf Baechle875d43e2005-09-03 15:56:16 -070040#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Daney949e51b2010-10-14 11:32:33 -070042extern u64 __ua_limit;
43
44#define __UA_LIMIT __ua_limit
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define __UA_ADDR ".dword"
47#define __UA_LA "dla"
48#define __UA_ADDU "daddu"
49#define __UA_t0 "$12"
50#define __UA_t1 "$13"
51
Ralf Baechle875d43e2005-09-03 15:56:16 -070052#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * USER_DS is a bitmask that has the bits set that may not be set in a valid
56 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
57 * the arithmetic we're doing only works if the limit is a power of two, so
58 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
59 * address in this range it's the process's problem, not ours :-)
60 */
61
Sanjay Lal9843b032012-11-21 18:34:03 -080062#ifdef CONFIG_KVM_GUEST
63#define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
64#define USER_DS ((mm_segment_t) { 0xC0000000UL })
65#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define KERNEL_DS ((mm_segment_t) { 0UL })
67#define USER_DS ((mm_segment_t) { __UA_LIMIT })
Sanjay Lal9843b032012-11-21 18:34:03 -080068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define VERIFY_READ 0
71#define VERIFY_WRITE 1
72
73#define get_ds() (KERNEL_DS)
74#define get_fs() (current_thread_info()->addr_limit)
75#define set_fs(x) (current_thread_info()->addr_limit = (x))
76
Ralf Baechle21a151d2007-10-11 23:46:15 +010077#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79
80/*
81 * Is a address valid? This does a straighforward calculation rather
82 * than tests.
83 *
84 * Address valid if:
85 * - "addr" doesn't have any high-bits set
86 * - AND "size" doesn't have any high-bits set
87 * - AND "addr+size" doesn't have any high-bits set
88 * - OR we are in kernel mode.
89 *
90 * __ua_size() is a trick to avoid runtime checking of positive constant
91 * sizes; for those we already know at compile time that the size is ok.
92 */
93#define __ua_size(size) \
94 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
95
96/*
97 * access_ok: - Checks if a user space pointer is valid
98 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
Ralf Baechle70342282013-01-22 12:59:30 +010099 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
100 * to write to a block, it is always safe to read from it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * @addr: User space pointer to start of block to check
102 * @size: Size of block to check
103 *
Ralf Baechle70342282013-01-22 12:59:30 +0100104 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * Checks if a pointer to a block of memory in user space is valid.
107 *
108 * Returns true (nonzero) if the memory block may be valid, false (zero)
109 * if it is definitely invalid.
110 *
111 * Note that, depending on architecture, this function probably just
112 * checks that the pointer is in the user space range - after calling
113 * this function, memory access functions may still return -EFAULT.
114 */
115
116#define __access_mask get_fs().seg
117
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200118#define __access_ok(addr, size, mask) \
119({ \
120 unsigned long __addr = (unsigned long) (addr); \
121 unsigned long __size = size; \
122 unsigned long __mask = mask; \
123 unsigned long __ok; \
124 \
125 __chk_user_ptr(addr); \
126 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
127 __ua_size(__size))); \
128 __ok == 0; \
Ralf Baechled0aab922009-04-27 15:31:34 +0200129})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define access_ok(type, addr, size) \
Ralf Baechled0aab922009-04-27 15:31:34 +0200132 likely(__access_ok((addr), (size), __access_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * put_user: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100136 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * @ptr: Destination address, in user space.
138 *
Ralf Baechle70342282013-01-22 12:59:30 +0100139 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * This macro copies a single simple value from kernel space to user
142 * space. It supports simple types like char and int, but not larger
143 * data types like structures or arrays.
144 *
145 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
146 * to the result of dereferencing @ptr.
147 *
148 * Returns zero on success, or -EFAULT on error.
149 */
Ralf Baechle70342282013-01-22 12:59:30 +0100150#define put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100151 __put_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * get_user: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100155 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * @ptr: Source address, in user space.
157 *
Ralf Baechle70342282013-01-22 12:59:30 +0100158 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * This macro copies a single simple variable from user space to kernel
161 * space. It supports simple types like char and int, but not larger
162 * data types like structures or arrays.
163 *
164 * @ptr must have pointer-to-simple-variable type, and the result of
165 * dereferencing @ptr must be assignable to @x without a cast.
166 *
167 * Returns zero on success, or -EFAULT on error.
168 * On error, the variable @x is set to zero.
169 */
170#define get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100171 __get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/*
174 * __put_user: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100175 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * @ptr: Destination address, in user space.
177 *
Ralf Baechle70342282013-01-22 12:59:30 +0100178 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
180 * This macro copies a single simple value from kernel space to user
181 * space. It supports simple types like char and int, but not larger
182 * data types like structures or arrays.
183 *
184 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
185 * to the result of dereferencing @ptr.
186 *
187 * Caller must check the pointer with access_ok() before calling this
188 * function.
189 *
190 * Returns zero on success, or -EFAULT on error.
191 */
192#define __put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100193 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * __get_user: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100197 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * @ptr: Source address, in user space.
199 *
Ralf Baechle70342282013-01-22 12:59:30 +0100200 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * This macro copies a single simple variable from user space to kernel
203 * space. It supports simple types like char and int, but not larger
204 * data types like structures or arrays.
205 *
206 * @ptr must have pointer-to-simple-variable type, and the result of
207 * dereferencing @ptr must be assignable to @x without a cast.
208 *
209 * Caller must check the pointer with access_ok() before calling this
210 * function.
211 *
212 * Returns zero on success, or -EFAULT on error.
213 * On error, the variable @x is set to zero.
214 */
215#define __get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100216 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000219#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
222 * Yuck. We need two variants, one for 64bit operation and one
223 * for 32 bit mode and old iron.
224 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000225#ifdef CONFIG_32BIT
Markos Chandrasd84869a2013-12-17 13:15:40 +0000226#define __GET_USER_DW(val, insn, ptr) __get_user_asm_ll32(val, insn, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000228#ifdef CONFIG_64BIT
Markos Chandrasd84869a2013-12-17 13:15:40 +0000229#define __GET_USER_DW(val, insn, ptr) __get_user_asm(val, "ld", ptr)
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000230#endif
231
232extern void __get_user_unknown(void);
233
234#define __get_user_common(val, size, ptr) \
235do { \
236 switch (size) { \
237 case 1: __get_user_asm(val, "lb", ptr); break; \
238 case 2: __get_user_asm(val, "lh", ptr); break; \
239 case 4: __get_user_asm(val, "lw", ptr); break; \
Markos Chandrasd84869a2013-12-17 13:15:40 +0000240 case 8: __GET_USER_DW(val, "lw", ptr); break; \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000241 default: __get_user_unknown(); break; \
242 } \
243} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Ralf Baechle21a151d2007-10-11 23:46:15 +0100245#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100247 int __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200249 __chk_user_ptr(ptr); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000250 __get_user_common((x), size, ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 __gu_err; \
252})
253
Ralf Baechle21a151d2007-10-11 23:46:15 +0100254#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100256 int __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900257 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200259 might_fault(); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000260 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
261 __get_user_common((x), size, __gu_ptr); \
262 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 __gu_err; \
264})
265
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000266#define __get_user_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000267{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000268 long __gu_tmp; \
269 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 __asm__ __volatile__( \
271 "1: " insn " %1, %3 \n" \
272 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500273 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 " .section .fixup,\"ax\" \n" \
275 "3: li %0, %4 \n" \
276 " j 2b \n" \
277 " .previous \n" \
278 " .section __ex_table,\"a\" \n" \
279 " "__UA_ADDR "\t1b, 3b \n" \
280 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000281 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000282 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000283 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900284 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/*
288 * Get a long long 64 using 32 bit registers.
289 */
Markos Chandrasd84869a2013-12-17 13:15:40 +0000290#define __get_user_asm_ll32(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000291{ \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000292 union { \
293 unsigned long long l; \
294 __typeof__(*(addr)) t; \
295 } __gu_tmp; \
Ralf Baechlecd1fb9e2007-02-12 23:12:38 +0000296 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 __asm__ __volatile__( \
Markos Chandrasd84869a2013-12-17 13:15:40 +0000298 "1: " insn " %1, (%3) \n" \
299 "2: " insn " %D1, 4(%3) \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500300 "3: \n" \
301 " .insn \n" \
302 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000303 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 " move %1, $0 \n" \
305 " move %D1, $0 \n" \
306 " j 3b \n" \
307 " .previous \n" \
308 " .section __ex_table,\"a\" \n" \
309 " " __UA_ADDR " 1b, 4b \n" \
310 " " __UA_ADDR " 2b, 4b \n" \
311 " .previous \n" \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000312 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000313 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000314 \
315 (val) = __gu_tmp.t; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Yuck. We need two variants, one for 64bit operation and one
320 * for 32 bit mode and old iron.
321 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000322#ifdef CONFIG_32BIT
Markos Chandrasd84869a2013-12-17 13:15:40 +0000323#define __PUT_USER_DW(insn, ptr) __put_user_asm_ll32(insn, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000325#ifdef CONFIG_64BIT
Markos Chandrasd84869a2013-12-17 13:15:40 +0000326#define __PUT_USER_DW(insn, ptr) __put_user_asm("sd", ptr)
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000329#define __put_user_common(ptr, size) \
330do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000332 case 1: __put_user_asm("sb", ptr); break; \
333 case 2: __put_user_asm("sh", ptr); break; \
334 case 4: __put_user_asm("sw", ptr); break; \
Markos Chandrasd84869a2013-12-17 13:15:40 +0000335 case 8: __PUT_USER_DW("sw", ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 default: __put_user_unknown(); break; \
337 } \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000338} while (0)
339
340#define __put_user_nocheck(x, ptr, size) \
341({ \
342 __typeof__(*(ptr)) __pu_val; \
343 int __pu_err = 0; \
344 \
345 __chk_user_ptr(ptr); \
346 __pu_val = (x); \
347 __put_user_common(ptr, size); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 __pu_err; \
349})
350
Ralf Baechle21a151d2007-10-11 23:46:15 +0100351#define __put_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000353 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
354 __typeof__(*(ptr)) __pu_val = (x); \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100355 int __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200357 might_fault(); \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000358 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
359 __put_user_common(__pu_addr, size); \
360 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 __pu_err; \
362})
363
Ralf Baechlefe00f942005-03-01 19:22:29 +0000364#define __put_user_asm(insn, ptr) \
365{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __asm__ __volatile__( \
367 "1: " insn " %z2, %3 # __put_user_asm\n" \
368 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500369 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 " .section .fixup,\"ax\" \n" \
371 "3: li %0, %4 \n" \
372 " j 2b \n" \
373 " .previous \n" \
374 " .section __ex_table,\"a\" \n" \
375 " " __UA_ADDR " 1b, 3b \n" \
376 " .previous \n" \
377 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000378 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000380}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Markos Chandrasd84869a2013-12-17 13:15:40 +0000382#define __put_user_asm_ll32(insn, ptr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000383{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 __asm__ __volatile__( \
Markos Chandrasd84869a2013-12-17 13:15:40 +0000385 "1: " insn " %2, (%3)# __put_user_asm_ll32 \n" \
386 "2: " insn " %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500388 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000390 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 " j 3b \n" \
392 " .previous \n" \
393 " .section __ex_table,\"a\" \n" \
394 " " __UA_ADDR " 1b, 4b \n" \
395 " " __UA_ADDR " 2b, 4b \n" \
396 " .previous" \
397 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000398 : "0" (0), "r" (__pu_val), "r" (ptr), \
399 "i" (-EFAULT)); \
400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402extern void __put_user_unknown(void);
403
404/*
Leonid Yegoshin18e90012013-12-17 15:20:24 +0000405 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
406 * EVA unaligned access is handled in the ADE exception handler.
407 */
408#ifndef CONFIG_EVA
409/*
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000410 * put_user_unaligned: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100411 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000412 * @ptr: Destination address, in user space.
413 *
Ralf Baechle70342282013-01-22 12:59:30 +0100414 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000415 *
416 * This macro copies a single simple value from kernel space to user
417 * space. It supports simple types like char and int, but not larger
418 * data types like structures or arrays.
419 *
420 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
421 * to the result of dereferencing @ptr.
422 *
423 * Returns zero on success, or -EFAULT on error.
424 */
425#define put_user_unaligned(x,ptr) \
426 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
427
428/*
429 * get_user_unaligned: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100430 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000431 * @ptr: Source address, in user space.
432 *
Ralf Baechle70342282013-01-22 12:59:30 +0100433 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000434 *
435 * This macro copies a single simple variable from user space to kernel
436 * space. It supports simple types like char and int, but not larger
437 * data types like structures or arrays.
438 *
439 * @ptr must have pointer-to-simple-variable type, and the result of
440 * dereferencing @ptr must be assignable to @x without a cast.
441 *
442 * Returns zero on success, or -EFAULT on error.
443 * On error, the variable @x is set to zero.
444 */
445#define get_user_unaligned(x,ptr) \
446 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
447
448/*
449 * __put_user_unaligned: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100450 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000451 * @ptr: Destination address, in user space.
452 *
Ralf Baechle70342282013-01-22 12:59:30 +0100453 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000454 *
455 * This macro copies a single simple value from kernel space to user
456 * space. It supports simple types like char and int, but not larger
457 * data types like structures or arrays.
458 *
459 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
460 * to the result of dereferencing @ptr.
461 *
462 * Caller must check the pointer with access_ok() before calling this
463 * function.
464 *
465 * Returns zero on success, or -EFAULT on error.
466 */
467#define __put_user_unaligned(x,ptr) \
468 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
469
470/*
471 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100472 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000473 * @ptr: Source address, in user space.
474 *
Ralf Baechle70342282013-01-22 12:59:30 +0100475 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000476 *
477 * This macro copies a single simple variable from user space to kernel
478 * space. It supports simple types like char and int, but not larger
479 * data types like structures or arrays.
480 *
481 * @ptr must have pointer-to-simple-variable type, and the result of
482 * dereferencing @ptr must be assignable to @x without a cast.
483 *
484 * Caller must check the pointer with access_ok() before calling this
485 * function.
486 *
487 * Returns zero on success, or -EFAULT on error.
488 * On error, the variable @x is set to zero.
489 */
490#define __get_user_unaligned(x,ptr) \
491 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
492
493/*
494 * Yuck. We need two variants, one for 64bit operation and one
495 * for 32 bit mode and old iron.
496 */
497#ifdef CONFIG_32BIT
498#define __GET_USER_UNALIGNED_DW(val, ptr) \
499 __get_user_unaligned_asm_ll32(val, ptr)
500#endif
501#ifdef CONFIG_64BIT
502#define __GET_USER_UNALIGNED_DW(val, ptr) \
503 __get_user_unaligned_asm(val, "uld", ptr)
504#endif
505
506extern void __get_user_unaligned_unknown(void);
507
508#define __get_user_unaligned_common(val, size, ptr) \
509do { \
510 switch (size) { \
511 case 1: __get_user_asm(val, "lb", ptr); break; \
512 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
513 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
514 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
515 default: __get_user_unaligned_unknown(); break; \
516 } \
517} while (0)
518
519#define __get_user_unaligned_nocheck(x,ptr,size) \
520({ \
521 int __gu_err; \
522 \
523 __get_user_unaligned_common((x), size, ptr); \
524 __gu_err; \
525})
526
527#define __get_user_unaligned_check(x,ptr,size) \
528({ \
529 int __gu_err = -EFAULT; \
530 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
531 \
532 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
533 __get_user_unaligned_common((x), size, __gu_ptr); \
534 \
535 __gu_err; \
536})
537
538#define __get_user_unaligned_asm(val, insn, addr) \
539{ \
540 long __gu_tmp; \
541 \
542 __asm__ __volatile__( \
543 "1: " insn " %1, %3 \n" \
544 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500545 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000546 " .section .fixup,\"ax\" \n" \
547 "3: li %0, %4 \n" \
548 " j 2b \n" \
549 " .previous \n" \
550 " .section __ex_table,\"a\" \n" \
551 " "__UA_ADDR "\t1b, 3b \n" \
552 " "__UA_ADDR "\t1b + 4, 3b \n" \
553 " .previous \n" \
554 : "=r" (__gu_err), "=r" (__gu_tmp) \
555 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
556 \
557 (val) = (__typeof__(*(addr))) __gu_tmp; \
558}
559
560/*
561 * Get a long long 64 using 32 bit registers.
562 */
563#define __get_user_unaligned_asm_ll32(val, addr) \
564{ \
Ralf Baechle70342282013-01-22 12:59:30 +0100565 unsigned long long __gu_tmp; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000566 \
567 __asm__ __volatile__( \
568 "1: ulw %1, (%3) \n" \
569 "2: ulw %D1, 4(%3) \n" \
570 " move %0, $0 \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500571 "3: \n" \
572 " .insn \n" \
573 " .section .fixup,\"ax\" \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000574 "4: li %0, %4 \n" \
575 " move %1, $0 \n" \
576 " move %D1, $0 \n" \
577 " j 3b \n" \
578 " .previous \n" \
579 " .section __ex_table,\"a\" \n" \
580 " " __UA_ADDR " 1b, 4b \n" \
581 " " __UA_ADDR " 1b + 4, 4b \n" \
582 " " __UA_ADDR " 2b, 4b \n" \
583 " " __UA_ADDR " 2b + 4, 4b \n" \
584 " .previous \n" \
585 : "=r" (__gu_err), "=&r" (__gu_tmp) \
586 : "0" (0), "r" (addr), "i" (-EFAULT)); \
587 (val) = (__typeof__(*(addr))) __gu_tmp; \
588}
589
590/*
591 * Yuck. We need two variants, one for 64bit operation and one
592 * for 32 bit mode and old iron.
593 */
594#ifdef CONFIG_32BIT
595#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
596#endif
597#ifdef CONFIG_64BIT
598#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
599#endif
600
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000601#define __put_user_unaligned_common(ptr, size) \
602do { \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000603 switch (size) { \
604 case 1: __put_user_asm("sb", ptr); break; \
605 case 2: __put_user_unaligned_asm("ush", ptr); break; \
606 case 4: __put_user_unaligned_asm("usw", ptr); break; \
607 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
608 default: __put_user_unaligned_unknown(); break; \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000609} while (0)
610
611#define __put_user_unaligned_nocheck(x,ptr,size) \
612({ \
613 __typeof__(*(ptr)) __pu_val; \
614 int __pu_err = 0; \
615 \
616 __pu_val = (x); \
617 __put_user_unaligned_common(ptr, size); \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000618 __pu_err; \
619})
620
621#define __put_user_unaligned_check(x,ptr,size) \
622({ \
623 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
624 __typeof__(*(ptr)) __pu_val = (x); \
625 int __pu_err = -EFAULT; \
626 \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000627 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
628 __put_user_unaligned_common(__pu_addr, size); \
629 \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000630 __pu_err; \
631})
632
633#define __put_user_unaligned_asm(insn, ptr) \
634{ \
635 __asm__ __volatile__( \
636 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
637 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500638 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000639 " .section .fixup,\"ax\" \n" \
640 "3: li %0, %4 \n" \
641 " j 2b \n" \
642 " .previous \n" \
643 " .section __ex_table,\"a\" \n" \
644 " " __UA_ADDR " 1b, 3b \n" \
645 " .previous \n" \
646 : "=r" (__pu_err) \
647 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
648 "i" (-EFAULT)); \
649}
650
651#define __put_user_unaligned_asm_ll32(ptr) \
652{ \
653 __asm__ __volatile__( \
Ralf Baechle70342282013-01-22 12:59:30 +0100654 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000655 "2: sw %D2, 4(%3) \n" \
656 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500657 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000658 " .section .fixup,\"ax\" \n" \
659 "4: li %0, %4 \n" \
660 " j 3b \n" \
661 " .previous \n" \
662 " .section __ex_table,\"a\" \n" \
663 " " __UA_ADDR " 1b, 4b \n" \
664 " " __UA_ADDR " 1b + 4, 4b \n" \
665 " " __UA_ADDR " 2b, 4b \n" \
666 " " __UA_ADDR " 2b + 4, 4b \n" \
667 " .previous" \
668 : "=r" (__pu_err) \
669 : "0" (0), "r" (__pu_val), "r" (ptr), \
670 "i" (-EFAULT)); \
671}
672
673extern void __put_user_unaligned_unknown(void);
Leonid Yegoshin18e90012013-12-17 15:20:24 +0000674#endif
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000675
676/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * We're generating jump to subroutines which will be outside the range of
678 * jump instructions
679 */
680#ifdef MODULE
681#define __MODULE_JAL(destination) \
682 ".set\tnoat\n\t" \
Ralf Baechle70342282013-01-22 12:59:30 +0100683 __UA_LA "\t$1, " #destination "\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 "jalr\t$1\n\t" \
685 ".set\tat\n\t"
686#else
687#define __MODULE_JAL(destination) \
688 "jal\t" #destination "\n\t"
689#endif
690
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100691#ifndef CONFIG_CPU_DADDI_WORKAROUNDS
692#define DADDI_SCRATCH "$0"
693#else
694#define DADDI_SCRATCH "$3"
695#endif
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697extern size_t __copy_user(void *__to, const void *__from, size_t __n);
698
Ralf Baechle21a151d2007-10-11 23:46:15 +0100699#define __invoke_copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100701 register void __user *__cu_to_r __asm__("$4"); \
702 register const void *__cu_from_r __asm__("$5"); \
703 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 \
705 __cu_to_r = (to); \
706 __cu_from_r = (from); \
707 __cu_len_r = (n); \
708 __asm__ __volatile__( \
709 __MODULE_JAL(__copy_user) \
710 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
711 : \
David Daneybb0757e2012-06-06 23:00:31 +0100712 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100713 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 __cu_len_r; \
715})
716
717/*
718 * __copy_to_user: - Copy a block of data into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100719 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100721 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
Ralf Baechle70342282013-01-22 12:59:30 +0100723 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 *
725 * Copy data from kernel space to user space. Caller must check
726 * the specified block with access_ok() before calling this function.
727 *
728 * Returns number of bytes that could not be copied.
729 * On success, this will be zero.
730 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100731#define __copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000733 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 const void *__cu_from; \
735 long __cu_len; \
736 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 __cu_to = (to); \
738 __cu_from = (from); \
739 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200740 might_fault(); \
Ralf Baechle70342282013-01-22 12:59:30 +0100741 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 __cu_len; \
743})
744
Ralf Baechled0c91ae2007-03-05 15:54:20 +0000745extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
746
Ralf Baechle21a151d2007-10-11 23:46:15 +0100747#define __copy_to_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000748({ \
749 void __user *__cu_to; \
750 const void *__cu_from; \
751 long __cu_len; \
752 \
753 __cu_to = (to); \
754 __cu_from = (from); \
755 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100756 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000757 __cu_len; \
758})
759
Ralf Baechle21a151d2007-10-11 23:46:15 +0100760#define __copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000761({ \
762 void *__cu_to; \
763 const void __user *__cu_from; \
764 long __cu_len; \
765 \
766 __cu_to = (to); \
767 __cu_from = (from); \
768 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100769 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
770 __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000771 __cu_len; \
772})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774/*
775 * copy_to_user: - Copy a block of data into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100776 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100778 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Ralf Baechle70342282013-01-22 12:59:30 +0100780 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *
782 * Copy data from kernel space to user space.
783 *
784 * Returns number of bytes that could not be copied.
785 * On success, this will be zero.
786 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100787#define copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000789 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 const void *__cu_from; \
791 long __cu_len; \
792 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 __cu_to = (to); \
794 __cu_from = (from); \
795 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200796 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
797 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100799 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200800 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 __cu_len; \
802})
803
Ralf Baechle21a151d2007-10-11 23:46:15 +0100804#define __invoke_copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100806 register void *__cu_to_r __asm__("$4"); \
807 register const void __user *__cu_from_r __asm__("$5"); \
808 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 \
810 __cu_to_r = (to); \
811 __cu_from_r = (from); \
812 __cu_len_r = (n); \
813 __asm__ __volatile__( \
814 ".set\tnoreorder\n\t" \
815 __MODULE_JAL(__copy_user) \
816 ".set\tnoat\n\t" \
817 __UA_ADDU "\t$1, %1, %2\n\t" \
818 ".set\tat\n\t" \
819 ".set\treorder" \
820 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
821 : \
David Daneybb0757e2012-06-06 23:00:31 +0100822 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100823 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 __cu_len_r; \
825})
826
Ralf Baechle21a151d2007-10-11 23:46:15 +0100827#define __invoke_copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000828({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100829 register void *__cu_to_r __asm__("$4"); \
830 register const void __user *__cu_from_r __asm__("$5"); \
831 register long __cu_len_r __asm__("$6"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000832 \
833 __cu_to_r = (to); \
834 __cu_from_r = (from); \
835 __cu_len_r = (n); \
836 __asm__ __volatile__( \
837 ".set\tnoreorder\n\t" \
838 __MODULE_JAL(__copy_user_inatomic) \
839 ".set\tnoat\n\t" \
840 __UA_ADDU "\t$1, %1, %2\n\t" \
841 ".set\tat\n\t" \
842 ".set\treorder" \
843 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
844 : \
David Daneybb0757e2012-06-06 23:00:31 +0100845 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100846 DADDI_SCRATCH, "memory"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000847 __cu_len_r; \
848})
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
Chris Dearman131c1a22007-02-01 19:54:13 +0000851 * __copy_from_user: - Copy a block of data from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100852 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100854 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 *
Ralf Baechle70342282013-01-22 12:59:30 +0100856 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
858 * Copy data from user space to kernel space. Caller must check
859 * the specified block with access_ok() before calling this function.
860 *
861 * Returns number of bytes that could not be copied.
862 * On success, this will be zero.
863 *
864 * If some data could not be copied, this function will pad the copied
865 * data to the requested size using zero bytes.
866 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100867#define __copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868({ \
869 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000870 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 long __cu_len; \
872 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 __cu_to = (to); \
874 __cu_from = (from); \
875 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200876 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100878 __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 __cu_len; \
880})
881
882/*
883 * copy_from_user: - Copy a block of data from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100884 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100886 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *
Ralf Baechle70342282013-01-22 12:59:30 +0100888 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
890 * Copy data from user space to kernel space.
891 *
892 * Returns number of bytes that could not be copied.
893 * On success, this will be zero.
894 *
895 * If some data could not be copied, this function will pad the copied
896 * data to the requested size using zero bytes.
897 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100898#define copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899({ \
900 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000901 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 long __cu_len; \
903 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 __cu_to = (to); \
905 __cu_from = (from); \
906 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200907 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
908 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100910 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200911 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 __cu_len; \
913})
914
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200915#define __copy_in_user(to, from, n) \
916({ \
917 void __user *__cu_to; \
918 const void __user *__cu_from; \
919 long __cu_len; \
920 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200921 __cu_to = (to); \
922 __cu_from = (from); \
923 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200924 might_fault(); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200925 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100926 __cu_len); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200927 __cu_len; \
928})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Ralf Baechle21a151d2007-10-11 23:46:15 +0100930#define copy_in_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000932 void __user *__cu_to; \
933 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 long __cu_len; \
935 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 __cu_to = (to); \
937 __cu_from = (from); \
938 __cu_len = (n); \
939 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
Ralf Baechle70342282013-01-22 12:59:30 +0100940 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
Ralf Baechleef41f462009-04-28 14:17:54 +0200941 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100943 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200944 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 __cu_len; \
946})
947
948/*
949 * __clear_user: - Zero a block of memory in user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100950 * @to: Destination address, in user space.
951 * @n: Number of bytes to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
953 * Zero a block of memory in user space. Caller must check
954 * the specified block with access_ok() before calling this function.
955 *
956 * Returns number of bytes that could not be cleared.
957 * On success, this will be zero.
958 */
959static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000960__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 __kernel_size_t res;
963
Ralf Baechleef41f462009-04-28 14:17:54 +0200964 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 __asm__ __volatile__(
966 "move\t$4, %1\n\t"
967 "move\t$5, $0\n\t"
968 "move\t$6, %2\n\t"
969 __MODULE_JAL(__bzero)
970 "move\t%0, $6"
971 : "=r" (res)
972 : "r" (addr), "r" (size)
973 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
974
975 return res;
976}
977
978#define clear_user(addr,n) \
979({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000980 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned long __cl_size = (n); \
982 if (__cl_size && access_ok(VERIFY_WRITE, \
Wu Zhangjin63d38922009-05-21 05:50:01 +0800983 __cl_addr, __cl_size)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 __cl_size = __clear_user(__cl_addr, __cl_size); \
985 __cl_size; \
986})
987
988/*
989 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
990 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +0100991 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * @src: Source address, in user space.
993 * @count: Maximum number of bytes to copy, including the trailing NUL.
994 *
995 * Copies a NUL-terminated string from userspace to kernel space.
996 * Caller must check the specified block with access_ok() before calling
997 * this function.
998 *
999 * On success, returns the length of the string (not including the trailing
1000 * NUL).
1001 *
1002 * If access to userspace fails, returns -EFAULT (some data may have been
1003 * copied).
1004 *
1005 * If @count is smaller than the length of the string, copies @count bytes
1006 * and returns @count.
1007 */
1008static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001009__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 long res;
1012
Ralf Baechleef41f462009-04-28 14:17:54 +02001013 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 __asm__ __volatile__(
1015 "move\t$4, %1\n\t"
1016 "move\t$5, %2\n\t"
1017 "move\t$6, %3\n\t"
1018 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1019 "move\t%0, $2"
1020 : "=r" (res)
1021 : "r" (__to), "r" (__from), "r" (__len)
1022 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1023
1024 return res;
1025}
1026
1027/*
1028 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1029 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +01001030 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * @src: Source address, in user space.
1032 * @count: Maximum number of bytes to copy, including the trailing NUL.
1033 *
1034 * Copies a NUL-terminated string from userspace to kernel space.
1035 *
1036 * On success, returns the length of the string (not including the trailing
1037 * NUL).
1038 *
1039 * If access to userspace fails, returns -EFAULT (some data may have been
1040 * copied).
1041 *
1042 * If @count is smaller than the length of the string, copies @count bytes
1043 * and returns @count.
1044 */
1045static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001046strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 long res;
1049
Ralf Baechleef41f462009-04-28 14:17:54 +02001050 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 __asm__ __volatile__(
1052 "move\t$4, %1\n\t"
1053 "move\t$5, %2\n\t"
1054 "move\t$6, %3\n\t"
1055 __MODULE_JAL(__strncpy_from_user_asm)
1056 "move\t%0, $2"
1057 : "=r" (res)
1058 : "r" (__to), "r" (__from), "r" (__len)
1059 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1060
1061 return res;
1062}
1063
1064/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001065static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 long res;
1068
Ralf Baechleef41f462009-04-28 14:17:54 +02001069 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 __asm__ __volatile__(
1071 "move\t$4, %1\n\t"
1072 __MODULE_JAL(__strlen_user_nocheck_asm)
1073 "move\t%0, $2"
1074 : "=r" (res)
1075 : "r" (s)
1076 : "$2", "$4", __UA_t0, "$31");
1077
1078 return res;
1079}
1080
1081/*
1082 * strlen_user: - Get the size of a string in user space.
1083 * @str: The string to measure.
1084 *
Ralf Baechle70342282013-01-22 12:59:30 +01001085 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
1087 * Get the size of a NUL-terminated string in user space.
1088 *
1089 * Returns the size of the string INCLUDING the terminating NUL.
1090 * On exception, returns 0.
1091 *
1092 * If there is a limit on the length of a valid string, you may wish to
1093 * consider using strnlen_user() instead.
1094 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001095static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 long res;
1098
Ralf Baechleef41f462009-04-28 14:17:54 +02001099 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 __asm__ __volatile__(
1101 "move\t$4, %1\n\t"
1102 __MODULE_JAL(__strlen_user_asm)
1103 "move\t%0, $2"
1104 : "=r" (res)
1105 : "r" (s)
1106 : "$2", "$4", __UA_t0, "$31");
1107
1108 return res;
1109}
1110
1111/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001112static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 long res;
1115
Ralf Baechleef41f462009-04-28 14:17:54 +02001116 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 __asm__ __volatile__(
1118 "move\t$4, %1\n\t"
1119 "move\t$5, %2\n\t"
1120 __MODULE_JAL(__strnlen_user_nocheck_asm)
1121 "move\t%0, $2"
1122 : "=r" (res)
1123 : "r" (s), "r" (n)
1124 : "$2", "$4", "$5", __UA_t0, "$31");
1125
1126 return res;
1127}
1128
1129/*
1130 * strlen_user: - Get the size of a string in user space.
1131 * @str: The string to measure.
1132 *
Ralf Baechle70342282013-01-22 12:59:30 +01001133 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 *
1135 * Get the size of a NUL-terminated string in user space.
1136 *
1137 * Returns the size of the string INCLUDING the terminating NUL.
1138 * On exception, returns 0.
1139 *
1140 * If there is a limit on the length of a valid string, you may wish to
1141 * consider using strnlen_user() instead.
1142 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001143static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 long res;
1146
Ralf Baechleef41f462009-04-28 14:17:54 +02001147 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 __asm__ __volatile__(
1149 "move\t$4, %1\n\t"
1150 "move\t$5, %2\n\t"
1151 __MODULE_JAL(__strnlen_user_asm)
1152 "move\t%0, $2"
1153 : "=r" (res)
1154 : "r" (s), "r" (n)
1155 : "$2", "$4", "$5", __UA_t0, "$31");
1156
1157 return res;
1158}
1159
1160struct exception_table_entry
1161{
1162 unsigned long insn;
1163 unsigned long nextinsn;
1164};
1165
1166extern int fixup_exception(struct pt_regs *regs);
1167
1168#endif /* _ASM_UACCESS_H */