uaccess.h 22.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8
 * Copyright (C) 2007  Maciej W. Rozycki
9
 * Copyright (C) 2014, Imagination Technologies Ltd.
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14
 */
#ifndef _ASM_UACCESS_H
#define _ASM_UACCESS_H

#include <linux/kernel.h>
15
#include <linux/string.h>
16
#include <asm/asm-eva.h>
17
#include <asm/extable.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22 23 24 25

/*
 * The fs value determines whether argument validity checking should be
 * performed or not.  If get_fs() == USER_DS, checking is performed, with
 * get_fs() == KERNEL_DS, checking is bypassed.
 *
 * For historical reasons, these macros are grossly misnamed.
 */
26
#ifdef CONFIG_32BIT
Linus Torvalds's avatar
Linus Torvalds committed
27

28
#define __UA_LIMIT 0x80000000UL
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35

#define __UA_ADDR	".word"
#define __UA_LA		"la"
#define __UA_ADDU	"addu"
#define __UA_t0		"$8"
#define __UA_t1		"$9"

36
#endif /* CONFIG_32BIT */
Linus Torvalds's avatar
Linus Torvalds committed
37

38
#ifdef CONFIG_64BIT
Linus Torvalds's avatar
Linus Torvalds committed
39

40 41 42
extern u64 __ua_limit;

#define __UA_LIMIT	__ua_limit
Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46 47 48 49

#define __UA_ADDR	".dword"
#define __UA_LA		"dla"
#define __UA_ADDU	"daddu"
#define __UA_t0		"$12"
#define __UA_t1		"$13"

50
#endif /* CONFIG_64BIT */
Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

/*
 * USER_DS is a bitmask that has the bits set that may not be set in a valid
 * userspace address.  Note that we limit 32-bit userspace to 0x7fff8000 but
 * the arithmetic we're doing only works if the limit is a power of two, so
 * we use 0x80000000 here on 32-bit kernels.  If a process passes an invalid
 * address in this range it's the process's problem, not ours :-)
 */

#define KERNEL_DS	((mm_segment_t) { 0UL })
#define USER_DS		((mm_segment_t) { __UA_LIMIT })

#define get_fs()	(current_thread_info()->addr_limit)
#define set_fs(x)	(current_thread_info()->addr_limit = (x))

66
#define uaccess_kernel()	(get_fs().seg == KERNEL_DS.seg)
Linus Torvalds's avatar
Linus Torvalds committed
67

68 69 70 71 72 73 74 75 76 77
/*
 * eva_kernel_access() - determine whether kernel memory access on an EVA system
 *
 * Determines whether memory accesses should be performed to kernel memory
 * on a system using Extended Virtual Addressing (EVA).
 *
 * Return: true if a kernel memory access on an EVA system, else false.
 */
static inline bool eva_kernel_access(void)
{
78
	if (!IS_ENABLED(CONFIG_EVA))
79 80
		return false;

Al Viro's avatar
Al Viro committed
81
	return uaccess_kernel();
82
}
Linus Torvalds's avatar
Linus Torvalds committed
83 84

/*
85
 * Is a address valid? This does a straightforward calculation rather
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
 * than tests.
 *
 * Address valid if:
 *  - "addr" doesn't have any high-bits set
 *  - AND "size" doesn't have any high-bits set
 *  - AND "addr+size" doesn't have any high-bits set
 *  - OR we are in kernel mode.
 *
 * __ua_size() is a trick to avoid runtime checking of positive constant
 * sizes; for those we already know at compile time that the size is ok.
 */
#define __ua_size(size)							\
	((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))

/*
 * access_ok: - Checks if a user space pointer is valid
 * @addr: User space pointer to start of block to check
 * @size: Size of block to check
 *
105 106
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115 116 117
 *
 * Checks if a pointer to a block of memory in user space is valid.
 *
 * Returns true (nonzero) if the memory block may be valid, false (zero)
 * if it is definitely invalid.
 *
 * Note that, depending on architecture, this function probably just
 * checks that the pointer is in the user space range - after calling
 * this function, memory access functions may still return -EFAULT.
 */

