Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 *  arch/arm/include/asm/uaccess.h
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License version 2 as
  6 * published by the Free Software Foundation.
  7 */
  8#ifndef _ASMARM_UACCESS_H
  9#define _ASMARM_UACCESS_H
 10
 11/*
 12 * User space memory access functions
 13 */
 
 14#include <linux/string.h>
 15#include <linux/thread_info.h>
 16#include <asm/errno.h>
 17#include <asm/memory.h>
 18#include <asm/domain.h>
 19#include <asm/system.h>
 20#include <asm/unified.h>
 
 21
 22#define VERIFY_READ 0
 23#define VERIFY_WRITE 1
 24
 25/*
 26 * The exception table consists of pairs of addresses: the first is the
 27 * address of an instruction that is allowed to fault, and the second is
 28 * the address at which the program should continue.  No registers are
 29 * modified, so it is entirely up to the continuation code to figure out
 30 * what to do.
 31 *
 32 * All the routines below use bits of fixup code that are out of line
 33 * with the main instruction path.  This means when everything is well,
 34 * we don't even have to jump over them.  Further, they do not intrude
 35 * on our cache or tlb entries.
 36 */
 37
 38struct exception_table_entry
 39{
 40	unsigned long insn, fixup;
 41};
 42
 43extern int fixup_exception(struct pt_regs *regs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44
 45/*
 46 * These two are intentionally not defined anywhere - if the kernel
 47 * code generates any references to them, that's a bug.
 48 */
 49extern int __get_user_bad(void);
 50extern int __put_user_bad(void);
 51
 52/*
 53 * Note that this is actually 0x1,0000,0000
 54 */
 55#define KERNEL_DS	0x00000000
 56#define get_ds()	(KERNEL_DS)
 57
 58#ifdef CONFIG_MMU
 59
 60#define USER_DS		TASK_SIZE
 61#define get_fs()	(current_thread_info()->addr_limit)
 
 
 
 
 62
 63static inline void set_fs(mm_segment_t fs)
 
 
 
 
 
 
 
 64{
 65	current_thread_info()->addr_limit = fs;
 66	modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
 67}
 68
 69#define segment_eq(a,b)	((a) == (b))
 
 
 
 
 
 
 
 
 
 70
 71#define __addr_ok(addr) ({ \
 72	unsigned long flag; \
 73	__asm__("cmp %2, %0; movlo %0, #0" \
 74		: "=&r" (flag) \
 75		: "0" (current_thread_info()->addr_limit), "r" (addr) \
 76		: "cc"); \
 77	(flag == 0); })
 78
 79/* We use 33-bit arithmetic here... */
 80#define __range_ok(addr,size) ({ \
 81	unsigned long flag, roksum; \
 82	__chk_user_ptr(addr);	\
 83	__asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
 84		: "=&r" (flag), "=&r" (roksum) \
 85		: "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
 86		: "cc"); \
 87	flag; })
 88
 89/*
 90 * Single-value transfer routines.  They automatically use the right
 91 * size if we just have the right pointer type.  Note that the functions
 92 * which read from user space (*get_*) need to take care not to leak
 93 * kernel data even if the calling code is buggy and fails to check
 94 * the return value.  This means zeroing out the destination variable
 95 * or buffer on error.  Normally this is done out of line by the
 96 * fixup code, but there are a few places where it intrudes on the
 97 * main code path.  When we only write to user space, there is no
 98 * problem.
 99 */
100extern int __get_user_1(void *);
101extern int __get_user_2(void *);
102extern int __get_user_4(void *);
 
 
 
 
 
103
104#define __get_user_x(__r2,__p,__e,__s,__i...)				\
105	   __asm__ __volatile__ (					\
106		__asmeq("%0", "r0") __asmeq("%1", "r2")			\
 
107		"bl	__get_user_" #__s				\
108		: "=&r" (__e), "=r" (__r2)				\
109		: "0" (__p)						\
110		: __i, "cc")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
112#define get_user(x,p)							\
 
