blob: 1c9edd63dda74e9b1eed703b705ea8acfecc5084 [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
26#define __UA_LIMIT 0x80000000UL
27
28#define __UA_ADDR ".word"
29#define __UA_LA "la"
30#define __UA_ADDU "addu"
31#define __UA_t0 "$8"
32#define __UA_t1 "$9"
33
Ralf Baechle875d43e2005-09-03 15:56:16 -070034#endif /* CONFIG_32BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ralf Baechle875d43e2005-09-03 15:56:16 -070036#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David Daney949e51b2010-10-14 11:32:33 -070038extern u64 __ua_limit;
39
40#define __UA_LIMIT __ua_limit
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define __UA_ADDR ".dword"
43#define __UA_LA "dla"
44#define __UA_ADDU "daddu"
45#define __UA_t0 "$12"
46#define __UA_t1 "$13"
47
Ralf Baechle875d43e2005-09-03 15:56:16 -070048#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * USER_DS is a bitmask that has the bits set that may not be set in a valid
52 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
53 * the arithmetic we're doing only works if the limit is a power of two, so
54 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
55 * address in this range it's the process's problem, not ours :-)
56 */
57
58#define KERNEL_DS ((mm_segment_t) { 0UL })
59#define USER_DS ((mm_segment_t) { __UA_LIMIT })
60
61#define VERIFY_READ 0
62#define VERIFY_WRITE 1
63
64#define get_ds() (KERNEL_DS)
65#define get_fs() (current_thread_info()->addr_limit)
66#define set_fs(x) (current_thread_info()->addr_limit = (x))
67
Ralf Baechle21a151d2007-10-11 23:46:15 +010068#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
71/*
72 * Is a address valid? This does a straighforward calculation rather
73 * than tests.
74 *
75 * Address valid if:
76 * - "addr" doesn't have any high-bits set
77 * - AND "size" doesn't have any high-bits set
78 * - AND "addr+size" doesn't have any high-bits set
79 * - OR we are in kernel mode.
80 *
81 * __ua_size() is a trick to avoid runtime checking of positive constant
82 * sizes; for those we already know at compile time that the size is ok.
83 */
84#define __ua_size(size) \
85 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
86
87/*
88 * access_ok: - Checks if a user space pointer is valid
89 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
Ralf Baechle70342282013-01-22 12:59:30 +010090 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
91 * to write to a block, it is always safe to read from it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * @addr: User space pointer to start of block to check
93 * @size: Size of block to check
94 *
Ralf Baechle70342282013-01-22 12:59:30 +010095 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
97 * Checks if a pointer to a block of memory in user space is valid.
98 *
99 * Returns true (nonzero) if the memory block may be valid, false (zero)
100 * if it is definitely invalid.
101 *
102 * Note that, depending on architecture, this function probably just
103 * checks that the pointer is in the user space range - after calling
104 * this function, memory access functions may still return -EFAULT.
105 */
106
107#define __access_mask get_fs().seg
108
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200109#define __access_ok(addr, size, mask) \
110({ \
111 unsigned long __addr = (unsigned long) (addr); \
112 unsigned long __size = size; \
113 unsigned long __mask = mask; \
114 unsigned long __ok; \
115 \
116 __chk_user_ptr(addr); \
117 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
118 __ua_size(__size))); \
119 __ok == 0; \
Ralf Baechled0aab922009-04-27 15:31:34 +0200120})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#define access_ok(type, addr, size) \
Ralf Baechled0aab922009-04-27 15:31:34 +0200123 likely(__access_ok((addr), (size), __access_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * put_user: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100127 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * @ptr: Destination address, in user space.
129 *
Ralf Baechle70342282013-01-22 12:59:30 +0100130 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
132 * This macro copies a single simple value from kernel space to user
133 * space. It supports simple types like char and int, but not larger
134 * data types like structures or arrays.
135 *
136 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
137 * to the result of dereferencing @ptr.
138 *
139 * Returns zero on success, or -EFAULT on error.
140 */
Ralf Baechle70342282013-01-22 12:59:30 +0100141#define put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100142 __put_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/*
145 * get_user: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100146 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * @ptr: Source address, in user space.
148 *
Ralf Baechle70342282013-01-22 12:59:30 +0100149 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * This macro copies a single simple variable from user space to kernel
152 * space. It supports simple types like char and int, but not larger
153 * data types like structures or arrays.
154 *
155 * @ptr must have pointer-to-simple-variable type, and the result of
156 * dereferencing @ptr must be assignable to @x without a cast.
157 *
158 * Returns zero on success, or -EFAULT on error.
159 * On error, the variable @x is set to zero.
160 */
161#define get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100162 __get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*
165 * __put_user: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100166 * @x: Value to copy to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * @ptr: Destination address, in user space.
168 *
Ralf Baechle70342282013-01-22 12:59:30 +0100169 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * This macro copies a single simple value from kernel space to user
172 * space. It supports simple types like char and int, but not larger
173 * data types like structures or arrays.
174 *
175 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
176 * to the result of dereferencing @ptr.
177 *
178 * Caller must check the pointer with access_ok() before calling this
179 * function.
180 *
181 * Returns zero on success, or -EFAULT on error.
182 */
183#define __put_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100184 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/*
187 * __get_user: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100188 * @x: Variable to store result.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * @ptr: Source address, in user space.
190 *
Ralf Baechle70342282013-01-22 12:59:30 +0100191 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * This macro copies a single simple variable from user space to kernel
194 * space. It supports simple types like char and int, but not larger
195 * data types like structures or arrays.
196 *
197 * @ptr must have pointer-to-simple-variable type, and the result of
198 * dereferencing @ptr must be assignable to @x without a cast.
199 *
200 * Caller must check the pointer with access_ok() before calling this
201 * function.
202 *
203 * Returns zero on success, or -EFAULT on error.
204 * On error, the variable @x is set to zero.
205 */
206#define __get_user(x,ptr) \
Ralf Baechle21a151d2007-10-11 23:46:15 +0100207 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209struct __large_struct { unsigned long buf[100]; };
Ralf Baechlefe00f942005-03-01 19:22:29 +0000210#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/*
213 * Yuck. We need two variants, one for 64bit operation and one
214 * for 32 bit mode and old iron.
215 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000216#ifdef CONFIG_32BIT
217#define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000219#ifdef CONFIG_64BIT
220#define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
221#endif
222
223extern void __get_user_unknown(void);
224
225#define __get_user_common(val, size, ptr) \
226do { \
227 switch (size) { \
228 case 1: __get_user_asm(val, "lb", ptr); break; \
229 case 2: __get_user_asm(val, "lh", ptr); break; \
230 case 4: __get_user_asm(val, "lw", ptr); break; \
231 case 8: __GET_USER_DW(val, ptr); break; \
232 default: __get_user_unknown(); break; \
233 } \
234} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Ralf Baechle21a151d2007-10-11 23:46:15 +0100236#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100238 int __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200240 __chk_user_ptr(ptr); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000241 __get_user_common((x), size, ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 __gu_err; \
243})
244
Ralf Baechle21a151d2007-10-11 23:46:15 +0100245#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246({ \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100247 int __gu_err = -EFAULT; \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900248 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200250 might_fault(); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000251 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
252 __get_user_common((x), size, __gu_ptr); \
253 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 __gu_err; \
255})
256
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000257#define __get_user_asm(val, insn, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000258{ \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000259 long __gu_tmp; \
260 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 __asm__ __volatile__( \
262 "1: " insn " %1, %3 \n" \
263 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500264 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 " .section .fixup,\"ax\" \n" \
266 "3: li %0, %4 \n" \
267 " j 2b \n" \
268 " .previous \n" \
269 " .section __ex_table,\"a\" \n" \
270 " "__UA_ADDR "\t1b, 3b \n" \
271 " .previous \n" \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000272 : "=r" (__gu_err), "=r" (__gu_tmp) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000273 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000274 \
Atsushi Nemoto8ecbbca2006-02-14 15:57:50 +0900275 (val) = (__typeof__(*(addr))) __gu_tmp; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/*
279 * Get a long long 64 using 32 bit registers.
280 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000281#define __get_user_asm_ll32(val, addr) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000282{ \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000283 union { \
284 unsigned long long l; \
285 __typeof__(*(addr)) t; \
286 } __gu_tmp; \
Ralf Baechlecd1fb9e2007-02-12 23:12:38 +0000287 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000289 "1: lw %1, (%3) \n" \
290 "2: lw %D1, 4(%3) \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500291 "3: \n" \
292 " .insn \n" \
293 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000294 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 " move %1, $0 \n" \
296 " move %D1, $0 \n" \
297 " j 3b \n" \
298 " .previous \n" \
299 " .section __ex_table,\"a\" \n" \
300 " " __UA_ADDR " 1b, 4b \n" \
301 " " __UA_ADDR " 2b, 4b \n" \
302 " .previous \n" \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000303 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000304 : "0" (0), "r" (addr), "i" (-EFAULT)); \
Ralf Baechlecb66fb32007-02-13 11:45:24 +0000305 \
306 (val) = __gu_tmp.t; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * Yuck. We need two variants, one for 64bit operation and one
311 * for 32 bit mode and old iron.
312 */
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000313#ifdef CONFIG_32BIT
Ralf Baechlefe00f942005-03-01 19:22:29 +0000314#define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
Ralf Baechle4feb8f82006-01-23 16:15:30 +0000316#ifdef CONFIG_64BIT
317#define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
318#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ralf Baechle21a151d2007-10-11 23:46:15 +0100320#define __put_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321({ \
322 __typeof__(*(ptr)) __pu_val; \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100323 int __pu_err = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200325 __chk_user_ptr(ptr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 __pu_val = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000328 case 1: __put_user_asm("sb", ptr); break; \
329 case 2: __put_user_asm("sh", ptr); break; \
330 case 4: __put_user_asm("sw", ptr); break; \
331 case 8: __PUT_USER_DW(ptr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 default: __put_user_unknown(); break; \
333 } \
334 __pu_err; \
335})
336
Ralf Baechle21a151d2007-10-11 23:46:15 +0100337#define __put_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000339 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
340 __typeof__(*(ptr)) __pu_val = (x); \
Ralf Baechle8d2d91e2008-10-11 16:18:50 +0100341 int __pu_err = -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 \
Ralf Baechleef41f462009-04-28 14:17:54 +0200343 might_fault(); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000344 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 switch (size) { \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000346 case 1: __put_user_asm("sb", __pu_addr); break; \
347 case 2: __put_user_asm("sh", __pu_addr); break; \
348 case 4: __put_user_asm("sw", __pu_addr); break; \
349 case 8: __PUT_USER_DW(__pu_addr); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 default: __put_user_unknown(); break; \
351 } \
352 } \
353 __pu_err; \
354})
355
Ralf Baechlefe00f942005-03-01 19:22:29 +0000356#define __put_user_asm(insn, ptr) \
357{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 __asm__ __volatile__( \
359 "1: " insn " %z2, %3 # __put_user_asm\n" \
360 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500361 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 " .section .fixup,\"ax\" \n" \
363 "3: li %0, %4 \n" \
364 " j 2b \n" \
365 " .previous \n" \
366 " .section __ex_table,\"a\" \n" \
367 " " __UA_ADDR " 1b, 3b \n" \
368 " .previous \n" \
369 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000370 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 "i" (-EFAULT)); \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Ralf Baechlefe00f942005-03-01 19:22:29 +0000374#define __put_user_asm_ll32(ptr) \
375{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 __asm__ __volatile__( \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000377 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
378 "2: sw %D2, 4(%3) \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500380 " .insn \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 " .section .fixup,\"ax\" \n" \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000382 "4: li %0, %4 \n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 " j 3b \n" \
384 " .previous \n" \
385 " .section __ex_table,\"a\" \n" \
386 " " __UA_ADDR " 1b, 4b \n" \
387 " " __UA_ADDR " 2b, 4b \n" \
388 " .previous" \
389 : "=r" (__pu_err) \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000390 : "0" (0), "r" (__pu_val), "r" (ptr), \
391 "i" (-EFAULT)); \
392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394extern void __put_user_unknown(void);
395
396/*
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000397 * put_user_unaligned: - Write a simple value into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100398 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000399 * @ptr: Destination address, in user space.
400 *
Ralf Baechle70342282013-01-22 12:59:30 +0100401 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000402 *
403 * This macro copies a single simple value from kernel space to user
404 * space. It supports simple types like char and int, but not larger
405 * data types like structures or arrays.
406 *
407 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
408 * to the result of dereferencing @ptr.
409 *
410 * Returns zero on success, or -EFAULT on error.
411 */
412#define put_user_unaligned(x,ptr) \
413 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
414
415/*
416 * get_user_unaligned: - Get a simple variable from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100417 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000418 * @ptr: Source address, in user space.
419 *
Ralf Baechle70342282013-01-22 12:59:30 +0100420 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000421 *
422 * This macro copies a single simple variable from user space to kernel
423 * space. It supports simple types like char and int, but not larger
424 * data types like structures or arrays.
425 *
426 * @ptr must have pointer-to-simple-variable type, and the result of
427 * dereferencing @ptr must be assignable to @x without a cast.
428 *
429 * Returns zero on success, or -EFAULT on error.
430 * On error, the variable @x is set to zero.
431 */
432#define get_user_unaligned(x,ptr) \
433 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
434
435/*
436 * __put_user_unaligned: - Write a simple value into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100437 * @x: Value to copy to user space.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000438 * @ptr: Destination address, in user space.
439 *
Ralf Baechle70342282013-01-22 12:59:30 +0100440 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000441 *
442 * This macro copies a single simple value from kernel space to user
443 * space. It supports simple types like char and int, but not larger
444 * data types like structures or arrays.
445 *
446 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
447 * to the result of dereferencing @ptr.
448 *
449 * Caller must check the pointer with access_ok() before calling this
450 * function.
451 *
452 * Returns zero on success, or -EFAULT on error.
453 */
454#define __put_user_unaligned(x,ptr) \
455 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
456
457/*
458 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100459 * @x: Variable to store result.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000460 * @ptr: Source address, in user space.
461 *
Ralf Baechle70342282013-01-22 12:59:30 +0100462 * Context: User context only. This function may sleep.
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000463 *
464 * This macro copies a single simple variable from user space to kernel
465 * space. It supports simple types like char and int, but not larger
466 * data types like structures or arrays.
467 *
468 * @ptr must have pointer-to-simple-variable type, and the result of
469 * dereferencing @ptr must be assignable to @x without a cast.
470 *
471 * Caller must check the pointer with access_ok() before calling this
472 * function.
473 *
474 * Returns zero on success, or -EFAULT on error.
475 * On error, the variable @x is set to zero.
476 */
477#define __get_user_unaligned(x,ptr) \
478 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
479
480/*
481 * Yuck. We need two variants, one for 64bit operation and one
482 * for 32 bit mode and old iron.
483 */
484#ifdef CONFIG_32BIT
485#define __GET_USER_UNALIGNED_DW(val, ptr) \
486 __get_user_unaligned_asm_ll32(val, ptr)
487#endif
488#ifdef CONFIG_64BIT
489#define __GET_USER_UNALIGNED_DW(val, ptr) \
490 __get_user_unaligned_asm(val, "uld", ptr)
491#endif
492
493extern void __get_user_unaligned_unknown(void);
494
495#define __get_user_unaligned_common(val, size, ptr) \
496do { \
497 switch (size) { \
498 case 1: __get_user_asm(val, "lb", ptr); break; \
499 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
500 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
501 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
502 default: __get_user_unaligned_unknown(); break; \
503 } \
504} while (0)
505
506#define __get_user_unaligned_nocheck(x,ptr,size) \
507({ \
508 int __gu_err; \
509 \
510 __get_user_unaligned_common((x), size, ptr); \
511 __gu_err; \
512})
513
514#define __get_user_unaligned_check(x,ptr,size) \
515({ \
516 int __gu_err = -EFAULT; \
517 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
518 \
519 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
520 __get_user_unaligned_common((x), size, __gu_ptr); \
521 \
522 __gu_err; \
523})
524
525#define __get_user_unaligned_asm(val, insn, addr) \
526{ \
527 long __gu_tmp; \
528 \
529 __asm__ __volatile__( \
530 "1: " insn " %1, %3 \n" \
531 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500532 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000533 " .section .fixup,\"ax\" \n" \
534 "3: li %0, %4 \n" \
535 " j 2b \n" \
536 " .previous \n" \
537 " .section __ex_table,\"a\" \n" \
538 " "__UA_ADDR "\t1b, 3b \n" \
539 " "__UA_ADDR "\t1b + 4, 3b \n" \
540 " .previous \n" \
541 : "=r" (__gu_err), "=r" (__gu_tmp) \
542 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
543 \
544 (val) = (__typeof__(*(addr))) __gu_tmp; \
545}
546
547/*
548 * Get a long long 64 using 32 bit registers.
549 */
550#define __get_user_unaligned_asm_ll32(val, addr) \
551{ \
Ralf Baechle70342282013-01-22 12:59:30 +0100552 unsigned long long __gu_tmp; \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000553 \
554 __asm__ __volatile__( \
555 "1: ulw %1, (%3) \n" \
556 "2: ulw %D1, 4(%3) \n" \
557 " move %0, $0 \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500558 "3: \n" \
559 " .insn \n" \
560 " .section .fixup,\"ax\" \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000561 "4: li %0, %4 \n" \
562 " move %1, $0 \n" \
563 " move %D1, $0 \n" \
564 " j 3b \n" \
565 " .previous \n" \
566 " .section __ex_table,\"a\" \n" \
567 " " __UA_ADDR " 1b, 4b \n" \
568 " " __UA_ADDR " 1b + 4, 4b \n" \
569 " " __UA_ADDR " 2b, 4b \n" \
570 " " __UA_ADDR " 2b + 4, 4b \n" \
571 " .previous \n" \
572 : "=r" (__gu_err), "=&r" (__gu_tmp) \
573 : "0" (0), "r" (addr), "i" (-EFAULT)); \
574 (val) = (__typeof__(*(addr))) __gu_tmp; \
575}
576
577/*
578 * Yuck. We need two variants, one for 64bit operation and one
579 * for 32 bit mode and old iron.
580 */
581#ifdef CONFIG_32BIT
582#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
583#endif
584#ifdef CONFIG_64BIT
585#define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
586#endif
587
588#define __put_user_unaligned_nocheck(x,ptr,size) \
589({ \
590 __typeof__(*(ptr)) __pu_val; \
591 int __pu_err = 0; \
592 \
593 __pu_val = (x); \
594 switch (size) { \
595 case 1: __put_user_asm("sb", ptr); break; \
596 case 2: __put_user_unaligned_asm("ush", ptr); break; \
597 case 4: __put_user_unaligned_asm("usw", ptr); break; \
598 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
599 default: __put_user_unaligned_unknown(); break; \
600 } \
601 __pu_err; \
602})
603
604#define __put_user_unaligned_check(x,ptr,size) \
605({ \
606 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
607 __typeof__(*(ptr)) __pu_val = (x); \
608 int __pu_err = -EFAULT; \
609 \
610 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
611 switch (size) { \
612 case 1: __put_user_asm("sb", __pu_addr); break; \
613 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
614 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
615 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
616 default: __put_user_unaligned_unknown(); break; \
617 } \
618 } \
619 __pu_err; \
620})
621
622#define __put_user_unaligned_asm(insn, ptr) \
623{ \
624 __asm__ __volatile__( \
625 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
626 "2: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500627 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000628 " .section .fixup,\"ax\" \n" \
629 "3: li %0, %4 \n" \
630 " j 2b \n" \
631 " .previous \n" \
632 " .section __ex_table,\"a\" \n" \
633 " " __UA_ADDR " 1b, 3b \n" \
634 " .previous \n" \
635 : "=r" (__pu_err) \
636 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
637 "i" (-EFAULT)); \
638}
639
640#define __put_user_unaligned_asm_ll32(ptr) \
641{ \
642 __asm__ __volatile__( \
Ralf Baechle70342282013-01-22 12:59:30 +0100643 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000644 "2: sw %D2, 4(%3) \n" \
645 "3: \n" \
Steven J. Hill1658f912013-03-25 13:22:59 -0500646 " .insn \n" \
Ralf Baechle71ec6cc2006-10-31 02:52:56 +0000647 " .section .fixup,\"ax\" \n" \
648 "4: li %0, %4 \n" \
649 " j 3b \n" \
650 " .previous \n" \
651 " .section __ex_table,\"a\" \n" \
652 " " __UA_ADDR " 1b, 4b \n" \
653 " " __UA_ADDR " 1b + 4, 4b \n" \
654 " " __UA_ADDR " 2b, 4b \n" \
655 " " __UA_ADDR " 2b + 4, 4b \n" \
656 " .previous" \
657 : "=r" (__pu_err) \
658 : "0" (0), "r" (__pu_val), "r" (ptr), \
659 "i" (-EFAULT)); \
660}
661
662extern void __put_user_unaligned_unknown(void);
663
664/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * We're generating jump to subroutines which will be outside the range of
666 * jump instructions
667 */
668#ifdef MODULE
669#define __MODULE_JAL(destination) \
670 ".set\tnoat\n\t" \
Ralf Baechle70342282013-01-22 12:59:30 +0100671 __UA_LA "\t$1, " #destination "\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 "jalr\t$1\n\t" \
673 ".set\tat\n\t"
674#else
675#define __MODULE_JAL(destination) \
676 "jal\t" #destination "\n\t"
677#endif
678
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100679#ifndef CONFIG_CPU_DADDI_WORKAROUNDS
680#define DADDI_SCRATCH "$0"
681#else
682#define DADDI_SCRATCH "$3"
683#endif
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685extern size_t __copy_user(void *__to, const void *__from, size_t __n);
686
Ralf Baechle21a151d2007-10-11 23:46:15 +0100687#define __invoke_copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100689 register void __user *__cu_to_r __asm__("$4"); \
690 register const void *__cu_from_r __asm__("$5"); \
691 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 \
693 __cu_to_r = (to); \
694 __cu_from_r = (from); \
695 __cu_len_r = (n); \
696 __asm__ __volatile__( \
697 __MODULE_JAL(__copy_user) \
698 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
699 : \
David Daneybb0757e2012-06-06 23:00:31 +0100700 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100701 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 __cu_len_r; \
703})
704
705/*
706 * __copy_to_user: - Copy a block of data into user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100707 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100709 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *
Ralf Baechle70342282013-01-22 12:59:30 +0100711 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *
713 * Copy data from kernel space to user space. Caller must check
714 * the specified block with access_ok() before calling this function.
715 *
716 * Returns number of bytes that could not be copied.
717 * On success, this will be zero.
718 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100719#define __copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000721 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 const void *__cu_from; \
723 long __cu_len; \
724 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 __cu_to = (to); \
726 __cu_from = (from); \
727 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200728 might_fault(); \
Ralf Baechle70342282013-01-22 12:59:30 +0100729 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 __cu_len; \
731})
732
Ralf Baechled0c91ae2007-03-05 15:54:20 +0000733extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
734
Ralf Baechle21a151d2007-10-11 23:46:15 +0100735#define __copy_to_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000736({ \
737 void __user *__cu_to; \
738 const void *__cu_from; \
739 long __cu_len; \
740 \
741 __cu_to = (to); \
742 __cu_from = (from); \
743 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100744 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000745 __cu_len; \
746})
747
Ralf Baechle21a151d2007-10-11 23:46:15 +0100748#define __copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000749({ \
750 void *__cu_to; \
751 const void __user *__cu_from; \
752 long __cu_len; \
753 \
754 __cu_to = (to); \
755 __cu_from = (from); \
756 __cu_len = (n); \
Ralf Baechle70342282013-01-22 12:59:30 +0100757 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
758 __cu_len); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000759 __cu_len; \
760})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762/*
763 * copy_to_user: - Copy a block of data into user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100764 * @to: Destination address, in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * @from: Source address, in kernel space.
Ralf Baechle70342282013-01-22 12:59:30 +0100766 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
Ralf Baechle70342282013-01-22 12:59:30 +0100768 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *
770 * Copy data from kernel space to user space.
771 *
772 * Returns number of bytes that could not be copied.
773 * On success, this will be zero.
774 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100775#define copy_to_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000777 void __user *__cu_to; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 const void *__cu_from; \
779 long __cu_len; \
780 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 __cu_to = (to); \
782 __cu_from = (from); \
783 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200784 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
785 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100787 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200788 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 __cu_len; \
790})
791
Ralf Baechle21a151d2007-10-11 23:46:15 +0100792#define __invoke_copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100794 register void *__cu_to_r __asm__("$4"); \
795 register const void __user *__cu_from_r __asm__("$5"); \
796 register long __cu_len_r __asm__("$6"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 \
798 __cu_to_r = (to); \
799 __cu_from_r = (from); \
800 __cu_len_r = (n); \
801 __asm__ __volatile__( \
802 ".set\tnoreorder\n\t" \
803 __MODULE_JAL(__copy_user) \
804 ".set\tnoat\n\t" \
805 __UA_ADDU "\t$1, %1, %2\n\t" \
806 ".set\tat\n\t" \
807 ".set\treorder" \
808 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
809 : \
David Daneybb0757e2012-06-06 23:00:31 +0100810 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100811 DADDI_SCRATCH, "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 __cu_len_r; \
813})
814
Ralf Baechle21a151d2007-10-11 23:46:15 +0100815#define __invoke_copy_from_user_inatomic(to, from, n) \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000816({ \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100817 register void *__cu_to_r __asm__("$4"); \
818 register const void __user *__cu_from_r __asm__("$5"); \
819 register long __cu_len_r __asm__("$6"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000820 \
821 __cu_to_r = (to); \
822 __cu_from_r = (from); \
823 __cu_len_r = (n); \
824 __asm__ __volatile__( \
825 ".set\tnoreorder\n\t" \
826 __MODULE_JAL(__copy_user_inatomic) \
827 ".set\tnoat\n\t" \
828 __UA_ADDU "\t$1, %1, %2\n\t" \
829 ".set\tat\n\t" \
830 ".set\treorder" \
831 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
832 : \
David Daneybb0757e2012-06-06 23:00:31 +0100833 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100834 DADDI_SCRATCH, "memory"); \
Ralf Baechlee03b5262007-02-19 16:59:24 +0000835 __cu_len_r; \
836})
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
Chris Dearman131c1a22007-02-01 19:54:13 +0000839 * __copy_from_user: - Copy a block of data from user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100840 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100842 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 *
Ralf Baechle70342282013-01-22 12:59:30 +0100844 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 *
846 * Copy data from user space to kernel space. Caller must check
847 * the specified block with access_ok() before calling this function.
848 *
849 * Returns number of bytes that could not be copied.
850 * On success, this will be zero.
851 *
852 * If some data could not be copied, this function will pad the copied
853 * data to the requested size using zero bytes.
854 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100855#define __copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856({ \
857 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000858 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 long __cu_len; \
860 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 __cu_to = (to); \
862 __cu_from = (from); \
863 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200864 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100866 __cu_len); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 __cu_len; \
868})
869
870/*
871 * copy_from_user: - Copy a block of data from user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100872 * @to: Destination address, in kernel space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * @from: Source address, in user space.
Ralf Baechle70342282013-01-22 12:59:30 +0100874 * @n: Number of bytes to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Ralf Baechle70342282013-01-22 12:59:30 +0100876 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *
878 * Copy data from user space to kernel space.
879 *
880 * Returns number of bytes that could not be copied.
881 * On success, this will be zero.
882 *
883 * If some data could not be copied, this function will pad the copied
884 * data to the requested size using zero bytes.
885 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100886#define copy_from_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887({ \
888 void *__cu_to; \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000889 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 long __cu_len; \
891 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 __cu_to = (to); \
893 __cu_from = (from); \
894 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200895 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
896 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100898 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200899 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 __cu_len; \
901})
902
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200903#define __copy_in_user(to, from, n) \
904({ \
905 void __user *__cu_to; \
906 const void __user *__cu_from; \
907 long __cu_len; \
908 \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200909 __cu_to = (to); \
910 __cu_from = (from); \
911 __cu_len = (n); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200912 might_fault(); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200913 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100914 __cu_len); \
Ralf Baechleed01b3d2009-04-27 16:46:21 +0200915 __cu_len; \
916})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Ralf Baechle21a151d2007-10-11 23:46:15 +0100918#define copy_in_user(to, from, n) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000920 void __user *__cu_to; \
921 const void __user *__cu_from; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 long __cu_len; \
923 \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 __cu_to = (to); \
925 __cu_from = (from); \
926 __cu_len = (n); \
927 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
Ralf Baechle70342282013-01-22 12:59:30 +0100928 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
Ralf Baechleef41f462009-04-28 14:17:54 +0200929 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
Ralf Baechle70342282013-01-22 12:59:30 +0100931 __cu_len); \
Ralf Baechleef41f462009-04-28 14:17:54 +0200932 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 __cu_len; \
934})
935
936/*
937 * __clear_user: - Zero a block of memory in user space, with less checking.
Ralf Baechle70342282013-01-22 12:59:30 +0100938 * @to: Destination address, in user space.
939 * @n: Number of bytes to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
941 * Zero a block of memory in user space. Caller must check
942 * the specified block with access_ok() before calling this function.
943 *
944 * Returns number of bytes that could not be cleared.
945 * On success, this will be zero.
946 */
947static inline __kernel_size_t
Ralf Baechlefe00f942005-03-01 19:22:29 +0000948__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 __kernel_size_t res;
951
Ralf Baechleef41f462009-04-28 14:17:54 +0200952 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 __asm__ __volatile__(
954 "move\t$4, %1\n\t"
955 "move\t$5, $0\n\t"
956 "move\t$6, %2\n\t"
957 __MODULE_JAL(__bzero)
958 "move\t%0, $6"
959 : "=r" (res)
960 : "r" (addr), "r" (size)
961 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
962
963 return res;
964}
965
966#define clear_user(addr,n) \
967({ \
Ralf Baechlefe00f942005-03-01 19:22:29 +0000968 void __user * __cl_addr = (addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned long __cl_size = (n); \
970 if (__cl_size && access_ok(VERIFY_WRITE, \
Wu Zhangjin63d38922009-05-21 05:50:01 +0800971 __cl_addr, __cl_size)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 __cl_size = __clear_user(__cl_addr, __cl_size); \
973 __cl_size; \
974})
975
976/*
977 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
978 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +0100979 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * @src: Source address, in user space.
981 * @count: Maximum number of bytes to copy, including the trailing NUL.
982 *
983 * Copies a NUL-terminated string from userspace to kernel space.
984 * Caller must check the specified block with access_ok() before calling
985 * this function.
986 *
987 * On success, returns the length of the string (not including the trailing
988 * NUL).
989 *
990 * If access to userspace fails, returns -EFAULT (some data may have been
991 * copied).
992 *
993 * If @count is smaller than the length of the string, copies @count bytes
994 * and returns @count.
995 */
996static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +0000997__strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 long res;
1000
Ralf Baechleef41f462009-04-28 14:17:54 +02001001 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 __asm__ __volatile__(
1003 "move\t$4, %1\n\t"
1004 "move\t$5, %2\n\t"
1005 "move\t$6, %3\n\t"
1006 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1007 "move\t%0, $2"
1008 : "=r" (res)
1009 : "r" (__to), "r" (__from), "r" (__len)
1010 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1011
1012 return res;
1013}
1014
1015/*
1016 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1017 * @dst: Destination address, in kernel space. This buffer must be at
Ralf Baechle70342282013-01-22 12:59:30 +01001018 * least @count bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * @src: Source address, in user space.
1020 * @count: Maximum number of bytes to copy, including the trailing NUL.
1021 *
1022 * Copies a NUL-terminated string from userspace to kernel space.
1023 *
1024 * On success, returns the length of the string (not including the trailing
1025 * NUL).
1026 *
1027 * If access to userspace fails, returns -EFAULT (some data may have been
1028 * copied).
1029 *
1030 * If @count is smaller than the length of the string, copies @count bytes
1031 * and returns @count.
1032 */
1033static inline long
Ralf Baechlefe00f942005-03-01 19:22:29 +00001034strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 long res;
1037
Ralf Baechleef41f462009-04-28 14:17:54 +02001038 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 __asm__ __volatile__(
1040 "move\t$4, %1\n\t"
1041 "move\t$5, %2\n\t"
1042 "move\t$6, %3\n\t"
1043 __MODULE_JAL(__strncpy_from_user_asm)
1044 "move\t%0, $2"
1045 : "=r" (res)
1046 : "r" (__to), "r" (__from), "r" (__len)
1047 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1048
1049 return res;
1050}
1051
1052/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001053static inline long __strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 long res;
1056
Ralf Baechleef41f462009-04-28 14:17:54 +02001057 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 __asm__ __volatile__(
1059 "move\t$4, %1\n\t"
1060 __MODULE_JAL(__strlen_user_nocheck_asm)
1061 "move\t%0, $2"
1062 : "=r" (res)
1063 : "r" (s)
1064 : "$2", "$4", __UA_t0, "$31");
1065
1066 return res;
1067}
1068
1069/*
1070 * strlen_user: - Get the size of a string in user space.
1071 * @str: The string to measure.
1072 *
Ralf Baechle70342282013-01-22 12:59:30 +01001073 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Get the size of a NUL-terminated string in user space.
1076 *
1077 * Returns the size of the string INCLUDING the terminating NUL.
1078 * On exception, returns 0.
1079 *
1080 * If there is a limit on the length of a valid string, you may wish to
1081 * consider using strnlen_user() instead.
1082 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001083static inline long strlen_user(const char __user *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 long res;
1086
Ralf Baechleef41f462009-04-28 14:17:54 +02001087 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 __asm__ __volatile__(
1089 "move\t$4, %1\n\t"
1090 __MODULE_JAL(__strlen_user_asm)
1091 "move\t%0, $2"
1092 : "=r" (res)
1093 : "r" (s)
1094 : "$2", "$4", __UA_t0, "$31");
1095
1096 return res;
1097}
1098
1099/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001100static inline long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 long res;
1103
Ralf Baechleef41f462009-04-28 14:17:54 +02001104 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 __asm__ __volatile__(
1106 "move\t$4, %1\n\t"
1107 "move\t$5, %2\n\t"
1108 __MODULE_JAL(__strnlen_user_nocheck_asm)
1109 "move\t%0, $2"
1110 : "=r" (res)
1111 : "r" (s), "r" (n)
1112 : "$2", "$4", "$5", __UA_t0, "$31");
1113
1114 return res;
1115}
1116
1117/*
1118 * strlen_user: - Get the size of a string in user space.
1119 * @str: The string to measure.
1120 *
Ralf Baechle70342282013-01-22 12:59:30 +01001121 * Context: User context only. This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
1123 * Get the size of a NUL-terminated string in user space.
1124 *
1125 * Returns the size of the string INCLUDING the terminating NUL.
1126 * On exception, returns 0.
1127 *
1128 * If there is a limit on the length of a valid string, you may wish to
1129 * consider using strnlen_user() instead.
1130 */
Ralf Baechlefe00f942005-03-01 19:22:29 +00001131static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 long res;
1134
Ralf Baechleef41f462009-04-28 14:17:54 +02001135 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 __asm__ __volatile__(
1137 "move\t$4, %1\n\t"
1138 "move\t$5, %2\n\t"
1139 __MODULE_JAL(__strnlen_user_asm)
1140 "move\t%0, $2"
1141 : "=r" (res)
1142 : "r" (s), "r" (n)
1143 : "$2", "$4", "$5", __UA_t0, "$31");
1144
1145 return res;
1146}
1147
1148struct exception_table_entry
1149{
1150 unsigned long insn;
1151 unsigned long nextinsn;
1152};
1153
1154extern int fixup_exception(struct pt_regs *regs);
1155
1156#endif /* _ASM_UACCESS_H */