Al Viro's avatar
Al Viro committed
118 119 120 121 122
static inline int __access_ok(const void __user *p, unsigned long size)
{
	unsigned long addr = (unsigned long)p;
	return (get_fs().seg & (addr | (addr + size) | __ua_size(size))) == 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
123

124
#define access_ok(addr, size)					\
Al Viro's avatar
Al Viro committed
125
	likely(__access_ok((addr), (size)))
Linus Torvalds's avatar
Linus Torvalds committed
126 127 128

/*
 * put_user: - Write a simple value into user space.
Ralf Baechle's avatar
Ralf Baechle committed
129
 * @x:	 Value to copy to user space.
Linus Torvalds's avatar
Linus Torvalds committed
130 131
 * @ptr: Destination address, in user space.
 *
132 133
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
134 135 136 137 138 139 140 141 142 143
 *
 * This macro copies a single simple value from kernel space to user
 * space.  It supports simple types like char and int, but not larger
 * data types like structures or arrays.
 *
 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
 * to the result of dereferencing @ptr.
 *
 * Returns zero on success, or -EFAULT on error.
 */
Ralf Baechle's avatar
Ralf Baechle committed
144
#define put_user(x,ptr) \
145
	__put_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds's avatar
Linus Torvalds committed
146 147 148

/*
 * get_user: - Get a simple variable from user space.
Ralf Baechle's avatar
Ralf Baechle committed
149
 * @x:	 Variable to store result.
Linus Torvalds's avatar
Linus Torvalds committed
150 151
 * @ptr: Source address, in user space.
 *
152 153
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
154 155 156 157 158 159 160 161 162 163 164 165
 *
 * This macro copies a single simple variable from user space to kernel
 * space.  It supports simple types like char and int, but not larger
 * data types like structures or arrays.
 *
 * @ptr must have pointer-to-simple-variable type, and the result of
 * dereferencing @ptr must be assignable to @x without a cast.
 *
 * Returns zero on success, or -EFAULT on error.
 * On error, the variable @x is set to zero.
 */
#define get_user(x,ptr) \
166
	__get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds's avatar
Linus Torvalds committed
167 168 169

/*
 * __put_user: - Write a simple value into user space, with less checking.
Ralf Baechle's avatar
Ralf Baechle committed
170
 * @x:	 Value to copy to user space.
Linus Torvalds's avatar
Linus Torvalds committed
171 172
 * @ptr: Destination address, in user space.
 *
173 174
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
 *
 * This macro copies a single simple value from kernel space to user
 * space.  It supports simple types like char and int, but not larger
 * data types like structures or arrays.
 *
 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
 * to the result of dereferencing @ptr.
 *
 * Caller must check the pointer with access_ok() before calling this
 * function.
 *
 * Returns zero on success, or -EFAULT on error.
 */
#define __put_user(x,ptr) \
189
	__put_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192

/*
 * __get_user: - Get a simple variable from user space, with less checking.
Ralf Baechle's avatar
Ralf Baechle committed
193
 * @x:	 Variable to store result.
Linus Torvalds's avatar
Linus Torvalds committed
194 195
 * @ptr: Source address, in user space.
 *
196 197
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
 *
 * This macro copies a single simple variable from user space to kernel
 * space.  It supports simple types like char and int, but not larger
 * data types like structures or arrays.
 *
 * @ptr must have pointer-to-simple-variable type, and the result of
 * dereferencing @ptr must be assignable to @x without a cast.
 *
 * Caller must check the pointer with access_ok() before calling this
 * function.
 *
 * Returns zero on success, or -EFAULT on error.
 * On error, the variable @x is set to zero.
 */
#define __get_user(x,ptr) \
213
	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Linus Torvalds's avatar
Linus Torvalds committed
214 215

struct __large_struct { unsigned long buf[100]; };
Ralf Baechle's avatar
Ralf Baechle committed
216
#define __m(x) (*(struct __large_struct __user *)(x))
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219 220 221

/*
 * Yuck.  We need two variants, one for 64bit operation and one
 * for 32 bit mode and old iron.
 */
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#ifndef CONFIG_EVA
#define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
#else
/*
 * Kernel specific functions for EVA. We need to use normal load instructions
 * to read data from kernel when operating in EVA mode. We use these macros to
 * avoid redefining __get_user_asm for EVA.
 */
#undef _loadd
#undef _loadw
#undef _loadh
#undef _loadb
#ifdef CONFIG_32BIT
#define _loadd			_loadw
#else
#define _loadd(reg, addr)	"ld " reg ", " addr
#endif
#define _loadw(reg, addr)	"lw " reg ", " addr
#define _loadh(reg, addr)	"lh " reg ", " addr
#define _loadb(reg, addr)	"lb " reg ", " addr

#define __get_kernel_common(val, size, ptr)				\
do {									\
	switch (size) {							\
246 247 248 249
	case 1: __get_data_asm(val, _loadb, ptr); break;		\
	case 2: __get_data_asm(val, _loadh, ptr); break;		\
	case 4: __get_data_asm(val, _loadw, ptr); break;		\
	case 8: __GET_DW(val, _loadd, ptr); break;			\
250 251 252 253 254
	default: __get_user_unknown(); break;				\
	}								\
} while (0)
#endif

255
#ifdef CONFIG_32BIT
256
#define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
257 258
#endif
#ifdef CONFIG_64BIT
259
#define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
Linus Torvalds's avatar
Linus Torvalds committed
260 261
#endif

262 263 264 265
extern void __get_user_unknown(void);

#define __get_user_common(val, size, ptr)				\
do {									\
Linus Torvalds's avatar
Linus Torvalds committed
266
	switch (size) {							\
267 268 269 270
	case 1: __get_data_asm(val, user_lb, ptr); break;		\
	case 2: __get_data_asm(val, user_lh, ptr); break;		\
	case 4: __get_data_asm(val, user_lw, ptr); break;		\
	case 8: __GET_DW(val, user_ld, ptr); break;			\
Linus Torvalds's avatar
Linus Torvalds committed
271 272
	default: __get_user_unknown(); break;				\
	}								\
273 274
} while (0)

275
#define __get_user_nocheck(x, ptr, size)				\
276
({									\
277
	int __gu_err;							\
278
									\
279
	if (eva_kernel_access()) {					\
280 281 282 283 284
		__get_kernel_common((x), size, ptr);			\
	} else {							\
		__chk_user_ptr(ptr);					\
		__get_user_common((x), size, ptr);			\
	}								\
Linus Torvalds's avatar
Linus Torvalds committed
285 286 287
	__gu_err;							\
})

288
#define __get_user_check(x, ptr, size)					\
Linus Torvalds's avatar
Linus Torvalds committed
289
({									\
290
	int __gu_err = -EFAULT;						\
291
	const __typeof__(*(ptr)) __user * __gu_ptr = (ptr);		\
292
									\
293
	might_fault();							\
294
	if (likely(access_ok( __gu_ptr, size))) {		\
295
		if (eva_kernel_access())				\
296 297 298
			__get_kernel_common((x), size, __gu_ptr);	\
		else							\
			__get_user_common((x), size, __gu_ptr);		\
299 300
	} else								\
		(x) = 0;						\
Linus Torvalds's avatar
Linus Torvalds committed
301 302 303 304
									\
	__gu_err;							\
})

305
#define __get_data_asm(val, insn, addr)					\
Ralf Baechle's avatar
Ralf Baechle committed
306
{									\
307 308
	long __gu_tmp;							\
									\
Linus Torvalds's avatar
Linus Torvalds committed
309
	__asm__ __volatile__(						\
310
	"1:	"insn("%1", "%3")"				\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
311
	"2:							\n"	\
312
	"	.insn						\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
313 314
	"	.section .fixup,\"ax\"				\n"	\
	"3:	li	%0, %4					\n"	\
315
	"	move	%1, $0					\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320
	"	j	2b					\n"	\
	"	.previous					\n"	\
	"	.section __ex_table,\"a\"			\n"	\
	"	"__UA_ADDR "\t1b, 3b				\n"	\
	"	.previous					\n"	\
321
	: "=r" (__gu_err), "=r" (__gu_tmp)				\
Ralf Baechle's avatar
Ralf Baechle committed
322
	: "0" (0), "o" (__m(addr)), "i" (-EFAULT));			\
323
									\
324
	(val) = (__typeof__(*(addr))) __gu_tmp;				\
Ralf Baechle's avatar
Ralf Baechle committed
325
}
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329

/*
 * Get a long long 64 using 32 bit registers.
 */
330
#define __get_data_asm_ll32(val, insn, addr)				\
Ralf Baechle's avatar
Ralf Baechle committed
331
{									\
332 333 334 335
	union {								\
		unsigned long long	l;				\
		__typeof__(*(addr))	t;				\
	} __gu_tmp;							\
336
									\
Linus Torvalds's avatar
Linus Torvalds committed
337
	__asm__ __volatile__(						\
338 339
	"1:	" insn("%1", "(%3)")"				\n"	\
	"2:	" insn("%D1", "4(%3)")"				\n"	\
340 341 342
	"3:							\n"	\
	"	.insn						\n"	\
	"	.section	.fixup,\"ax\"			\n"	\
Ralf Baechle's avatar
Ralf Baechle committed
343
	"4:	li	%0, %4					\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351
	"	move	%1, $0					\n"	\
	"	move	%D1, $0					\n"	\
	"	j	3b					\n"	\
	"	.previous					\n"	\
	"	.section	__ex_table,\"a\"		\n"	\
	"	" __UA_ADDR "	1b, 4b				\n"	\
	"	" __UA_ADDR "	2b, 4b				\n"	\
	"	.previous					\n"	\
352
	: "=r" (__gu_err), "=&r" (__gu_tmp.l)				\
Ralf Baechle's avatar
Ralf Baechle committed
353
	: "0" (0), "r" (addr), "i" (-EFAULT));				\
354 355
									\
	(val) = __gu_tmp.t;						\
Ralf Baechle's avatar
Ralf Baechle committed
356
}
Linus Torvalds's avatar
Linus Torvalds committed
357

358 359 360 361 362 363 364 365 366 367 368 369
#define HAVE_GET_KERNEL_NOFAULT

#define __get_kernel_nofault(dst, src, type, err_label)			\
do {									\
	int __gu_err;							\
									\
	__get_kernel_common(*((type *)(dst)), sizeof(type),		\
			    (__force type *)(src));			\
	if (unlikely(__gu_err))						\
		goto err_label;						\
} while (0)

370 371 372 373 374 375
#ifndef CONFIG_EVA
#define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
#else
/*
 * Kernel specific functions for EVA. We need to use normal load instructions
 * to read data from kernel when operating in EVA mode. We use these macros to
376
 * avoid redefining __get_data_asm for EVA.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
 */
#undef _stored
#undef _storew
#undef _storeh
#undef _storeb
#ifdef CONFIG_32BIT
#define _stored			_storew
#else
#define _stored(reg, addr)	"ld " reg ", " addr
#endif

#define _storew(reg, addr)	"sw " reg ", " addr
#define _storeh(reg, addr)	"sh " reg ", " addr
#define _storeb(reg, addr)	"sb " reg ", " addr

#define __put_kernel_common(ptr, size)					\
do {									\
	switch (size) {							\
395 396 397 398
	case 1: __put_data_asm(_storeb, ptr); break;			\
	case 2: __put_data_asm(_storeh, ptr); break;			\
	case 4: __put_data_asm(_storew, ptr); break;			\
	case 8: __PUT_DW(_stored, ptr); break;				\
399 400 401 402 403
	default: __put_user_unknown(); break;				\
	}								\
} while(0)
#endif

Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407
/*
 * Yuck.  We need two variants, one for 64bit operation and one
 * for 32 bit mode and old iron.
 */
408
#ifdef CONFIG_32BIT
409
#define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
Linus Torvalds's avatar
Linus Torvalds committed
410
#endif
411
#ifdef CONFIG_64BIT
412
#define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
413
#endif
Linus Torvalds's avatar
Linus Torvalds committed
414

415 416
#define __put_user_common(ptr, size)					\
do {									\
Linus Torvalds's avatar
Linus Torvalds committed
417
	switch (size) {							\
418 419 420 421
	case 1: __put_data_asm(user_sb, ptr); break;			\
	case 2: __put_data_asm(user_sh, ptr); break;			\
	case 4: __put_data_asm(user_sw, ptr); break;			\
	case 8: __PUT_DW(user_sd, ptr); break;				\
Linus Torvalds's avatar
Linus Torvalds committed
422 423
	default: __put_user_unknown(); break;				\
	}								\
424 425 426 427 428 429 430 431
} while (0)

#define __put_user_nocheck(x, ptr, size)				\
({									\
	__typeof__(*(ptr)) __pu_val;					\
	int __pu_err = 0;						\
									\
	__pu_val = (x);							\
432
	if (eva_kernel_access()) {					\
433 434 435 436 437
		__put_kernel_common(ptr, size);				\
	} else {							\
		__chk_user_ptr(ptr);					\
		__put_user_common(ptr, size);				\
	}								\
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440
	__pu_err;							\
})

441
#define __put_user_check(x, ptr, size)					\
Linus Torvalds's avatar
Linus Torvalds committed
442
({									\
Ralf Baechle's avatar
Ralf Baechle committed
443 444
	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
	__typeof__(*(ptr)) __pu_val = (x);				\
445
	int __pu_err = -EFAULT;						\
Linus Torvalds's avatar
Linus Torvalds committed
446
									\
447
	might_fault();							\
448
	if (likely(access_ok( __pu_addr, size))) {	\
449
		if (eva_kernel_access())				\
450 451 452 453
			__put_kernel_common(__pu_addr, size);		\
		else							\
			__put_user_common(__pu_addr, size);		\
	}								\
454
									\
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457
	__pu_err;							\
})

458
#define __put_data_asm(insn, ptr)					\
Ralf Baechle's avatar
Ralf Baechle committed
459
{									\
Linus Torvalds's avatar
Linus Torvalds committed
460
	__asm__ __volatile__(						\
461
	"1:	"insn("%z2", "%3")"	# __put_data_asm	\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
462
	"2:							\n"	\
463
	"	.insn						\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468 469 470 471
	"	.section	.fixup,\"ax\"			\n"	\
	"3:	li	%0, %4					\n"	\
	"	j	2b					\n"	\
	"	.previous					\n"	\
	"	.section	__ex_table,\"a\"		\n"	\
	"	" __UA_ADDR "	1b, 3b				\n"	\
	"	.previous					\n"	\
	: "=r" (__pu_err)						\
Ralf Baechle's avatar
Ralf Baechle committed
472
	: "0" (0), "Jr" (__pu_val), "o" (__m(ptr)),			\
Linus Torvalds's avatar
Linus Torvalds committed
473
	  "i" (-EFAULT));						\
Ralf Baechle's avatar
Ralf Baechle committed
474
}
Linus Torvalds's avatar
Linus Torvalds committed
475

476
#define __put_data_asm_ll32(insn, ptr)					\
Ralf Baechle's avatar
Ralf Baechle committed
477
{									\
Linus Torvalds's avatar
Linus Torvalds committed
478
	__asm__ __volatile__(						\
479
	"1:	"insn("%2", "(%3)")"	# __put_data_asm_ll32	\n"	\
480
	"2:	"insn("%D2", "4(%3)")"				\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
481
	"3:							\n"	\
482
	"	.insn						\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
483
	"	.section	.fixup,\"ax\"			\n"	\
Ralf Baechle's avatar
Ralf Baechle committed
484
	"4:	li	%0, %4					\n"	\
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491
	"	j	3b					\n"	\
	"	.previous					\n"	\
	"	.section	__ex_table,\"a\"		\n"	\
	"	" __UA_ADDR "	1b, 4b				\n"	\
	"	" __UA_ADDR "	2b, 4b				\n"	\
	"	.previous"						\
	: "=r" (__pu_err)						\
Ralf Baechle's avatar
Ralf Baechle committed
492 493 494
	: "0" (0), "r" (__pu_val), "r" (ptr),				\
	  "i" (-EFAULT));						\
}
Linus Torvalds's avatar
Linus Torvalds committed
495 496 497

extern void __put_user_unknown(void);

498 499 500 501 502 503 504 505 506 507 508 509
#define __put_kernel_nofault(dst, src, type, err_label)			\
do {									\
	type __pu_val;					\
	int __pu_err = 0;						\
									\
	__pu_val = *(__force type *)(src);				\
	__put_kernel_common(((type *)(dst)), sizeof(type));		\
	if (unlikely(__pu_err))						\
		goto err_label;						\
} while (0)


Linus Torvalds's avatar
Linus Torvalds committed
510 511 512 513 514 515 516
/*
 * We're generating jump to subroutines which will be outside the range of
 * jump instructions
 */
#ifdef MODULE
#define __MODULE_JAL(destination)					\
	".set\tnoat\n\t"						\
Ralf Baechle's avatar
Ralf Baechle committed
517
	__UA_LA "\t$1, " #destination "\n\t"				\
Linus Torvalds's avatar
Linus Torvalds committed
518 519 520 521 522 523 524
	"jalr\t$1\n\t"							\
	".set\tat\n\t"
#else
#define __MODULE_JAL(destination)					\
	"jal\t" #destination "\n\t"
#endif

525 526
#if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) &&	\
					      defined(CONFIG_CPU_HAS_PREFETCH))