113	({								\
114		register const typeof(*(p)) __user *__p asm("r0") = (p);\
115		register unsigned long __r2 asm("r2");			\
 
 
116		register int __e asm("r0");				\
 
 
117		switch (sizeof(*(__p))) {				\
118		case 1:							\
119			__get_user_x(__r2, __p, __e, 1, "lr");		\
120	       		break;						\
 
 
 
121		case 2:							\
122			__get_user_x(__r2, __p, __e, 2, "r3", "lr");	\
 
 
 
123			break;						\
124		case 4:							\
125	       		__get_user_x(__r2, __p, __e, 4, "lr");		\
 
 
 
 
 
 
 
 
 
126			break;						\
127		default: __e = __get_user_bad(); break;			\
128		}							\
 
 
129		x = (typeof(*(p))) __r2;				\
130		__e;							\
131	})
132
 
 
 
 
 
 
133extern int __put_user_1(void *, unsigned int);
134extern int __put_user_2(void *, unsigned int);
135extern int __put_user_4(void *, unsigned int);
136extern int __put_user_8(void *, unsigned long long);
137
138#define __put_user_x(__r2,__p,__e,__s)					\
139	   __asm__ __volatile__ (					\
140		__asmeq("%0", "r0") __asmeq("%2", "r2")			\
141		"bl	__put_user_" #__s				\
142		: "=&r" (__e)						\
143		: "0" (__p), "r" (__r2)					\
144		: "ip", "lr", "cc")
145
146#define put_user(x,p)							\
147	({								\
148		register const typeof(*(p)) __r2 asm("r2") = (x);	\
149		register const typeof(*(p)) __user *__p asm("r0") = (p);\
 
 
150		register int __e asm("r0");				\
151		switch (sizeof(*(__p))) {				\
152		case 1:							\
153			__put_user_x(__r2, __p, __e, 1);		\
154			break;						\
155		case 2:							\
156			__put_user_x(__r2, __p, __e, 2);		\
157			break;						\
158		case 4:							\
159			__put_user_x(__r2, __p, __e, 4);		\
160			break;						\
161		case 8:							\
162			__put_user_x(__r2, __p, __e, 8);		\
163			break;						\
164		default: __e = __put_user_bad(); break;			\
165		}							\
166		__e;							\
167	})
168
169#else /* CONFIG_MMU */
170
171/*
172 * uClinux has only one addr space, so has simplified address limits.
173 */
174#define USER_DS			KERNEL_DS
175
176#define segment_eq(a,b)		(1)
177#define __addr_ok(addr)		(1)
178#define __range_ok(addr,size)	(0)
179#define get_fs()		(KERNEL_DS)
180
181static inline void set_fs(mm_segment_t fs)
182{
183}
184
185#define get_user(x,p)	__get_user(x,p)
186#define put_user(x,p)	__put_user(x,p)
187
188#endif /* CONFIG_MMU */
189
190#define access_ok(type,addr,size)	(__range_ok(addr,size) == 0)
 
 
 
 
 
 
 
 
 
 
191
192/*
193 * The "__xxx" versions of the user access functions do not verify the
194 * address space - it must have been done previously with a separate
195 * "access_ok()" call.
196 *
197 * The "xxx_error" versions set the third argument to EFAULT if an
198 * error occurs, and leave it unchanged on success.  Note that these
199 * versions are void (ie, don't return a value as such).
200 */
201#define __get_user(x,ptr)						\
202({									\
203	long __gu_err = 0;						\
204	__get_user_err((x),(ptr),__gu_err);				\
205	__gu_err;							\
206})
207
208#define __get_user_error(x,ptr,err)					\
209({									\
210	__get_user_err((x),(ptr),err);					\
211	(void) 0;							\
212})
213
214#define __get_user_err(x,ptr,err)					\
215do {									\
216	unsigned long __gu_addr = (unsigned long)(ptr);			\
217	unsigned long __gu_val;						\
 
218	__chk_user_ptr(ptr);						\
 
 
219	switch (sizeof(*(ptr))) {					\
220	case 1:	__get_user_asm_byte(__gu_val,__gu_addr,err);	break;	\
221	case 2:	__get_user_asm_half(__gu_val,__gu_addr,err);	break;	\
222	case 4:	__get_user_asm_word(__gu_val,__gu_addr,err);	break;	\
223	default: (__gu_val) = __get_user_bad();				\
224	}								\
 
225	(x) = (__typeof__(*(ptr)))__gu_val;				\
226} while (0)
 
