blob: 133678ab4eb88cbf213d6516c21be76d06e23a22 [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
Markos Chandrasac1d8592013-12-11 11:25:33 +00009 * Copyright (C) 2014, Imagination Technologies Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#ifndef _ASM_UACCESS_H
12#define _ASM_UACCESS_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/thread_info.h>
Markos Chandrasac1d8592013-12-11 11:25:33 +000017#include <asm/asm-eva.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*
20 * The fs value determines whether argument validity checking should be
21 * performed or not. If get_fs() == USER_DS, checking is performed, with
22 * get_fs() == KERNEL_DS, checking is bypassed.
23 *
24 * For historical reasons, these macros are grossly misnamed.
25 */
Ralf Baechle875d43e2005-09-03 15:56:16 -070026#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Sanjay Lal9843b032012-11-21 18:34:03 -080028#ifdef CONFIG_KVM_GUEST
29#define __UA_LIMIT 0x40000000UL
30#else
31#define __UA_LIMIT 0x80000000UL
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define __UA_ADDR ".word"
35#define __UA_LA "la"
36#define __UA_ADDU "addu"
37#define __UA_t0 "$8"
38#define __UA_t1 "$9"
39
Ralf Baechle875d43e2005-09-03 15:56:16 -070040#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ralf Baechle875d43e2005-09-03 15:56:16 -070042#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Daney949e51b2010-10-14 11:32:33 -070044extern u64 __ua_limit;
45
46#define __UA_LIMIT __ua_limit
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define __UA_ADDR ".dword"
49#define __UA_LA "dla"
50#define __UA_ADDU "daddu"
51#define __UA_t0 "$12"
52#define __UA_t1 "$13"
53
Ralf Baechle875d43e2005-09-03 15:56:16 -070054#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * USER_DS is a bitmask that has the bits set that may not be set in a valid
58 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
59 * the arithmetic we're doing only works if the limit is a power of two, so
60 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
61 * address in this range it's the process's problem, not ours :-)
62 */
63
Sanjay Lal9843b032012-11-21 18:34:03 -080064#ifdef CONFIG_KVM_GUEST
65#define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
66#define USER_DS ((mm_segment_t) { 0xC0000000UL })
67#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define KERNEL_DS ((mm_segment_t) { 0UL })
69#define USER_DS ((mm_segment_t) { __UA_LIMIT })
Sanjay Lal9843b032012-11-21 18:34:03 -080070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define VERIFY_READ 0
73#define VERIFY_WRITE 1
74
75#define get_ds() (KERNEL_DS)
76#define get_fs() (current_thread_info()->addr_limit)
77#define set_fs(x) (current_thread_info()->addr_limit = (x))
78
Ralf Baechle21a151d2007-10-11 23:46:15 +010079#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81
82/*
83 * Is a address valid? This does a straighforward calculation rather
84 * than tests.
85 *
86 * Address valid if:
87 * - "addr" doesn't have any high-bits set
88 * - AND "size" doesn't have any high-bits set
89 * - AND "addr+size" doesn't have any high-bits set
90 * - OR we are in kernel mode.
91 *
92 * __ua_size() is a trick to avoid runtime checking of positive constant
93 * sizes; for those we already know at compile time that the size is ok.
94 */
95#define __ua_size(size) \
96 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
97
98/*
99 * access_ok: - Checks if a user space pointer is valid
100 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
Ralf Baechle70342282013-01-22 12:59:30 +0100101 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
102 * to write to a block, it is always safe to read from it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * @addr: User space pointer to start of block to check
104 * @size: Size of block to check
105 *
Ralf Baechle70342282013-01-22 12:59:30 +0100106 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
108 * Checks if a pointer to a block of memory in user space is valid.
109 *
110 * Returns true (nonzero) if the memory block may be valid, false (zero)
111 * if it is definitely invalid.
112 *
113 * Note that, depending on architecture, this function probably just
114 * checks that the pointer is in the user space range - after calling
115 * this function, memory access functions may still return -EFAULT.
116 */
117
118#define __access_mask get_fs().seg
119
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200120#define __access_ok(addr, size, mask) \
121({ \
122 unsigned long __addr = (unsigned long) (addr); \
123 unsigned long __size = size; \
124 unsigned long __mask = mask; \
125 unsigned long __ok; \
126 \
127 __chk_user_ptr(addr); \
128 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
129 __ua_size(__size))); \
130 __ok == 0; \
Ralf Baechled0aab922009-04-27 15:31:34 +0200131})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#define access_ok(type, addr, size) \
Ralf Baechled0aab922009-04-27 15:31:34 +0200134 likely(__access_ok((addr), (size), __access_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * put_user: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100138 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * @ptr: Destination address, in user space.
140 *
Ralf Baechle70342282013-01-22 12:59:30 +0100141 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
143 * This macro copies a single simple value from kernel space to user
144 * space. It supports simple types like char and int, but not larger
145 * data types like structures or arrays.
146 *
147 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
148 * to the result of dereferencing @ptr.
149 *
150 * Returns zero on success, or -EFAULT on error.
151 */
Ralf Baechle70342282013-01-22 12:59:30 +0100152#define put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100153 __put_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
156 * get_user: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100157 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * @ptr: Source address, in user space.
159 *
Ralf Baechle70342282013-01-22 12:59:30 +0100160 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
162 * This macro copies a single simple variable from user space to kernel
163 * space. It supports simple types like char and int, but not larger
164 * data types like structures or arrays.
165 *
166 * @ptr must have pointer-to-simple-variable type, and the result of
167 * dereferencing @ptr must be assignable to @x without a cast.
168 *
169 * Returns zero on success, or -EFAULT on error.
170 * On error, the variable @x is set to zero.
171 */
172#define get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100173 __get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/*
176 * __put_user: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100177 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * @ptr: Destination address, in user space.
179 *
Ralf Baechle70342282013-01-22 12:59:30 +0100180 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 *
182 * This macro copies a single simple value from kernel space to user
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 @x must be assignable
187 * to the result of dereferencing @ptr.
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 */
194#define __put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100195 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/*
198 * __get_user: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100199 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @ptr: Source address, in user space.
201 *
Ralf Baechle70342282013-01-22 12:59:30 +0100202 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
204 * This macro copies a single simple variable from user space to kernel
205 * space. It supports simple types like char and int, but not larger
206 * data types like structures or arrays.
207 *
208 * @ptr must have pointer-to-simple-variable type, and the result of
209 * dereferencing @ptr must be assignable to @x without a cast.
210 *
211 * Caller must check the pointer with access_ok() before calling this
212 * function.
213 *
214 * Returns zero on success, or -EFAULT on error.
215 * On error, the variable @x is set to zero.
216 */
217#define __get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100218 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000221#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/*
224 * Yuck. We need two variants, one for 64bit operation and one
225 * for 32 bit mode and old iron.
226 */
Markos Chandrasac1d8592013-12-11 11:25:33 +0000227#ifndef CONFIG_EVA
228#define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
229#else
230/*
231 * Kernel specific functions for EVA. We need to use normal load instructions
232 * to read data from kernel when operating in EVA mode. We use these macros to
233 * avoid redefining __get_user_asm for EVA.
234 */
235#undef _loadd
236#undef _loadw
237#undef _loadh
238#undef _loadb
239#ifdef CONFIG_32BIT
240#define _loadd _loadw
241#else
242#define _loadd(reg, addr) "ld " reg ", " addr
243#endif
244#define _loadw(reg, addr) "lw " reg ", " addr
245#define _loadh(reg, addr) "lh " reg ", " addr
246#define _loadb(reg, addr) "lb " reg ", " addr
247
248#define __get_kernel_common(val, size, ptr) \
249do { \
250 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000251 case 1: __get_data_asm(val, _loadb, ptr); break; \
252 case 2: __get_data_asm(val, _loadh, ptr); break; \
253 case 4: __get_data_asm(val, _loadw, ptr); break; \
254 case 8: __GET_DW(val, _loadd, ptr); break; \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000255 default: __get_user_unknown(); break; \
256 } \
257} while (0)
258#endif
259
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000260#ifdef CONFIG_32BIT
Markos Chandras0081ad22014-01-06 12:48:28 +0000261#define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000263#ifdef CONFIG_64BIT
Markos Chandras0081ad22014-01-06 12:48:28 +0000264#define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000265#endif
266
267extern void __get_user_unknown(void);
268
269#define __get_user_common(val, size, ptr) \
270do { \
271 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000272 case 1: __get_data_asm(val, user_lb, ptr); break; \
273 case 2: __get_data_asm(val, user_lh, ptr); break; \
274 case 4: __get_data_asm(val, user_lw, ptr); break; \
275 case 8: __GET_DW(val, user_ld, ptr); break; \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000276 default: __get_user_unknown(); break; \
277 } \
278} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ralf Baechle21a151d2007-10-11 23:46:15 +0100280#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100282 int __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000284 if (segment_eq(get_fs(), get_ds())) { \
285 __get_kernel_common((x), size, ptr); \
286 } else { \
287 __chk_user_ptr(ptr); \
288 __get_user_common((x), size, ptr); \
289 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 __gu_err; \
291})
292
Ralf Baechle21a151d2007-10-11 23:46:15 +0100293#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100295 int __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900296 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200298 might_fault(); \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000299 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) { \
300 if (segment_eq(get_fs(), get_ds())) \
301 __get_kernel_common((x), size, __gu_ptr); \
302 else \
303 __get_user_common((x), size, __gu_ptr); \
Ralf Baechle640465b2014-11-18 18:47:13 +0100304 } else \
305 (x) = 0; \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000306 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 __gu_err; \
308})
309
Markos Chandras0081ad22014-01-06 12:48:28 +0000310#define __get_data_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000311{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000312 long __gu_tmp; \
313 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 __asm__ __volatile__( \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000315 "1: "insn("%1", "%3")" \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500317 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 " .section .fixup,\"ax\" \n" \
319 "3: li %0, %4 \n" \
Ralf Baechle640465b2014-11-18 18:47:13 +0100320 " move %1, $0 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 " j 2b \n" \
322 " .previous \n" \
323 " .section __ex_table,\"a\" \n" \
324 " "__UA_ADDR "\t1b, 3b \n" \
325 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000326 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000327 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000328 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900329 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/*
333 * Get a long long 64 using 32 bit registers.
334 */
Markos Chandras0081ad22014-01-06 12:48:28 +0000335#define __get_data_asm_ll32(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000336{ \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000337 union { \
338 unsigned long long l; \
339 __typeof__(*(addr)) t; \
340 } __gu_tmp; \
Ralf Baechlecd1fb9e2007-02-12 23:12:38 +0000341 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 __asm__ __volatile__( \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000343 "1: " insn("%1", "(%3)")" \n" \
344 "2: " insn("%D1", "4(%3)")" \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500345 "3: \n" \
346 " .insn \n" \
347 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000348 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 " move %1, $0 \n" \
350 " move %D1, $0 \n" \
351 " j 3b \n" \
352 " .previous \n" \
353 " .section __ex_table,\"a\" \n" \
354 " " __UA_ADDR " 1b, 4b \n" \
355 " " __UA_ADDR " 2b, 4b \n" \
356 " .previous \n" \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000357 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000358 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000359 \
360 (val) = __gu_tmp.t; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Markos Chandrasac1d8592013-12-11 11:25:33 +0000363#ifndef CONFIG_EVA
364#define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
365#else
366/*
367 * Kernel specific functions for EVA. We need to use normal load instructions
368 * to read data from kernel when operating in EVA mode. We use these macros to
Markos Chandras0081ad22014-01-06 12:48:28 +0000369 * avoid redefining __get_data_asm for EVA.
Markos Chandrasac1d8592013-12-11 11:25:33 +0000370 */
371#undef _stored
372#undef _storew
373#undef _storeh
374#undef _storeb
375#ifdef CONFIG_32BIT
376#define _stored _storew
377#else
378#define _stored(reg, addr) "ld " reg ", " addr
379#endif
380
381#define _storew(reg, addr) "sw " reg ", " addr
382#define _storeh(reg, addr) "sh " reg ", " addr
383#define _storeb(reg, addr) "sb " reg ", " addr
384
385#define __put_kernel_common(ptr, size) \
386do { \
387 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000388 case 1: __put_data_asm(_storeb, ptr); break; \
389 case 2: __put_data_asm(_storeh, ptr); break; \
390 case 4: __put_data_asm(_storew, ptr); break; \
391 case 8: __PUT_DW(_stored, ptr); break; \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000392 default: __put_user_unknown(); break; \
393 } \
394} while(0)
395#endif
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * Yuck. We need two variants, one for 64bit operation and one
399 * for 32 bit mode and old iron.
400 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000401#ifdef CONFIG_32BIT
Markos Chandras0081ad22014-01-06 12:48:28 +0000402#define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000404#ifdef CONFIG_64BIT
Markos Chandras0081ad22014-01-06 12:48:28 +0000405#define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000408#define __put_user_common(ptr, size) \
409do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000411 case 1: __put_data_asm(user_sb, ptr); break; \
412 case 2: __put_data_asm(user_sh, ptr); break; \
413 case 4: __put_data_asm(user_sw, ptr); break; \
414 case 8: __PUT_DW(user_sd, ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 default: __put_user_unknown(); break; \
416 } \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000417} while (0)
418
419#define __put_user_nocheck(x, ptr, size) \
420({ \
421 __typeof__(*(ptr)) __pu_val; \
422 int __pu_err = 0; \
423 \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000424 __pu_val = (x); \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000425 if (segment_eq(get_fs(), get_ds())) { \
426 __put_kernel_common(ptr, size); \
427 } else { \
428 __chk_user_ptr(ptr); \
429 __put_user_common(ptr, size); \
430 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 __pu_err; \
432})
433
Ralf Baechle21a151d2007-10-11 23:46:15 +0100434#define __put_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000436 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
437 __typeof__(*(ptr)) __pu_val = (x); \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100438 int __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200440 might_fault(); \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000441 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
442 if (segment_eq(get_fs(), get_ds())) \
443 __put_kernel_common(__pu_addr, size); \
444 else \
445 __put_user_common(__pu_addr, size); \
446 } \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000447 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 __pu_err; \
449})
450
Markos Chandras0081ad22014-01-06 12:48:28 +0000451#define __put_data_asm(insn, ptr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000452{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 __asm__ __volatile__( \
Markos Chandras0081ad22014-01-06 12:48:28 +0000454 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500456 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 " .section .fixup,\"ax\" \n" \
458 "3: li %0, %4 \n" \
459 " j 2b \n" \
460 " .previous \n" \
461 " .section __ex_table,\"a\" \n" \
462 " " __UA_ADDR " 1b, 3b \n" \
463 " .previous \n" \
464 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000465 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Markos Chandras0081ad22014-01-06 12:48:28 +0000469#define __put_data_asm_ll32(insn, ptr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000470{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 __asm__ __volatile__( \
Markos Chandras0081ad22014-01-06 12:48:28 +0000472 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
Markos Chandrasac1d8592013-12-11 11:25:33 +0000473 "2: "insn("%D2", "4(%3)")" \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500475 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000477 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 " j 3b \n" \
479 " .previous \n" \
480 " .section __ex_table,\"a\" \n" \
481 " " __UA_ADDR " 1b, 4b \n" \
482 " " __UA_ADDR " 2b, 4b \n" \
483 " .previous" \
484 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000485 : "0" (0), "r" (__pu_val), "r" (ptr), \
486 "i" (-EFAULT)); \
487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489extern void __put_user_unknown(void);
490
491/*
Leonid Yegoshin18e90012013-12-17 15:20:24 +0000492 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
493 * EVA unaligned access is handled in the ADE exception handler.
494 */
495#ifndef CONFIG_EVA
496/*
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000497 * put_user_unaligned: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100498 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000499 * @ptr: Destination address, in user space.
500 *
Ralf Baechle70342282013-01-22 12:59:30 +0100501 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000502 *
503 * This macro copies a single simple value from kernel space to user
504 * space. It supports simple types like char and int, but not larger
505 * data types like structures or arrays.
506 *
507 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
508 * to the result of dereferencing @ptr.
509 *
510 * Returns zero on success, or -EFAULT on error.
511 */
512#define put_user_unaligned(x,ptr) \
513 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
514
515/*
516 * get_user_unaligned: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100517 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000518 * @ptr: Source address, in user space.
519 *
Ralf Baechle70342282013-01-22 12:59:30 +0100520 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000521 *
522 * This macro copies a single simple variable from user space to kernel
523 * space. It supports simple types like char and int, but not larger
524 * data types like structures or arrays.
525 *
526 * @ptr must have pointer-to-simple-variable type, and the result of
527 * dereferencing @ptr must be assignable to @x without a cast.
528 *
529 * Returns zero on success, or -EFAULT on error.
530 * On error, the variable @x is set to zero.
531 */
532#define get_user_unaligned(x,ptr) \
533 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
534
535/*
536 * __put_user_unaligned: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100537 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000538 * @ptr: Destination address, in user space.
539 *
Ralf Baechle70342282013-01-22 12:59:30 +0100540 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000541 *
542 * This macro copies a single simple value from kernel space to user
543 * space. It supports simple types like char and int, but not larger
544 * data types like structures or arrays.
545 *
546 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
547 * to the result of dereferencing @ptr.
548 *
549 * Caller must check the pointer with access_ok() before calling this
550 * function.
551 *
552 * Returns zero on success, or -EFAULT on error.
553 */
554#define __put_user_unaligned(x,ptr) \
555 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
556
557/*
558 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100559 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000560 * @ptr: Source address, in user space.
561 *
Ralf Baechle70342282013-01-22 12:59:30 +0100562 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000563 *
564 * This macro copies a single simple variable from user space to kernel
565 * space. It supports simple types like char and int, but not larger
566 * data types like structures or arrays.
567 *
568 * @ptr must have pointer-to-simple-variable type, and the result of
569 * dereferencing @ptr must be assignable to @x without a cast.
570 *
571 * Caller must check the pointer with access_ok() before calling this
572 * function.
573 *
574 * Returns zero on success, or -EFAULT on error.
575 * On error, the variable @x is set to zero.
576 */
577#define __get_user_unaligned(x,ptr) \
578 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
579
580/*
581 * Yuck. We need two variants, one for 64bit operation and one
582 * for 32 bit mode and old iron.
583 */
584#ifdef CONFIG_32BIT
585#define __GET_USER_UNALIGNED_DW(val, ptr) \
586 __get_user_unaligned_asm_ll32(val, ptr)
587#endif
588#ifdef CONFIG_64BIT
589#define __GET_USER_UNALIGNED_DW(val, ptr) \
590 __get_user_unaligned_asm(val, "uld", ptr)
591#endif
592
593extern void __get_user_unaligned_unknown(void);
594
595#define __get_user_unaligned_common(val, size, ptr) \
596do { \
597 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000598 case 1: __get_data_asm(val, "lb", ptr); break; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000599 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
600 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
601 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
602 default: __get_user_unaligned_unknown(); break; \
603 } \
604} while (0)
605
606#define __get_user_unaligned_nocheck(x,ptr,size) \
607({ \
608 int __gu_err; \
609 \
610 __get_user_unaligned_common((x), size, ptr); \
611 __gu_err; \
612})
613
614#define __get_user_unaligned_check(x,ptr,size) \
615({ \
616 int __gu_err = -EFAULT; \
617 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
618 \
619 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
620 __get_user_unaligned_common((x), size, __gu_ptr); \
621 \
622 __gu_err; \
623})
624
Markos Chandras0081ad22014-01-06 12:48:28 +0000625#define __get_data_unaligned_asm(val, insn, addr) \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000626{ \
627 long __gu_tmp; \
628 \
629 __asm__ __volatile__( \
630 "1: " insn " %1, %3 \n" \
631 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500632 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000633 " .section .fixup,\"ax\" \n" \
634 "3: li %0, %4 \n" \
Ralf Baechle640465b2014-11-18 18:47:13 +0100635 " move %1, $0 \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000636 " j 2b \n" \
637 " .previous \n" \
638 " .section __ex_table,\"a\" \n" \
639 " "__UA_ADDR "\t1b, 3b \n" \
640 " "__UA_ADDR "\t1b + 4, 3b \n" \
641 " .previous \n" \
642 : "=r" (__gu_err), "=r" (__gu_tmp) \
643 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
644 \
645 (val) = (__typeof__(*(addr))) __gu_tmp; \
646}
647
648/*
649 * Get a long long 64 using 32 bit registers.
650 */
651#define __get_user_unaligned_asm_ll32(val, addr) \
652{ \
Ralf Baechle70342282013-01-22 12:59:30 +0100653 unsigned long long __gu_tmp; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000654 \
655 __asm__ __volatile__( \
656 "1: ulw %1, (%3) \n" \
657 "2: ulw %D1, 4(%3) \n" \
658 " move %0, $0 \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500659 "3: \n" \
660 " .insn \n" \
661 " .section .fixup,\"ax\" \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000662 "4: li %0, %4 \n" \
663 " move %1, $0 \n" \
664 " move %D1, $0 \n" \
665 " j 3b \n" \
666 " .previous \n" \
667 " .section __ex_table,\"a\" \n" \
668 " " __UA_ADDR " 1b, 4b \n" \
669 " " __UA_ADDR " 1b + 4, 4b \n" \
670 " " __UA_ADDR " 2b, 4b \n" \
671 " " __UA_ADDR " 2b + 4, 4b \n" \
672 " .previous \n" \
673 : "=r" (__gu_err), "=&r" (__gu_tmp) \
674 : "0" (0), "r" (addr), "i" (-EFAULT)); \
675 (val) = (__typeof__(*(addr))) __gu_tmp; \
676}
677
678/*
679 * Yuck. We need two variants, one for 64bit operation and one
680 * for 32 bit mode and old iron.
681 */
682#ifdef CONFIG_32BIT
683#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
684#endif
685#ifdef CONFIG_64BIT
686#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
687#endif
688
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000689#define __put_user_unaligned_common(ptr, size) \
690do { \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000691 switch (size) { \
Markos Chandras0081ad22014-01-06 12:48:28 +0000692 case 1: __put_data_asm("sb", ptr); break; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000693 case 2: __put_user_unaligned_asm("ush", ptr); break; \
694 case 4: __put_user_unaligned_asm("usw", ptr); break; \
695 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
696 default: __put_user_unaligned_unknown(); break; \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000697} while (0)
698
699#define __put_user_unaligned_nocheck(x,ptr,size) \
700({ \
701 __typeof__(*(ptr)) __pu_val; \
702 int __pu_err = 0; \
703 \
704 __pu_val = (x); \
705 __put_user_unaligned_common(ptr, size); \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000706 __pu_err; \
707})
708
709#define __put_user_unaligned_check(x,ptr,size) \
710({ \
711 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
712 __typeof__(*(ptr)) __pu_val = (x); \
713 int __pu_err = -EFAULT; \
714 \
Markos Chandrasec56b1d2013-12-17 14:42:23 +0000715 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
716 __put_user_unaligned_common(__pu_addr, size); \
717 \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000718 __pu_err; \
719})
720
721#define __put_user_unaligned_asm(insn, ptr) \
722{ \
723 __asm__ __volatile__( \
724 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
725 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500726 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000727 " .section .fixup,\"ax\" \n" \
728 "3: li %0, %4 \n" \
729 " j 2b \n" \
730 " .previous \n" \
731 " .section __ex_table,\"a\" \n" \
732 " " __UA_ADDR " 1b, 3b \n" \
733 " .previous \n" \
734 : "=r" (__pu_err) \
735 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
736 "i" (-EFAULT)); \
737}
738
739#define __put_user_unaligned_asm_ll32(ptr) \
740{ \
741 __asm__ __volatile__( \
Ralf Baechle70342282013-01-22 12:59:30 +0100742 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000743 "2: sw %D2, 4(%3) \n" \
744 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500745 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000746 " .section .fixup,\"ax\" \n" \
747 "4: li %0, %4 \n" \
748 " j 3b \n" \
749 " .previous \n" \
750 " .section __ex_table,\"a\" \n" \
751 " " __UA_ADDR " 1b, 4b \n" \
752 " " __UA_ADDR " 1b + 4, 4b \n" \
753 " " __UA_ADDR " 2b, 4b \n" \
754 " " __UA_ADDR " 2b + 4, 4b \n" \
755 " .previous" \
756 : "=r" (__pu_err) \
757 : "0" (0), "r" (__pu_val), "r" (ptr), \
758 "i" (-EFAULT)); \
759}
760
761extern void __put_user_unaligned_unknown(void);
Leonid Yegoshin18e90012013-12-17 15:20:24 +0000762#endif
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000763
764/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * We're generating jump to subroutines which will be outside the range of
766 * jump instructions
767 */
768#ifdef MODULE
769#define __MODULE_JAL(destination) \
770 ".set\tnoat\n\t" \
Ralf Baechle70342282013-01-22 12:59:30 +0100771 __UA_LA "\t$1, " #destination "\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 "jalr\t$1\n\t" \
773 ".set\tat\n\t"
774#else
775#define __MODULE_JAL(destination) \
776 "jal\t" #destination "\n\t"
777#endif
778
Markos Chandras58563812014-11-17 09:30:23 +0000779#if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) && \
780 defined(CONFIG_CPU_HAS_PREFETCH))
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100781#define DADDI_SCRATCH "$3"
Markos Chandras58563812014-11-17 09:30:23 +0000782#else
783#define DADDI_SCRATCH "$0"
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100784#endif
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786extern size_t __copy_user(void *__to, const void *__from, size_t __n);
787
Markos Chandras05c65162013-12-11 16:47:10 +0000788#ifndef CONFIG_EVA
Ralf Baechle21a151d2007-10-11 23:46:15 +0100789#define __invoke_copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100791 register void __user *__cu_to_r __asm__("$4"); \
792 register const void *__cu_from_r __asm__("$5"); \
793 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 \
795 __cu_to_r = (to); \
796 __cu_from_r = (from); \
797 __cu_len_r = (n); \
798 __asm__ __volatile__( \
799 __MODULE_JAL(__copy_user) \
800 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
801 : \
David Daneybb0757e2012-06-06 23:00:31 +0100802 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100803 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 __cu_len_r; \
805})
806
Markos Chandras05c65162013-12-11 16:47:10 +0000807#define __invoke_copy_to_kernel(to, from, n) \
808 __invoke_copy_to_user(to, from, n)
809
810#endif
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
813 * __copy_to_user: - Copy a block of data into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100814 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100816 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Ralf Baechle70342282013-01-22 12:59:30 +0100818 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * Copy data from kernel space to user space. Caller must check
821 * the specified block with access_ok() before calling this function.
822 *
823 * Returns number of bytes that could not be copied.
824 * On success, this will be zero.
825 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100826#define __copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000828 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 const void *__cu_from; \
830 long __cu_len; \
831 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 __cu_to = (to); \
833 __cu_from = (from); \
834 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200835 might_fault(); \
Markos Chandras05c65162013-12-11 16:47:10 +0000836 if (segment_eq(get_fs(), get_ds())) \
837 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
838 __cu_len); \
839 else \
840 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
841 __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 __cu_len; \
843})
844
Ralf Baechled0c91ae2007-03-05 15:54:20 +0000845extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
846
Ralf Baechle21a151d2007-10-11 23:46:15 +0100847#define __copy_to_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000848({ \
849 void __user *__cu_to; \
850 const void *__cu_from; \
851 long __cu_len; \
852 \
853 __cu_to = (to); \
854 __cu_from = (from); \
855 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +0000856 if (segment_eq(get_fs(), get_ds())) \
857 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
858 __cu_len); \
859 else \
860 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
861 __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000862 __cu_len; \
863})
864
Ralf Baechle21a151d2007-10-11 23:46:15 +0100865#define __copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000866({ \
867 void *__cu_to; \
868 const void __user *__cu_from; \
869 long __cu_len; \
870 \
871 __cu_to = (to); \
872 __cu_from = (from); \
873 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +0000874 if (segment_eq(get_fs(), get_ds())) \
875 __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
876 __cu_from,\
877 __cu_len);\
878 else \
879 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
880 __cu_from, \
881 __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000882 __cu_len; \
883})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885/*
886 * copy_to_user: - Copy a block of data into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100887 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100889 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 *
Ralf Baechle70342282013-01-22 12:59:30 +0100891 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *
893 * Copy data from kernel space to user space.
894 *
895 * Returns number of bytes that could not be copied.
896 * On success, this will be zero.
897 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100898#define copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000900 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 const void *__cu_from; \
902 long __cu_len; \
903 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 __cu_to = (to); \
905 __cu_from = (from); \
906 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +0000907 if (segment_eq(get_fs(), get_ds())) { \
908 __cu_len = __invoke_copy_to_kernel(__cu_to, \
909 __cu_from, \
910 __cu_len); \
911 } else { \
912 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
913 might_fault(); \
914 __cu_len = __invoke_copy_to_user(__cu_to, \
915 __cu_from, \
916 __cu_len); \
917 } \
Ralf Baechleef41f462009-04-28 14:17:54 +0200918 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 __cu_len; \
920})
921
Markos Chandras05c65162013-12-11 16:47:10 +0000922#ifndef CONFIG_EVA
923
Ralf Baechle21a151d2007-10-11 23:46:15 +0100924#define __invoke_copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100926 register void *__cu_to_r __asm__("$4"); \
927 register const void __user *__cu_from_r __asm__("$5"); \
928 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 \
930 __cu_to_r = (to); \
931 __cu_from_r = (from); \
932 __cu_len_r = (n); \
933 __asm__ __volatile__( \
934 ".set\tnoreorder\n\t" \
935 __MODULE_JAL(__copy_user) \
936 ".set\tnoat\n\t" \
937 __UA_ADDU "\t$1, %1, %2\n\t" \
938 ".set\tat\n\t" \
939 ".set\treorder" \
940 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
941 : \
David Daneybb0757e2012-06-06 23:00:31 +0100942 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100943 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 __cu_len_r; \
945})
946
Markos Chandras05c65162013-12-11 16:47:10 +0000947#define __invoke_copy_from_kernel(to, from, n) \
948 __invoke_copy_from_user(to, from, n)
949
950/* For userland <-> userland operations */
951#define ___invoke_copy_in_user(to, from, n) \
952 __invoke_copy_from_user(to, from, n)
953
954/* For kernel <-> kernel operations */
955#define ___invoke_copy_in_kernel(to, from, n) \
956 __invoke_copy_from_user(to, from, n)
957
Ralf Baechle21a151d2007-10-11 23:46:15 +0100958#define __invoke_copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000959({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100960 register void *__cu_to_r __asm__("$4"); \
961 register const void __user *__cu_from_r __asm__("$5"); \
962 register long __cu_len_r __asm__("$6"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000963 \
964 __cu_to_r = (to); \
965 __cu_from_r = (from); \
966 __cu_len_r = (n); \
967 __asm__ __volatile__( \
968 ".set\tnoreorder\n\t" \
969 __MODULE_JAL(__copy_user_inatomic) \
970 ".set\tnoat\n\t" \
971 __UA_ADDU "\t$1, %1, %2\n\t" \
972 ".set\tat\n\t" \
973 ".set\treorder" \
974 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
975 : \
David Daneybb0757e2012-06-06 23:00:31 +0100976 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100977 DADDI_SCRATCH, "memory"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000978 __cu_len_r; \
979})
980
Markos Chandras05c65162013-12-11 16:47:10 +0000981#define __invoke_copy_from_kernel_inatomic(to, from, n) \
982 __invoke_copy_from_user_inatomic(to, from, n) \
983
984#else
985
986/* EVA specific functions */
987
988extern size_t __copy_user_inatomic_eva(void *__to, const void *__from,
989 size_t __n);
990extern size_t __copy_from_user_eva(void *__to, const void *__from,
991 size_t __n);
992extern size_t __copy_to_user_eva(void *__to, const void *__from,
993 size_t __n);
994extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);
995
996#define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
997({ \
998 register void *__cu_to_r __asm__("$4"); \
999 register const void __user *__cu_from_r __asm__("$5"); \
1000 register long __cu_len_r __asm__("$6"); \
1001 \
1002 __cu_to_r = (to); \
1003 __cu_from_r = (from); \
1004 __cu_len_r = (n); \
1005 __asm__ __volatile__( \
1006 ".set\tnoreorder\n\t" \
1007 __MODULE_JAL(func_ptr) \
1008 ".set\tnoat\n\t" \
1009 __UA_ADDU "\t$1, %1, %2\n\t" \
1010 ".set\tat\n\t" \
1011 ".set\treorder" \
1012 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1013 : \
1014 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1015 DADDI_SCRATCH, "memory"); \
1016 __cu_len_r; \
1017})
1018
1019#define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
1020({ \
1021 register void *__cu_to_r __asm__("$4"); \
1022 register const void __user *__cu_from_r __asm__("$5"); \
1023 register long __cu_len_r __asm__("$6"); \
1024 \
1025 __cu_to_r = (to); \
1026 __cu_from_r = (from); \
1027 __cu_len_r = (n); \
1028 __asm__ __volatile__( \
1029 __MODULE_JAL(func_ptr) \
1030 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1031 : \
1032 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1033 DADDI_SCRATCH, "memory"); \
1034 __cu_len_r; \
1035})
1036
1037/*
1038 * Source or destination address is in userland. We need to go through
1039 * the TLB
1040 */
1041#define __invoke_copy_from_user(to, from, n) \
1042 __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
1043
1044#define __invoke_copy_from_user_inatomic(to, from, n) \
1045 __invoke_copy_from_user_eva_generic(to, from, n, \
1046 __copy_user_inatomic_eva)
1047
1048#define __invoke_copy_to_user(to, from, n) \
1049 __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
1050
1051#define ___invoke_copy_in_user(to, from, n) \
1052 __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
1053
1054/*
1055 * Source or destination address in the kernel. We are not going through
1056 * the TLB
1057 */
1058#define __invoke_copy_from_kernel(to, from, n) \
1059 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1060
1061#define __invoke_copy_from_kernel_inatomic(to, from, n) \
1062 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
1063
1064#define __invoke_copy_to_kernel(to, from, n) \
1065 __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
1066
1067#define ___invoke_copy_in_kernel(to, from, n) \
1068 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1069
1070#endif /* CONFIG_EVA */
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
Chris Dearman131c1a22007-02-01 19:54:13 +00001073 * __copy_from_user: - Copy a block of data from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +01001074 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +01001076 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
Ralf Baechle70342282013-01-22 12:59:30 +01001078 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
1080 * Copy data from user space to kernel space. Caller must check
1081 * the specified block with access_ok() before calling this function.
1082 *
1083 * Returns number of bytes that could not be copied.
1084 * On success, this will be zero.
1085 *
1086 * If some data could not be copied, this function will pad the copied
1087 * data to the requested size using zero bytes.
1088 */
Ralf Baechle21a151d2007-10-11 23:46:15 +01001089#define __copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090({ \
1091 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +00001092 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 long __cu_len; \
1094 \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 __cu_to = (to); \
1096 __cu_from = (from); \
1097 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +02001098 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +01001100 __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 __cu_len; \
1102})
1103
1104/*
1105 * copy_from_user: - Copy a block of data from user space.
Ralf Baechle70342282013-01-22 12:59:30 +01001106 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +01001108 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 *
Ralf Baechle70342282013-01-22 12:59:30 +01001110 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
1112 * Copy data from user space to kernel space.
1113 *
1114 * Returns number of bytes that could not be copied.
1115 * On success, this will be zero.
1116 *
1117 * If some data could not be copied, this function will pad the copied
1118 * data to the requested size using zero bytes.
1119 */
Ralf Baechle21a151d2007-10-11 23:46:15 +01001120#define copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121({ \
1122 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +00001123 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 long __cu_len; \
1125 \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 __cu_to = (to); \
1127 __cu_from = (from); \
1128 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +00001129 if (segment_eq(get_fs(), get_ds())) { \
1130 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1131 __cu_from, \
1132 __cu_len); \
1133 } else { \
1134 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
1135 might_fault(); \
1136 __cu_len = __invoke_copy_from_user(__cu_to, \
1137 __cu_from, \
1138 __cu_len); \
1139 } \
Ralf Baechleef41f462009-04-28 14:17:54 +02001140 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 __cu_len; \
1142})
1143
Ralf Baechleed01b3d2009-04-27 16:46:21 +02001144#define __copy_in_user(to, from, n) \
1145({ \
1146 void __user *__cu_to; \
1147 const void __user *__cu_from; \
1148 long __cu_len; \
1149 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +02001150 __cu_to = (to); \
1151 __cu_from = (from); \
1152 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +00001153 if (segment_eq(get_fs(), get_ds())) { \
1154 __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
1155 __cu_len); \
1156 } else { \
1157 might_fault(); \
1158 __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
1159 __cu_len); \
1160 } \
Ralf Baechleed01b3d2009-04-27 16:46:21 +02001161 __cu_len; \
1162})
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Ralf Baechle21a151d2007-10-11 23:46:15 +01001164#define copy_in_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +00001166 void __user *__cu_to; \
1167 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 long __cu_len; \
1169 \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 __cu_to = (to); \
1171 __cu_from = (from); \
1172 __cu_len = (n); \
Markos Chandras05c65162013-12-11 16:47:10 +00001173 if (segment_eq(get_fs(), get_ds())) { \
1174 __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
1175 __cu_len); \
1176 } else { \
1177 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
1178 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
1179 might_fault(); \
1180 __cu_len = ___invoke_copy_in_user(__cu_to, \
1181 __cu_from, \
1182 __cu_len); \
1183 } \
Ralf Baechleef41f462009-04-28 14:17:54 +02001184 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 __cu_len; \
1186})
1187
1188/*
1189 * __clear_user: - Zero a block of memory in user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +01001190 * @to: Destination address, in user space.
1191 * @n: Number of bytes to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Zero a block of memory in user space. Caller must check
1194 * the specified block with access_ok() before calling this function.
1195 *
1196 * Returns number of bytes that could not be cleared.
1197 * On success, this will be zero.
1198 */
1199static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +00001200__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 __kernel_size_t res;
1203
Ralf Baechleef41f462009-04-28 14:17:54 +02001204 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 __asm__ __volatile__(
1206 "move\t$4, %1\n\t"
1207 "move\t$5, $0\n\t"
1208 "move\t$6, %2\n\t"
1209 __MODULE_JAL(__bzero)
1210 "move\t%0, $6"
1211 : "=r" (res)
1212 : "r" (addr), "r" (size)
1213 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
1214
1215 return res;
1216}
1217
1218#define clear_user(addr,n) \
1219({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +00001220 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 unsigned long __cl_size = (n); \
1222 if (__cl_size && access_ok(VERIFY_WRITE, \
Wu Zhangjin63d38922009-05-21 05:50:01 +08001223 __cl_addr, __cl_size)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 __cl_size = __clear_user(__cl_addr, __cl_size); \
1225 __cl_size; \
1226})
1227
1228/*
1229 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1230 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +01001231 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * @src: Source address, in user space.
1233 * @count: Maximum number of bytes to copy, including the trailing NUL.
1234 *
1235 * Copies a NUL-terminated string from userspace to kernel space.
1236 * Caller must check the specified block with access_ok() before calling
1237 * this function.
1238 *
1239 * On success, returns the length of the string (not including the trailing
1240 * NUL).
1241 *
1242 * If access to userspace fails, returns -EFAULT (some data may have been
1243 * copied).
1244 *
1245 * If @count is smaller than the length of the string, copies @count bytes
1246 * and returns @count.
1247 */
1248static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001249__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 long res;
1252
Markos Chandrase3a9b072014-01-03 14:55:02 +00001253 if (segment_eq(get_fs(), get_ds())) {
1254 __asm__ __volatile__(
1255 "move\t$4, %1\n\t"
1256 "move\t$5, %2\n\t"
1257 "move\t$6, %3\n\t"
1258 __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
1259 "move\t%0, $2"
1260 : "=r" (res)
1261 : "r" (__to), "r" (__from), "r" (__len)
1262 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1263 } else {
1264 might_fault();
1265 __asm__ __volatile__(
1266 "move\t$4, %1\n\t"
1267 "move\t$5, %2\n\t"
1268 "move\t$6, %3\n\t"
1269 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1270 "move\t%0, $2"
1271 : "=r" (res)
1272 : "r" (__to), "r" (__from), "r" (__len)
1273 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 return res;
1277}
1278
1279/*
1280 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1281 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +01001282 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 * @src: Source address, in user space.
1284 * @count: Maximum number of bytes to copy, including the trailing NUL.
1285 *
1286 * Copies a NUL-terminated string from userspace to kernel space.
1287 *
1288 * On success, returns the length of the string (not including the trailing
1289 * NUL).
1290 *
1291 * If access to userspace fails, returns -EFAULT (some data may have been
1292 * copied).
1293 *
1294 * If @count is smaller than the length of the string, copies @count bytes
1295 * and returns @count.
1296 */
1297static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001298strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 long res;
1301
Markos Chandrase3a9b072014-01-03 14:55:02 +00001302 if (segment_eq(get_fs(), get_ds())) {
1303 __asm__ __volatile__(
1304 "move\t$4, %1\n\t"
1305 "move\t$5, %2\n\t"
1306 "move\t$6, %3\n\t"
1307 __MODULE_JAL(__strncpy_from_kernel_asm)
1308 "move\t%0, $2"
1309 : "=r" (res)
1310 : "r" (__to), "r" (__from), "r" (__len)
1311 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1312 } else {
1313 might_fault();
1314 __asm__ __volatile__(
1315 "move\t$4, %1\n\t"
1316 "move\t$5, %2\n\t"
1317 "move\t$6, %3\n\t"
1318 __MODULE_JAL(__strncpy_from_user_asm)
1319 "move\t%0, $2"
1320 : "=r" (res)
1321 : "r" (__to), "r" (__from), "r" (__len)
1322 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 return res;
1326}
1327
1328/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001329static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 long res;
1332
Markos Chandrase3a9b072014-01-03 14:55:02 +00001333 if (segment_eq(get_fs(), get_ds())) {
1334 __asm__ __volatile__(
1335 "move\t$4, %1\n\t"
1336 __MODULE_JAL(__strlen_kernel_nocheck_asm)
1337 "move\t%0, $2"
1338 : "=r" (res)
1339 : "r" (s)
1340 : "$2", "$4", __UA_t0, "$31");
1341 } else {
1342 might_fault();
1343 __asm__ __volatile__(
1344 "move\t$4, %1\n\t"
1345 __MODULE_JAL(__strlen_user_nocheck_asm)
1346 "move\t%0, $2"
1347 : "=r" (res)
1348 : "r" (s)
1349 : "$2", "$4", __UA_t0, "$31");
1350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 return res;
1353}
1354
1355/*
1356 * strlen_user: - Get the size of a string in user space.
1357 * @str: The string to measure.
1358 *
Ralf Baechle70342282013-01-22 12:59:30 +01001359 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 *
1361 * Get the size of a NUL-terminated string in user space.
1362 *
1363 * Returns the size of the string INCLUDING the terminating NUL.
1364 * On exception, returns 0.
1365 *
1366 * If there is a limit on the length of a valid string, you may wish to
1367 * consider using strnlen_user() instead.
1368 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001369static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 long res;
1372
Markos Chandrase3a9b072014-01-03 14:55:02 +00001373 if (segment_eq(get_fs(), get_ds())) {
1374 __asm__ __volatile__(
1375 "move\t$4, %1\n\t"
1376 __MODULE_JAL(__strlen_kernel_asm)
1377 "move\t%0, $2"
1378 : "=r" (res)
1379 : "r" (s)
1380 : "$2", "$4", __UA_t0, "$31");
1381 } else {
1382 might_fault();
1383 __asm__ __volatile__(
1384 "move\t$4, %1\n\t"
1385 __MODULE_JAL(__strlen_kernel_asm)
1386 "move\t%0, $2"
1387 : "=r" (res)
1388 : "r" (s)
1389 : "$2", "$4", __UA_t0, "$31");
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 return res;
1393}
1394
1395/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001396static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 long res;
1399
Markos Chandrase3a9b072014-01-03 14:55:02 +00001400 if (segment_eq(get_fs(), get_ds())) {
1401 __asm__ __volatile__(
1402 "move\t$4, %1\n\t"
1403 "move\t$5, %2\n\t"
1404 __MODULE_JAL(__strnlen_kernel_nocheck_asm)
1405 "move\t%0, $2"
1406 : "=r" (res)
1407 : "r" (s), "r" (n)
1408 : "$2", "$4", "$5", __UA_t0, "$31");
1409 } else {
1410 might_fault();
1411 __asm__ __volatile__(
1412 "move\t$4, %1\n\t"
1413 "move\t$5, %2\n\t"
1414 __MODULE_JAL(__strnlen_user_nocheck_asm)
1415 "move\t%0, $2"
1416 : "=r" (res)
1417 : "r" (s), "r" (n)
1418 : "$2", "$4", "$5", __UA_t0, "$31");
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 return res;
1422}
1423
1424/*
1425 * strlen_user: - Get the size of a string in user space.
1426 * @str: The string to measure.
1427 *
Ralf Baechle70342282013-01-22 12:59:30 +01001428 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 *
1430 * Get the size of a NUL-terminated string in user space.
1431 *
1432 * Returns the size of the string INCLUDING the terminating NUL.
1433 * On exception, returns 0.
1434 *
1435 * If there is a limit on the length of a valid string, you may wish to
1436 * consider using strnlen_user() instead.
1437 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001438static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 long res;
1441
Ralf Baechleef41f462009-04-28 14:17:54 +02001442 might_fault();
Markos Chandrase3a9b072014-01-03 14:55:02 +00001443 if (segment_eq(get_fs(), get_ds())) {
1444 __asm__ __volatile__(
1445 "move\t$4, %1\n\t"
1446 "move\t$5, %2\n\t"
1447 __MODULE_JAL(__strnlen_kernel_asm)
1448 "move\t%0, $2"
1449 : "=r" (res)
1450 : "r" (s), "r" (n)
1451 : "$2", "$4", "$5", __UA_t0, "$31");
1452 } else {
1453 __asm__ __volatile__(
1454 "move\t$4, %1\n\t"
1455 "move\t$5, %2\n\t"
1456 __MODULE_JAL(__strnlen_user_asm)
1457 "move\t%0, $2"
1458 : "=r" (res)
1459 : "r" (s), "r" (n)
1460 : "$2", "$4", "$5", __UA_t0, "$31");
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 return res;
1464}
1465
1466struct exception_table_entry
1467{
1468 unsigned long insn;
1469 unsigned long nextinsn;
1470};
1471
1472extern int fixup_exception(struct pt_regs *regs);
1473
1474#endif /* _ASM_UACCESS_H */