527
#define DADDI_SCRATCH "$3"
528 529
#else
#define DADDI_SCRATCH "$0"
530 531
#endif

Linus Torvalds's avatar
Linus Torvalds committed
532 533
extern size_t __copy_user(void *__to, const void *__from, size_t __n);

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
#define __invoke_copy_from(func, to, from, n)				\
({									\
	register void *__cu_to_r __asm__("$4");				\
	register const void __user *__cu_from_r __asm__("$5");		\
	register long __cu_len_r __asm__("$6");				\
									\
	__cu_to_r = (to);						\
	__cu_from_r = (from);						\
	__cu_len_r = (n);						\
	__asm__ __volatile__(						\
	".set\tnoreorder\n\t"						\
	__MODULE_JAL(func)						\
	".set\tnoat\n\t"						\
	__UA_ADDU "\t$1, %1, %2\n\t"					\
	".set\tat\n\t"							\
	".set\treorder"							\
	: "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)	\
	:								\
	: "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31",	\
	  DADDI_SCRATCH, "memory");					\
	__cu_len_r;							\
})

#define __invoke_copy_to(func, to, from, n)				\
Linus Torvalds's avatar
Linus Torvalds committed
558
({									\
559 560 561
	register void __user *__cu_to_r __asm__("$4");			\
	register const void *__cu_from_r __asm__("$5");			\
	register long __cu_len_r __asm__("$6");				\
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566
									\
	__cu_to_r = (to);						\
	__cu_from_r = (from);						\
	__cu_len_r = (n);						\
	__asm__ __volatile__(						\
567
	__MODULE_JAL(func)						\
Linus Torvalds's avatar
Linus Torvalds committed
568 569
	: "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)	\
	:								\
570
	: "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31",	\
571
	  DADDI_SCRATCH, "memory");					\
Linus Torvalds's avatar
Linus Torvalds committed
572 573 574
	__cu_len_r;							\
})

575 576 577 578 579 580 581 582 583
#define __invoke_copy_from_kernel(to, from, n)				\
	__invoke_copy_from(__copy_user, to, from, n)

#define __invoke_copy_to_kernel(to, from, n)				\
	__invoke_copy_to(__copy_user, to, from, n)

#define ___invoke_copy_in_kernel(to, from, n)				\
	__invoke_copy_from(__copy_user, to, from, n)

584
#ifndef CONFIG_EVA
585 586 587
#define __invoke_copy_from_user(to, from, n)				\
	__invoke_copy_from(__copy_user, to, from, n)

588 589 590
#define __invoke_copy_to_user(to, from, n)				\
	__invoke_copy_to(__copy_user, to, from, n)

591 592
#define ___invoke_copy_in_user(to, from, n)				\
	__invoke_copy_from(__copy_user, to, from, n)
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
#else

/* EVA specific functions */

extern size_t __copy_from_user_eva(void *__to, const void *__from,
				   size_t __n);
extern size_t __copy_to_user_eva(void *__to, const void *__from,
				 size_t __n);
extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);

/*
 * Source or destination address is in userland. We need to go through
 * the TLB
 */
#define __invoke_copy_from_user(to, from, n)				\
	__invoke_copy_from(__copy_from_user_eva, to, from, n)

#define __invoke_copy_to_user(to, from, n)				\
	__invoke_copy_to(__copy_to_user_eva, to, from, n)

#define ___invoke_copy_in_user(to, from, n)				\
	__invoke_copy_from(__copy_in_user_eva, to, from, n)

#endif /* CONFIG_EVA */
618

Al Viro's avatar
Al Viro committed
619 620 621 622 623 624 625 626
static inline unsigned long
raw_copy_to_user(void __user *to, const void *from, unsigned long n)
{
	if (eva_kernel_access())
		return __invoke_copy_to_kernel(to, from, n);
	else
		return __invoke_copy_to_user(to, from, n);
}
Linus Torvalds's avatar
Linus Torvalds committed
627

Al Viro's avatar
Al Viro committed
628 629 630 631 632 633 634 635
static inline unsigned long
raw_copy_from_user(void *to, const void __user *from, unsigned long n)
{
	if (eva_kernel_access())
		return __invoke_copy_from_kernel(to, from, n);
	else
		return __invoke_copy_from_user(to, from, n);
}
Linus Torvalds's avatar
Linus Torvalds committed
636

Al Viro's avatar
Al Viro committed
637 638
#define INLINE_COPY_FROM_USER
#define INLINE_COPY_TO_USER
Linus Torvalds's avatar
Linus Torvalds committed
639

Al Viro's avatar
Al Viro committed
640 641 642 643 644 645 646 647
static inline unsigned long
raw_copy_in_user(void __user*to, const void __user *from, unsigned long n)
{
	if (eva_kernel_access())
		return ___invoke_copy_in_kernel(to, from, n);
	else
		return ___invoke_copy_in_user(to, from,	n);
}
Linus Torvalds's avatar
Linus Torvalds committed
648

Arnd Bergmann's avatar
Arnd Bergmann committed
649 650 651
extern __kernel_size_t __bzero_kernel(void __user *addr, __kernel_size_t size);
extern __kernel_size_t __bzero(void __user *addr, __kernel_size_t size);

Linus Torvalds's avatar
Linus Torvalds committed
652 653
/*
 * __clear_user: - Zero a block of memory in user space, with less checking.
Ralf Baechle's avatar
Ralf Baechle committed
654 655
 * @to:	  Destination address, in user space.
 * @n:	  Number of bytes to zero.
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658 659 660 661 662 663
 *
 * Zero a block of memory in user space.  Caller must check
 * the specified block with access_ok() before calling this function.
 *
 * Returns number of bytes that could not be cleared.
 * On success, this will be zero.
 */
static inline __kernel_size_t
Ralf Baechle's avatar
Ralf Baechle committed
664
__clear_user(void __user *addr, __kernel_size_t size)
Linus Torvalds's avatar
Linus Torvalds committed
665 666 667
{
	__kernel_size_t res;

668 669 670 671 672 673 674
#ifdef CONFIG_CPU_MICROMIPS
/* micromips memset / bzero also clobbers t7 & t8 */
#define bzero_clobbers "$4", "$5", "$6", __UA_t0, __UA_t1, "$15", "$24", "$31"
#else
#define bzero_clobbers "$4", "$5", "$6", __UA_t0, __UA_t1, "$31"
#endif /* CONFIG_CPU_MICROMIPS */

675 676 677 678 679 680 681 682 683
	if (eva_kernel_access()) {
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, $0\n\t"
			"move\t$6, %2\n\t"
			__MODULE_JAL(__bzero_kernel)
			"move\t%0, $6"
			: "=r" (res)
			: "r" (addr), "r" (size)
684
			: bzero_clobbers);
685 686 687 688 689 690 691 692 693 694
	} else {
		might_fault();
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, $0\n\t"
			"move\t$6, %2\n\t"
			__MODULE_JAL(__bzero)
			"move\t%0, $6"
			: "=r" (res)
			: "r" (addr), "r" (size)
695
			: bzero_clobbers);
696
	}
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699 700 701 702

	return res;
}