227
228#define __get_user_asm_byte(x,addr,err)				\
229	__asm__ __volatile__(					\
230	"1:	" T(ldrb) "	%1,[%2],#0\n"			\
231	"2:\n"							\
232	"	.pushsection .fixup,\"ax\"\n"			\
233	"	.align	2\n"					\
234	"3:	mov	%0, %3\n"				\
235	"	mov	%1, #0\n"				\
236	"	b	2b\n"					\
237	"	.popsection\n"					\
238	"	.pushsection __ex_table,\"a\"\n"		\
239	"	.align	3\n"					\
240	"	.long	1b, 3b\n"				\
241	"	.popsection"					\
242	: "+r" (err), "=&r" (x)					\
243	: "r" (addr), "i" (-EFAULT)				\
244	: "cc")
245
 
 
 
 
 
 
 
 
 
 
246#ifndef __ARMEB__
247#define __get_user_asm_half(x,__gu_addr,err)			\
248({								\
249	unsigned long __b1, __b2;				\
250	__get_user_asm_byte(__b1, __gu_addr, err);		\
251	__get_user_asm_byte(__b2, __gu_addr + 1, err);		\
252	(x) = __b1 | (__b2 << 8);				\
253})
254#else
255#define __get_user_asm_half(x,__gu_addr,err)			\
256({								\
257	unsigned long __b1, __b2;				\
258	__get_user_asm_byte(__b1, __gu_addr, err);		\
259	__get_user_asm_byte(__b2, __gu_addr + 1, err);		\
260	(x) = (__b1 << 8) | __b2;				\
261})
262#endif
263
264#define __get_user_asm_word(x,addr,err)				\
265	__asm__ __volatile__(					\
266	"1:	" T(ldr) "	%1,[%2],#0\n"			\
267	"2:\n"							\
268	"	.pushsection .fixup,\"ax\"\n"			\
269	"	.align	2\n"					\
270	"3:	mov	%0, %3\n"				\
271	"	mov	%1, #0\n"				\
272	"	b	2b\n"					\
273	"	.popsection\n"					\
274	"	.pushsection __ex_table,\"a\"\n"		\
275	"	.align	3\n"					\
276	"	.long	1b, 3b\n"				\
277	"	.popsection"					\
278	: "+r" (err), "=&r" (x)					\
279	: "r" (addr), "i" (-EFAULT)				\
280	: "cc")
 
 
 
 
281
282#define __put_user(x,ptr)						\
283({									\
284	long __pu_err = 0;						\
285	__put_user_err((x),(ptr),__pu_err);				\
286	__pu_err;							\
287})
288
289#define __put_user_error(x,ptr,err)					\
 
 
 
 
 
 
 
 
290({									\
291	__put_user_err((x),(ptr),err);					\
292	(void) 0;							\
 
293})
294
295#define __put_user_err(x,ptr,err)					\
296do {									\
297	unsigned long __pu_addr = (unsigned long)(ptr);			\
298	__typeof__(*(ptr)) __pu_val = (x);				\
299	__chk_user_ptr(ptr);						\
300	switch (sizeof(*(ptr))) {					\
301	case 1: __put_user_asm_byte(__pu_val,__pu_addr,err);	break;	\
302	case 2: __put_user_asm_half(__pu_val,__pu_addr,err);	break;	\
303	case 4: __put_user_asm_word(__pu_val,__pu_addr,err);	break;	\
304	case 8:	__put_user_asm_dword(__pu_val,__pu_addr,err);	break;	\
305	default: __put_user_bad();					\
306	}								\
307} while (0)
308
309#define __put_user_asm_byte(x,__pu_addr,err)			\
310	__asm__ __volatile__(					\
311	"1:	" T(strb) "	%1,[%2],#0\n"			\
312	"2:\n"							\
313	"	.pushsection .fixup,\"ax\"\n"			\
314	"	.align	2\n"					\
315	"3:	mov	%0, %3\n"				\
316	"	b	2b\n"					\
317	"	.popsection\n"					\
318	"	.pushsection __ex_table,\"a\"\n"		\
319	"	.align	3\n"					\
320	"	.long	1b, 3b\n"				\
321	"	.popsection"					\
322	: "+r" (err)						\
323	: "r" (x), "r" (__pu_addr), "i" (-EFAULT)		\
324	: "cc")
325
 
 
 
 
 
 
 
 
 
 
326#ifndef __ARMEB__
327#define __put_user_asm_half(x,__pu_addr,err)			\
328({								\
329	unsigned long __temp = (unsigned long)(x);		\
330	__put_user_asm_byte(__temp, __pu_addr, err);		\
331	__put_user_asm_byte(__temp >> 8, __pu_addr + 1, err);	\
332})
333#else
334#define __put_user_asm_half(x,__pu_addr,err)			\
335({								\
336	unsigned long __temp = (unsigned long)(x);		\
337	__put_user_asm_byte(__temp >> 8, __pu_addr, err);	\
338	__put_user_asm_byte(__temp, __pu_addr + 1, err);	\
339})
340#endif
341
342#define __put_user_asm_word(x,__pu_addr,err)			\
343	__asm__ __volatile__(					\
344	"1:	" T(str) "	%1,[%2],#0\n"			\
345	"2:\n"							\
346	"	.pushsection .fixup,\"ax\"\n"			\
347	"	.align	2\n"					\
348	"3:	mov	%0, %3\n"				\
349	"	b	2b\n"					\
350	"	.popsection\n"					\
351	"	.pushsection __ex_table,\"a\"\n"		\
352	"	.align	3\n"					\
353	"	.long	1b, 3b\n"				\
354	"	.popsection"					\
355	: "+r" (err)						\
356	: "r" (x), "r" (__pu_addr), "i" (-EFAULT)		\
357	: "cc")
358
359#ifndef __ARMEB__
360#define	__reg_oper0	"%R2"
361#define	__reg_oper1	"%Q2"
362#else
363#define	__reg_oper0	"%Q2"
364#define	__reg_oper1	"%R2"
365#endif
366
367#define __put_user_asm_dword(x,__pu_addr,err)			\
368	__asm__ __volatile__(					\
369 ARM(	"1:	" T(str) "	" __reg_oper1 ", [%1], #4\n"	)	\
370 ARM(	"2:	" T(str) "	" __reg_oper0 ", [%1]\n"	)	\
371 THUMB(	"1:	" T(str) "	" __reg_oper1 ", [%1]\n"	)	\
372 THUMB(	"2:	" T(str) "	" __reg_oper0 ", [%1, #4]\n"	)	\
373	"3:\n"							\
374	"	.pushsection .fixup,\"ax\"\n"			\
375	"	.align	2\n"					\
376	"4:	mov	%0, %3\n"				\
377	"	b	3b\n"					\
378	"	.popsection\n"					\
379	"	.pushsection __ex_table,\"a\"\n"		\
380	"	.align	3\n"					\
381	"	.long	1b, 4b\n"				\
382	"	.long	2b, 4b\n"				\
383	"	.popsection"					\
384	: "+r" (err), "+r" (__pu_addr)				\
385	: "r" (x), "i" (-EFAULT)				\
386	: "cc")
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
389#ifdef CONFIG_MMU
390extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
391extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
392extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
393extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
394extern unsigned long __must_check __clear_user_std(void __user *addr, unsigned long n);
395#else
396#define __copy_from_user(to,from,n)	(memcpy(to, (void __force *)from, n), 0)
397#define __copy_to_user(to,from,n)	(memcpy((void __force *)to, from, n), 0)
398#define __clear_user(addr,n)		(memset((void __force *)addr, 0, n), 0)
399#endif
 
 
 
 
 
 
 
400
401extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
402extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
 
403
404static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
 
405{
406	if (access_ok(VERIFY_READ, from, n))
407		n = __copy_from_user(to, from, n);
408	else /* security hole - plug it */
409		memset(to, 0, n);
 
410	return n;
411}
412
413static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
 
 
 
 
 
 
414{
415	if (access_ok(VERIFY_WRITE, to, n))
416		n = __copy_to_user(to, from, n);
 
 
 
417	return n;
 
 
 
418}
419
420#define __copy_to_user_inatomic __copy_to_user
421#define __copy_from_user_inatomic __copy_from_user
 
 
422
423static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
 
424{
425	if (access_ok(VERIFY_WRITE, to, n))
426		n = __clear_user(to, n);
 
427	return n;
428}
429
430static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
 
 
431{
432	long res = -EFAULT;
433	if (access_ok(VERIFY_READ, src, 1))
434		res = __strncpy_from_user(dst, src, count);
435	return res;
436}
 
 
 
 
 
 
 
 
 
 
437
438#define strlen_user(s)	strnlen_user(s, ~0UL >> 1)
439
440static inline long __must_check strnlen_user(const char __user *s, long n)
441{
442	unsigned long res = 0;
 
 
 
443
444	if (__addr_ok(s))
445		res = __strnlen_user(s, n);
446
447	return res;
448}
449
450#endif /* _ASMARM_UACCESS_H */
v6.8
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 *  arch/arm/include/asm/uaccess.h
 
 
 
 
  4 */
  5#ifndef _ASMARM_UACCESS_H
  6#define _ASMARM_UACCESS_H
  7
  8/*
  9 * User space memory access functions
 10 */
 11#include <linux/kernel.h>
 12#include <linux/string.h>
 13#include <asm/page.h>
 
 
 14#include <asm/domain.h>
 15#include <asm/unaligned.h>
 16#include <asm/unified.h>
 17#include <asm/compiler.h>
 18
 19#include <asm/extable.h>
 
 20
 21/*
 22 * These two functions allow hooking accesses to userspace to increase
 23 * system integrity by ensuring that the kernel can not inadvertantly
 24 * perform such accesses (eg, via list poison values) which could then
 25 * be exploited for priviledge escalation.
 
 
 
 
 
 
 26 */
 27static __always_inline unsigned int uaccess_save_and_enable(void)
 
 28{
 29#ifdef CONFIG_CPU_SW_DOMAIN_PAN
 30	unsigned int old_domain = get_domain();
 31
 32	/* Set the current domain access to permit user accesses */
 33	set_domain((old_domain & ~domain_mask(DOMAIN_USER)) |
 34		   domain_val(DOMAIN_USER, DOMAIN_CLIENT));
 35
 36	return old_domain;
 37#else
 38	return 0;
 39#endif
 40}
 41
 42static __always_inline void uaccess_restore(unsigned int flags)
 43{
 44#ifdef CONFIG_CPU_SW_DOMAIN_PAN
 45	/* Restore the user access mask */
 46	set_domain(flags);
 47#endif
 48}
 49
 50/*
 51 * These two are intentionally not defined anywhere - if the kernel
 52 * code generates any references to them, that's a bug.
 53 */
 54extern int __get_user_bad(void);
 55extern int __put_user_bad(void);
 56
 
 
 
 
 
 
 57#ifdef CONFIG_MMU
 58
 59/*
 60 * This is a type: either unsigned long, if the argument fits into
 61 * that type, or otherwise unsigned long long.
 62 */
 63#define __inttype(x) \
 64	__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
 65
 66/*
 67 * Sanitise a uaccess pointer such that it becomes NULL if addr+size
 68 * is above the current addr_limit.
 69 */
 70#define uaccess_mask_range_ptr(ptr, size)			\
 71	((__typeof__(ptr))__uaccess_mask_range_ptr(ptr, size))
 72static inline void __user *__uaccess_mask_range_ptr(const void __user *ptr,
 73						    size_t size)
 74{
 75	void __user *safe_ptr = (void __user *)ptr;
 76	unsigned long tmp;
 
 77
 78	asm volatile(
 79	"	.syntax unified\n"
 80	"	sub	%1, %3, #1\n"
 81	"	subs	%1, %1, %0\n"
 82	"	addhs	%1, %1, #1\n"
 83	"	subshs	%1, %1, %2\n"
 84	"	movlo	%0, #0\n"
 85	: "+r" (safe_ptr), "=&r" (tmp)
 86	: "r" (size), "r" (TASK_SIZE)
 87	: "cc");
 88
 89	csdb();
 90	return safe_ptr;
 91}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 92
 93/*
 94 * Single-value transfer routines.  They automatically use the right
 95 * size if we just have the right pointer type.  Note that the functions
 96 * which read from user space (*get_*) need to take care not to leak
 97 * kernel data even if the calling code is buggy and fails to check
 98 * the return value.  This means zeroing out the destination variable
 99 * or buffer on error.  Normally this is done out of line by the
100 * fixup code, but there are a few places where it intrudes on the
101 * main code path.  When we only write to user space, there is no
102 * problem.
103 */
104extern int __get_user_1(void *);
105extern int __get_user_2(void *);
106extern int __get_user_4(void *);
107extern int __get_user_32t_8(void *);
108extern int __get_user_8(void *);
109extern int __get_user_64t_1(void *);
110extern int __get_user_64t_2(void *);
111extern int __get_user_64t_4(void *);
112
113#define __get_user_x(__r2, __p, __e, __l, __s)				\
114	   __asm__ __volatile__ (					\
115		__asmeq("%0", "r0") __asmeq("%1", "r2")			\
116		__asmeq("%3", "r1")					\
117		"bl	__get_user_" #__s				\
118		: "=&r" (__e), "=r" (__r2)				\
119		: "0" (__p), "r" (__l)					\
120		: "ip", "lr", "cc")
121
122/* narrowing a double-word get into a single 32bit word register: */
123#ifdef __ARMEB__
124#define __get_user_x_32t(__r2, __p, __e, __l, __s)			\
125	__get_user_x(__r2, __p, __e, __l, 32t_8)
126#else
127#define __get_user_x_32t __get_user_x
128#endif
129
130/*
131 * storing result into proper least significant word of 64bit target var,
132 * different only for big endian case where 64 bit __r2 lsw is r3:
133 */
134#ifdef __ARMEB__
135#define __get_user_x_64t(__r2, __p, __e, __l, __s)		        \
136	   __asm__ __volatile__ (					\
137		__asmeq("%0", "r0") __asmeq("%1", "r2")			\
138		__asmeq("%3", "r1")					\
139		"bl	__get_user_64t_" #__s				\
140		: "=&r" (__e), "=r" (__r2)				\
141		: "0" (__p), "r" (__l)					\
142		: "ip", "lr", "cc")
143#else
144#define __get_user_x_64t __get_user_x
145#endif
146
147
148#define __get_user_check(x, p)						\
149	({								\
150		unsigned long __limit = TASK_SIZE - 1; \
151		register typeof(*(p)) __user *__p asm("r0") = (p);	\
152		register __inttype(x) __r2 asm("r2");			\
153		register unsigned long __l asm("r1") = __limit;		\
154		register int __e asm("r0");				\
155		unsigned int __ua_flags = uaccess_save_and_enable();	\
156		int __tmp_e;						\
157		switch (sizeof(*(__p))) {				\
158		case 1:							\
159			if (sizeof((x)) >= 8)				\
160				__get_user_x_64t(__r2, __p, __e, __l, 1); \
161			else						\
162				__get_user_x(__r2, __p, __e, __l, 1);	\
163			break;						\
164		case 2:							\
165			if (sizeof((x)) >= 8)				\
166				__get_user_x_64t(__r2, __p, __e, __l, 2); \
167			else						\
168				__get_user_x(__r2, __p, __e, __l, 2);	\
169			break;						\
170		case 4:							\
171			if (sizeof((x)) >= 8)				\
172				__get_user_x_64t(__r2, __p, __e, __l, 4); \
173			else						\
174				__get_user_x(__r2, __p, __e, __l, 4);	\
175			break;						\
176		case 8:							\
177			if (sizeof((x)) < 8)				\
178				__get_user_x_32t(__r2, __p, __e, __l, 4); \
179			else						\
180				__get_user_x(__r2, __p, __e, __l, 8);	\
181			break;						\
182		default: __e = __get_user_bad(); break;			\
183		}							\
184		__tmp_e = __e;						\
185		uaccess_restore(__ua_flags);				\
186		x = (typeof(*(p))) __r2;				\
187		__tmp_e;						\
188	})
189
190#define get_user(x, p)							\
191	({								\
192		might_fault();						\
193		__get_user_check(x, p);					\
194	 })
195
196extern int __put_user_1(void *, unsigned int);
197extern int __put_user_2(void *, unsigned int);
198extern int __put_user_4(void *, unsigned int);
199extern int __put_user_8(void *, unsigned long long);
200
201#define __put_user_check(__pu_val, __ptr, __err, __s)			\
 
 
 
 
 
 
 
 
202	({								\
203		unsigned long __limit = TASK_SIZE - 1; \
204		register typeof(__pu_val) __r2 asm("r2") = __pu_val;	\
205		register const void __user *__p asm("r0") = __ptr;	\
206		register unsigned long __l asm("r1") = __limit;		\
207		register int __e asm("r0");				\
208		__asm__ __volatile__ (					\
209			__asmeq("%0", "r0") __asmeq("%2", "r2")		\
210			__asmeq("%3", "r1")				\
211			"bl	__put_user_" #__s			\
212			: "=&r" (__e)					\
213			: "0" (__p), "r" (__r2), "r" (__l)		\
214			: "ip", "lr", "cc");				\
215		__err = __e;						\
 
 
 
 
 
 
 
 
216	})
217
218#else /* CONFIG_MMU */
219
220#define get_user(x, p)	__get_user(x, p)
221#define __put_user_check __put_user_nocheck
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
223#endif /* CONFIG_MMU */
224
225#include <asm-generic/access_ok.h>
226
227#ifdef CONFIG_CPU_SPECTRE
228/*
229 * When mitigating Spectre variant 1, it is not worth fixing the non-
230 * verifying accessors, because we need to add verification of the
231 * address space there.  Force these to use the standard get_user()
232 * version instead.
233 */
234#define __get_user(x, ptr) get_user(x, ptr)
235#else
236
237/*
238 * The "__xxx" versions of the user access functions do not verify the
239 * address space - it must have been done previously with a separate
240 * "access_ok()" call.
241 *
242 * The "xxx_error" versions set the third argument to EFAULT if an
243 * error occurs, and leave it unchanged on success.  Note that these
244 * versions are void (ie, don't return a value as such).
245 */
246#define __get_user(x, ptr)						\
247({									\
248	long __gu_err = 0;						\
249	__get_user_err((x), (ptr), __gu_err, TUSER());			\
250	__gu_err;							\
251})
252
253#define __get_user_err(x, ptr, err, __t)				\
 
 
 
 
 
 
254do {									\
255	unsigned long __gu_addr = (unsigned long)(ptr);			\
256	unsigned long __gu_val;						\
257	unsigned int __ua_flags;					\
258	__chk_user_ptr(ptr);						\
259	might_fault();							\
260	__ua_flags = uaccess_save_and_enable();				\
261	switch (sizeof(*(ptr))) {					\
262	case 1:	__get_user_asm_byte(__gu_val, __gu_addr, err, __t); break;	\
263	case 2:	__get_user_asm_half(__gu_val, __gu_addr, err, __t); break;	\
264	case 4:	__get_user_asm_word(__gu_val, __gu_addr, err, __t); break;	\
265	default: (__gu_val) = __get_user_bad();				\
266	}								\
267	uaccess_restore(__ua_flags);					\
268	(x) = (__typeof__(*(ptr)))__gu_val;				\
269} while (0)
270#endif
271
272#define __get_user_asm(x, addr, err, instr)			\
273	__asm__ __volatile__(					\
274	"1:	" instr " %1, [%2], #0\n"			\
275	"2:\n"							\
276	"	.pushsection .text.fixup,\"ax\"\n"		\
277	"	.align	2\n"					\
278	"3:	mov	%0, %3\n"				\
279	"	mov	%1, #0\n"				\
280	"	b	2b\n"					\
281	"	.popsection\n"					\
282	"	.pushsection __ex_table,\"a\"\n"		\
283	"	.align	3\n"					\
284	"	.long	1b, 3b\n"				\
285	"	.popsection"					\
286	: "+r" (err), "=&r" (x)					\
287	: "r" (addr), "i" (-EFAULT)				\
288	: "cc")
289
290#define __get_user_asm_byte(x, addr, err, __t)			\
291	__get_user_asm(x, addr, err, "ldrb" __t)
292
293#if __LINUX_ARM_ARCH__ >= 6
294
295#define __get_user_asm_half(x, addr, err, __t)			\
296	__get_user_asm(x, addr, err, "ldrh" __t)
297
298#else
299
300#ifndef __ARMEB__
301#define __get_user_asm_half(x, __gu_addr, err, __t)		\
302({								\
303	unsigned long __b1, __b2;				\
304	__get_user_asm_byte(__b1, __gu_addr, err, __t);		\
305	__get_user_asm_byte(__b2, __gu_addr + 1, err, __t);	\
306	(x) = __b1 | (__b2 << 8);				\
307})
308#else
309#define __get_user_asm_half(x, __gu_addr, err, __t)		\
310({								\
311	unsigned long __b1, __b2;				\
312	__get_user_asm_byte(__b1, __gu_addr, err, __t);		\
313	__get_user_asm_byte(__b2, __gu_addr + 1, err, __t);	\
314	(x) = (__b1 << 8) | __b2;				\
315})
316#endif
317
318#endif /* __LINUX_ARM_ARCH__ >= 6 */
319
320#define __get_user_asm_word(x, addr, err, __t)			\
321	__get_user_asm(x, addr, err, "ldr" __t)
322
323#define __put_user_switch(x, ptr, __err, __fn)				\
324	do {								\
325		const __typeof__(*(ptr)) __user *__pu_ptr = (ptr);	\
326		__typeof__(*(ptr)) __pu_val = (x);			\
327		unsigned int __ua_flags;				\
328		might_fault();						\
329		__ua_flags = uaccess_save_and_enable();			\
330		switch (sizeof(*(ptr))) {				\
331		case 1: __fn(__pu_val, __pu_ptr, __err, 1); break;	\
332		case 2:	__fn(__pu_val, __pu_ptr, __err, 2); break;	\
333		case 4:	__fn(__pu_val, __pu_ptr, __err, 4); break;	\
334		case 8:	__fn(__pu_val, __pu_ptr, __err, 8); break;	\
335		default: __err = __put_user_bad(); break;		\
336		}							\
337		uaccess_restore(__ua_flags);				\
338	} while (0)
339
340#define put_user(x, ptr)						\
341({									\
342	int __pu_err = 0;						\
343	__put_user_switch((x), (ptr), __pu_err, __put_user_check);	\
344	__pu_err;							\
345})
346
347#ifdef CONFIG_CPU_SPECTRE
348/*
349 * When mitigating Spectre variant 1.1, all accessors need to include
350 * verification of the address space.
351 */
352#define __put_user(x, ptr) put_user(x, ptr)
353
354#else
355#define __put_user(x, ptr)						\
356({									\
357	long __pu_err = 0;						\
358	__put_user_switch((x), (ptr), __pu_err, __put_user_nocheck);	\
359	__pu_err;							\
360})
361
362#define __put_user_nocheck(x, __pu_ptr, __err, __size)			\
363	do {								\
364		unsigned long __pu_addr = (unsigned long)__pu_ptr;	\
365		__put_user_nocheck_##__size(x, __pu_addr, __err, TUSER());\
366	} while (0)
367
368#define __put_user_nocheck_1 __put_user_asm_byte
369#define __put_user_nocheck_2 __put_user_asm_half
370#define __put_user_nocheck_4 __put_user_asm_word
371#define __put_user_nocheck_8 __put_user_asm_dword
372
373#endif /* !CONFIG_CPU_SPECTRE */
 