#define clear_user(addr,n)						\
({									\
Ralf Baechle's avatar
Ralf Baechle committed
703
	void __user * __cl_addr = (addr);				\
Linus Torvalds's avatar
Linus Torvalds committed
704
	unsigned long __cl_size = (n);					\
705
	if (__cl_size && access_ok(__cl_addr, __cl_size))		\
Linus Torvalds's avatar
Linus Torvalds committed
706 707 708 709
		__cl_size = __clear_user(__cl_addr, __cl_size);		\
	__cl_size;							\
})

Arnd Bergmann's avatar
Arnd Bergmann committed
710 711 712
extern long __strncpy_from_kernel_asm(char *__to, const char __user *__from, long __len);
extern long __strncpy_from_user_asm(char *__to, const char __user *__from, long __len);

Linus Torvalds's avatar
Linus Torvalds committed
713 714 715
/*
 * strncpy_from_user: - Copy a NUL terminated string from userspace.
 * @dst:   Destination address, in kernel space.  This buffer must be at
Ralf Baechle's avatar
Ralf Baechle committed
716
 *	   least @count bytes long.
Linus Torvalds's avatar
Linus Torvalds committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
 * @src:   Source address, in user space.
 * @count: Maximum number of bytes to copy, including the trailing NUL.
 *
 * Copies a NUL-terminated string from userspace to kernel space.
 *
 * On success, returns the length of the string (not including the trailing
 * NUL).
 *
 * If access to userspace fails, returns -EFAULT (some data may have been
 * copied).
 *
 * If @count is smaller than the length of the string, copies @count bytes
 * and returns @count.
 */