374
375#define __put_user_asm(x, __pu_addr, err, instr)		\
376	__asm__ __volatile__(					\
377	"1:	" instr " %1, [%2], #0\n"		\
378	"2:\n"							\
379	"	.pushsection .text.fixup,\"ax\"\n"		\
380	"	.align	2\n"					\
381	"3:	mov	%0, %3\n"				\
382	"	b	2b\n"					\
383	"	.popsection\n"					\
384	"	.pushsection __ex_table,\"a\"\n"		\
385	"	.align	3\n"					\
386	"	.long	1b, 3b\n"				\
387	"	.popsection"					\
388	: "+r" (err)						\
389	: "r" (x), "r" (__pu_addr), "i" (-EFAULT)		\
390	: "cc")
391
392#define __put_user_asm_byte(x, __pu_addr, err, __t)		\
393	__put_user_asm(x, __pu_addr, err, "strb" __t)
394
395#if __LINUX_ARM_ARCH__ >= 6
396
397#define __put_user_asm_half(x, __pu_addr, err, __t)		\
398	__put_user_asm(x, __pu_addr, err, "strh" __t)
399
400#else
401
402#ifndef __ARMEB__
403#define __put_user_asm_half(x, __pu_addr, err, __t)		\
404({								\
405	unsigned long __temp = (__force unsigned long)(x);	\
406	__put_user_asm_byte(__temp, __pu_addr, err, __t);	\
407	__put_user_asm_byte(__temp >> 8, __pu_addr + 1, err, __t);\
408})
409#else
410#define __put_user_asm_half(x, __pu_addr, err, __t)		\
411({								\
412	unsigned long __temp = (__force unsigned long)(x);	\
413	__put_user_asm_byte(__temp >> 8, __pu_addr, err, __t);	\
414	__put_user_asm_byte(__temp, __pu_addr + 1, err, __t);	\
415})
416#endif
417
418#endif /* __LINUX_ARM_ARCH__ >= 6 */
419
420#define __put_user_asm_word(x, __pu_addr, err, __t)		\
421	__put_user_asm(x, __pu_addr, err, "str" __t)
 
 
 
 
 
 
 
 
 
 
 
 
422
423#ifndef __ARMEB__
424#define	__reg_oper0	"%R2"
425#define	__reg_oper1	"%Q2"
426#else
427#define	__reg_oper0	"%Q2"
428#define	__reg_oper1	"%R2"
429#endif
430
431#define __put_user_asm_dword(x, __pu_addr, err, __t)		\
432	__asm__ __volatile__(					\
433 ARM(	"1:	str" __t "	" __reg_oper1 ", [%1], #4\n"  ) \
434 ARM(	"2:	str" __t "	" __reg_oper0 ", [%1]\n"      ) \
435 THUMB(	"1:	str" __t "	" __reg_oper1 ", [%1]\n"      ) \
436 THUMB(	"2:	str" __t "	" __reg_oper0 ", [%1, #4]\n"  ) \
437	"3:\n"							\
438	"	.pushsection .text.fixup,\"ax\"\n"		\
439	"	.align	2\n"					\
440	"4:	mov	%0, %3\n"				\
441	"	b	3b\n"					\
442	"	.popsection\n"					\
443	"	.pushsection __ex_table,\"a\"\n"		\
444	"	.align	3\n"					\
445	"	.long	1b, 4b\n"				\
446	"	.long	2b, 4b\n"				\
447	"	.popsection"					\
448	: "+r" (err), "+r" (__pu_addr)				\
449	: "r" (x), "i" (-EFAULT)				\
450	: "cc")
451
452#define __get_kernel_nofault(dst, src, type, err_label)			\
453do {									\
454	const type *__pk_ptr = (src);					\
455	unsigned long __src = (unsigned long)(__pk_ptr);		\
456	type __val;							\
457	int __err = 0;							\
458	switch (sizeof(type)) {						\
459	case 1:	__get_user_asm_byte(__val, __src, __err, ""); break;	\
460	case 2: __get_user_asm_half(__val, __src, __err, ""); break;	\
461	case 4: __get_user_asm_word(__val, __src, __err, ""); break;	\
462	case 8: {							\
463		u32 *__v32 = (u32*)&__val;				\
464		__get_user_asm_word(__v32[0], __src, __err, "");	\
465		if (__err)						\
466			break;						\
467		__get_user_asm_word(__v32[1], __src+4, __err, "");	\
468		break;							\
469	}								\
470	default: __err = __get_user_bad(); break;			\
471	}								\
472	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))		\
473		put_unaligned(__val, (type *)(dst));			\
474	else								\
475		*(type *)(dst) = __val; /* aligned by caller */		\
476	if (__err)							\
477		goto err_label;						\
478} while (0)
479
480#define __put_kernel_nofault(dst, src, type, err_label)			\
481do {									\
482	const type *__pk_ptr = (dst);					\
483	unsigned long __dst = (unsigned long)__pk_ptr;			\
484	int __err = 0;							\
485	type __val = IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)	\
486		     ? get_unaligned((type *)(src))			\
487		     : *(type *)(src);	/* aligned by caller */		\
488	switch (sizeof(type)) {						\
489	case 1: __put_user_asm_byte(__val, __dst, __err, ""); break;	\
490	case 2:	__put_user_asm_half(__val, __dst, __err, ""); break;	\
491	case 4:	__put_user_asm_word(__val, __dst, __err, ""); break;	\
492	case 8:	__put_user_asm_dword(__val, __dst, __err, ""); break;	\
493	default: __err = __put_user_bad(); break;			\
494	}								\
495	if (__err)							\
496		goto err_label;						\
497} while (0)
498
499#ifdef CONFIG_MMU
500extern unsigned long __must_check
501arm_copy_from_user(void *to, const void __user *from, unsigned long n);
502
503static inline unsigned long __must_check
504raw_copy_from_user(void *to, const void __user *from, unsigned long n)
505{
506	unsigned int __ua_flags;
507
508	__ua_flags = uaccess_save_and_enable();
509	n = arm_copy_from_user(to, from, n);
510	uaccess_restore(__ua_flags);
511	return n;
512}
513
514extern unsigned long __must_check
515arm_copy_to_user(void __user *to, const void *from, unsigned long n);
516extern unsigned long __must_check
517__copy_to_user_std(void __user *to, const void *from, unsigned long n);
518
519static inline unsigned long __must_check
520raw_copy_to_user(void __user *to, const void *from, unsigned long n)
521{
522#ifndef CONFIG_UACCESS_WITH_MEMCPY
523	unsigned int __ua_flags;
524	__ua_flags = uaccess_save_and_enable();
525	n = arm_copy_to_user(to, from, n);
526	uaccess_restore(__ua_flags);
527	return n;
528#else
529	return arm_copy_to_user(to, from, n);
530#endif
531}
532
533extern unsigned long __must_check
534arm_clear_user(void __user *addr, unsigned long n);
535extern unsigned long __must_check
536__clear_user_std(void __user *addr, unsigned long n);
537
538static inline unsigned long __must_check
539__clear_user(void __user *addr, unsigned long n)
540{
541	unsigned int __ua_flags = uaccess_save_and_enable();
542	n = arm_clear_user(addr, n);
543	uaccess_restore(__ua_flags);
544	return n;
545}
546
547#else
548static inline unsigned long
549raw_copy_from_user(void *to, const void __user *from, unsigned long n)
550{
551	memcpy(to, (const void __force *)from, n);
552	return 0;
 
 
553}
554static inline unsigned long
555raw_copy_to_user(void __user *to, const void *from, unsigned long n)
556{
557	memcpy((void __force *)to, from, n);
558	return 0;
559}
560#define __clear_user(addr, n)		(memset((void __force *)addr, 0, n), 0)
561#endif
562#define INLINE_COPY_TO_USER
563#define INLINE_COPY_FROM_USER
564
565static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
 
 
566{
567	if (access_ok(to, n))
568		n = __clear_user(to, n);
569	return n;
570}
571
572/* These are from lib/ code, and use __get_user() and friends */
573extern long strncpy_from_user(char *dest, const char __user *src, long count);
574
575extern __must_check long strnlen_user(const char __user *str, long n);
 
576
577#endif /* _ASMARM_UACCESS_H */