static inline long
Ralf Baechle's avatar
Ralf Baechle committed
732
strncpy_from_user(char *__to, const char __user *__from, long __len)
Linus Torvalds's avatar
Linus Torvalds committed
733 734 735
{
	long res;

736
	if (eva_kernel_access()) {
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, %2\n\t"
			"move\t$6, %3\n\t"
			__MODULE_JAL(__strncpy_from_kernel_asm)
			"move\t%0, $2"
			: "=r" (res)
			: "r" (__to), "r" (__from), "r" (__len)
			: "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
	} else {
		might_fault();
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, %2\n\t"
			"move\t$6, %3\n\t"
			__MODULE_JAL(__strncpy_from_user_asm)
			"move\t%0, $2"
			: "=r" (res)
			: "r" (__to), "r" (__from), "r" (__len)
			: "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
	}
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760 761

	return res;
}

Arnd Bergmann's avatar
Arnd Bergmann committed
762 763 764
extern long __strnlen_kernel_asm(const char __user *s, long n);
extern long __strnlen_user_asm(const char __user *s, long n);

Linus Torvalds's avatar
Linus Torvalds committed
765
/*
766
 * strnlen_user: - Get the size of a string in user space.
Linus Torvalds's avatar
Linus Torvalds committed
767 768
 * @str: The string to measure.
 *
769 770
 * Context: User context only. This function may sleep if pagefaults are
 *          enabled.
Linus Torvalds's avatar
Linus Torvalds committed
771 772 773 774 775
 *
 * Get the size of a NUL-terminated string in user space.
 *
 * Returns the size of the string INCLUDING the terminating NUL.
 * On exception, returns 0.
776
 * If the string is too long, returns a value greater than @n.
Linus Torvalds's avatar
Linus Torvalds committed
777
 */
Ralf Baechle's avatar
Ralf Baechle committed
778
static inline long strnlen_user(const char __user *s, long n)
Linus Torvalds's avatar
Linus Torvalds committed
779 780 781
{
	long res;

782
	might_fault();
783
	if (eva_kernel_access()) {
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, %2\n\t"
			__MODULE_JAL(__strnlen_kernel_asm)
			"move\t%0, $2"
			: "=r" (res)
			: "r" (s), "r" (n)
			: "$2", "$4", "$5", __UA_t0, "$31");
	} else {
		__asm__ __volatile__(
			"move\t$4, %1\n\t"
			"move\t$5, %2\n\t"
			__MODULE_JAL(__strnlen_user_asm)
			"move\t%0, $2"
			: "=r" (res)
			: "r" (s), "r" (n)
			: "$2", "$4", "$5", __UA_t0, "$31");
	}
Linus Torvalds's avatar
Linus Torvalds committed
802 803 804 805 806

	return res;
}

#endif /* _ASM_UACCESS_H */