xref: /openbmc/linux/arch/ia64/include/asm/uaccess.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27f30491cSTony Luck #ifndef _ASM_IA64_UACCESS_H
37f30491cSTony Luck #define _ASM_IA64_UACCESS_H
47f30491cSTony Luck 
57f30491cSTony Luck /*
67f30491cSTony Luck  * This file defines various macros to transfer memory areas across
77f30491cSTony Luck  * the user/kernel boundary.  This needs to be done carefully because
87f30491cSTony Luck  * this code is executed in kernel mode and uses user-specified
97f30491cSTony Luck  * addresses.  Thus, we need to be careful not to let the user to
107f30491cSTony Luck  * trick us into accessing kernel memory that would normally be
117f30491cSTony Luck  * inaccessible.  This code is also fairly performance sensitive,
127f30491cSTony Luck  * so we want to spend as little time doing safety checks as
137f30491cSTony Luck  * possible.
147f30491cSTony Luck  *
157f30491cSTony Luck  * To make matters a bit more interesting, these macros sometimes also
167f30491cSTony Luck  * called from within the kernel itself, in which case the address
177f30491cSTony Luck  * validity check must be skipped.  The get_fs() macro tells us what
187f30491cSTony Luck  * to do: if get_fs()==USER_DS, checking is performed, if
197f30491cSTony Luck  * get_fs()==KERNEL_DS, checking is bypassed.
207f30491cSTony Luck  *
217f30491cSTony Luck  * Note that even if the memory area specified by the user is in a
227f30491cSTony Luck  * valid address range, it is still possible that we'll get a page
237f30491cSTony Luck  * fault while accessing it.  This is handled by filling out an
247f30491cSTony Luck  * exception handler fixup entry for each instruction that has the
257f30491cSTony Luck  * potential to fault.  When such a fault occurs, the page fault
267f30491cSTony Luck  * handler checks to see whether the faulting instruction has a fixup
277f30491cSTony Luck  * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
287f30491cSTony Luck  * then resumes execution at the continuation point.
297f30491cSTony Luck  *
307f30491cSTony Luck  * Based on <asm-alpha/uaccess.h>.
317f30491cSTony Luck  *
327f30491cSTony Luck  * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
337f30491cSTony Luck  *	David Mosberger-Tang <davidm@hpl.hp.com>
347f30491cSTony Luck  */
357f30491cSTony Luck 
367f30491cSTony Luck #include <linux/compiler.h>
377f30491cSTony Luck #include <linux/page-flags.h>
387f30491cSTony Luck 
397f30491cSTony Luck #include <asm/intrinsics.h>
40ca5999fdSMike Rapoport #include <linux/pgtable.h>
417f30491cSTony Luck #include <asm/io.h>
428bec2717SAl Viro #include <asm/extable.h>
437f30491cSTony Luck 
447f30491cSTony Luck /*
45*e5ef21d1SArnd Bergmann  * When accessing user memory, we need to make sure the entire area really is
46*e5ef21d1SArnd Bergmann  * in user-level space.  We also need to make sure that the address doesn't
477f30491cSTony Luck  * point inside the virtually mapped linear page table.
487f30491cSTony Luck  */
__access_ok(const void __user * p,unsigned long size)497bb8a503SAl Viro static inline int __access_ok(const void __user *p, unsigned long size)
507bb8a503SAl Viro {
51*e5ef21d1SArnd Bergmann 	unsigned long limit = TASK_SIZE;
527bb8a503SAl Viro 	unsigned long addr = (unsigned long)p;
53*e5ef21d1SArnd Bergmann 
54*e5ef21d1SArnd Bergmann 	return likely((size <= limit) && (addr <= (limit - size)) &&
55*e5ef21d1SArnd Bergmann 		 likely(REGION_OFFSET(addr) < RGN_MAP_LIMIT));
567bb8a503SAl Viro }
5712700c17SArnd Bergmann #define __access_ok __access_ok
5812700c17SArnd Bergmann #include <asm-generic/access_ok.h>
597f30491cSTony Luck 
607f30491cSTony Luck /*
617f30491cSTony Luck  * These are the main single-value transfer routines.  They automatically
627f30491cSTony Luck  * use the right size if we just have the right pointer type.
637f30491cSTony Luck  *
647f30491cSTony Luck  * Careful to not
657f30491cSTony Luck  * (a) re-use the arguments for side effects (sizeof/typeof is ok)
667f30491cSTony Luck  * (b) require any knowledge of processes at this stage
677f30491cSTony Luck  */
681bd5986bSAl Viro #define put_user(x, ptr)	__put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
691bd5986bSAl Viro #define get_user(x, ptr)	__get_user_check((x), (ptr), sizeof(*(ptr)))
707f30491cSTony Luck 
717f30491cSTony Luck /*
727f30491cSTony Luck  * The "__xxx" versions do not do address space checking, useful when
737f30491cSTony Luck  * doing multiple accesses to the same area (the programmer has to do the
747f30491cSTony Luck  * checks by hand with "access_ok()")
757f30491cSTony Luck  */
767f30491cSTony Luck #define __put_user(x, ptr)	__put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
777f30491cSTony Luck #define __get_user(x, ptr)	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
787f30491cSTony Luck 
797f30491cSTony Luck #ifdef ASM_SUPPORTED
807f30491cSTony Luck   struct __large_struct { unsigned long buf[100]; };
817f30491cSTony Luck # define __m(x) (*(struct __large_struct __user *)(x))
827f30491cSTony Luck 
837f30491cSTony Luck /* We need to declare the __ex_table section before we can use it in .xdata.  */
847f30491cSTony Luck asm (".section \"__ex_table\", \"a\"\n\t.previous");
857f30491cSTony Luck 
867f30491cSTony Luck # define __get_user_size(val, addr, n, err)							\
877f30491cSTony Luck do {												\
887f30491cSTony Luck 	register long __gu_r8 asm ("r8") = 0;							\
897f30491cSTony Luck 	register long __gu_r9 asm ("r9");							\
907f30491cSTony Luck 	asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"	\
917f30491cSTony Luck 	     "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"						\
927f30491cSTony Luck 	     "[1:]"										\
937f30491cSTony Luck 	     : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8));			\
947f30491cSTony Luck 	(err) = __gu_r8;									\
957f30491cSTony Luck 	(val) = __gu_r9;									\
967f30491cSTony Luck } while (0)
977f30491cSTony Luck 
987f30491cSTony Luck /*
997f30491cSTony Luck  * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it.  This
1007f30491cSTony Luck  * is because they do not write to any memory gcc knows about, so there are no aliasing
1017f30491cSTony Luck  * issues.
1027f30491cSTony Luck  */
1037f30491cSTony Luck # define __put_user_size(val, addr, n, err)							\
1047f30491cSTony Luck do {												\
1057f30491cSTony Luck 	register long __pu_r8 asm ("r8") = 0;							\
1067f30491cSTony Luck 	asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n"	\
1077f30491cSTony Luck 		      "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"					\
1087f30491cSTony Luck 		      "[1:]"									\
1097f30491cSTony Luck 		      : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8));		\
1107f30491cSTony Luck 	(err) = __pu_r8;									\
1117f30491cSTony Luck } while (0)
1127f30491cSTony Luck 
1137f30491cSTony Luck #else /* !ASM_SUPPORTED */
1147f30491cSTony Luck # define RELOC_TYPE	2	/* ip-rel */
1157f30491cSTony Luck # define __get_user_size(val, addr, n, err)				\
1167f30491cSTony Luck do {									\
1177f30491cSTony Luck 	__ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE);	\
1187f30491cSTony Luck 	(err) = ia64_getreg(_IA64_REG_R8);				\
1197f30491cSTony Luck 	(val) = ia64_getreg(_IA64_REG_R9);				\
1207f30491cSTony Luck } while (0)
1217f30491cSTony Luck # define __put_user_size(val, addr, n, err)				\
1227f30491cSTony Luck do {									\
1239605ce7eSMichael S. Tsirkin 	__st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE,	\
1249605ce7eSMichael S. Tsirkin 		  (__force unsigned long) (val));			\
1257f30491cSTony Luck 	(err) = ia64_getreg(_IA64_REG_R8);				\
1267f30491cSTony Luck } while (0)
1277f30491cSTony Luck #endif /* !ASM_SUPPORTED */
1287f30491cSTony Luck 
1297f30491cSTony Luck extern void __get_user_unknown (void);
1307f30491cSTony Luck 
1317f30491cSTony Luck /*
1327f30491cSTony Luck  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
1337f30491cSTony Luck  * could clobber r8 and r9 (among others).  Thus, be careful not to evaluate it while
1347f30491cSTony Luck  * using r8/r9.
1357f30491cSTony Luck  */
13611836eceSAl Viro #define __do_get_user(check, x, ptr, size)						\
1377f30491cSTony Luck ({											\
1387f30491cSTony Luck 	const __typeof__(*(ptr)) __user *__gu_ptr = (ptr);				\
1397f30491cSTony Luck 	__typeof__ (size) __gu_size = (size);						\
1407f30491cSTony Luck 	long __gu_err = -EFAULT;							\
1417f30491cSTony Luck 	unsigned long __gu_val = 0;							\
1427bb8a503SAl Viro 	if (!check || __access_ok(__gu_ptr, size))					\
1437f30491cSTony Luck 		switch (__gu_size) {							\
1447f30491cSTony Luck 		      case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break;	\
1457f30491cSTony Luck 		      case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break;	\
1467f30491cSTony Luck 		      case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break;	\
1477f30491cSTony Luck 		      case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break;	\
1487f30491cSTony Luck 		      default: __get_user_unknown(); break;				\
1497f30491cSTony Luck 		}									\
150a6325e72SMichael S. Tsirkin 	(x) = (__force __typeof__(*(__gu_ptr))) __gu_val;				\
1517f30491cSTony Luck 	__gu_err;									\
1527f30491cSTony Luck })
1537f30491cSTony Luck 
15411836eceSAl Viro #define __get_user_nocheck(x, ptr, size)	__do_get_user(0, x, ptr, size)
15511836eceSAl Viro #define __get_user_check(x, ptr, size)	__do_get_user(1, x, ptr, size)
1567f30491cSTony Luck 
1577f30491cSTony Luck extern void __put_user_unknown (void);
1587f30491cSTony Luck 
1597f30491cSTony Luck /*
1607f30491cSTony Luck  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
1617f30491cSTony Luck  * could clobber r8 (among others).  Thus, be careful not to evaluate them while using r8.
1627f30491cSTony Luck  */
16311836eceSAl Viro #define __do_put_user(check, x, ptr, size)						\
1647f30491cSTony Luck ({											\
1657f30491cSTony Luck 	__typeof__ (x) __pu_x = (x);							\
1667f30491cSTony Luck 	__typeof__ (*(ptr)) __user *__pu_ptr = (ptr);					\
1677f30491cSTony Luck 	__typeof__ (size) __pu_size = (size);						\
1687f30491cSTony Luck 	long __pu_err = -EFAULT;							\
1697f30491cSTony Luck 											\
1707bb8a503SAl Viro 	if (!check || __access_ok(__pu_ptr, __pu_size))					\
1717f30491cSTony Luck 		switch (__pu_size) {							\
1727f30491cSTony Luck 		      case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break;	\
1737f30491cSTony Luck 		      case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break;	\
1747f30491cSTony Luck 		      case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break;	\
1757f30491cSTony Luck 		      case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break;	\
1767f30491cSTony Luck 		      default: __put_user_unknown(); break;				\
1777f30491cSTony Luck 		}									\
1787f30491cSTony Luck 	__pu_err;									\
1797f30491cSTony Luck })
1807f30491cSTony Luck 
18111836eceSAl Viro #define __put_user_nocheck(x, ptr, size)	__do_put_user(0, x, ptr, size)
18211836eceSAl Viro #define __put_user_check(x, ptr, size)	__do_put_user(1, x, ptr, size)
1837f30491cSTony Luck 
1847f30491cSTony Luck /*
1857f30491cSTony Luck  * Complex access routines
1867f30491cSTony Luck  */
1877f30491cSTony Luck extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
1887f30491cSTony Luck 					       unsigned long count);
1897f30491cSTony Luck 
1907f30491cSTony Luck static inline unsigned long
raw_copy_to_user(void __user * to,const void * from,unsigned long count)191b3622d32SAl Viro raw_copy_to_user(void __user *to, const void *from, unsigned long count)
1927f30491cSTony Luck {
1937f30491cSTony Luck 	return __copy_user(to, (__force void __user *) from, count);
1947f30491cSTony Luck }
1957f30491cSTony Luck 
1967f30491cSTony Luck static inline unsigned long
raw_copy_from_user(void * to,const void __user * from,unsigned long count)197b3622d32SAl Viro raw_copy_from_user(void *to, const void __user *from, unsigned long count)
1987f30491cSTony Luck {
1997f30491cSTony Luck 	return __copy_user((__force void __user *) to, from, count);
2007f30491cSTony Luck }
2017f30491cSTony Luck 
202b3622d32SAl Viro #define INLINE_COPY_FROM_USER
203b3622d32SAl Viro #define INLINE_COPY_TO_USER
2047f30491cSTony Luck 
2057f30491cSTony Luck extern unsigned long __do_clear_user (void __user *, unsigned long);
2067f30491cSTony Luck 
2077f30491cSTony Luck #define __clear_user(to, n)		__do_clear_user(to, n)
2087f30491cSTony Luck 
2097f30491cSTony Luck #define clear_user(to, n)					\
2107f30491cSTony Luck ({								\
2117f30491cSTony Luck 	unsigned long __cu_len = (n);				\
2127bb8a503SAl Viro 	if (__access_ok(to, __cu_len))				\
2137f30491cSTony Luck 		__cu_len = __do_clear_user(to, __cu_len);	\
2147f30491cSTony Luck 	__cu_len;						\
2157f30491cSTony Luck })
2167f30491cSTony Luck 
2177f30491cSTony Luck 
2187f30491cSTony Luck /*
2197f30491cSTony Luck  * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
2207f30491cSTony Luck  * strlen.
2217f30491cSTony Luck  */
2227f30491cSTony Luck extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
2237f30491cSTony Luck 
2247f30491cSTony Luck #define strncpy_from_user(to, from, n)					\
2257f30491cSTony Luck ({									\
2267f30491cSTony Luck 	const char __user * __sfu_from = (from);			\
2277f30491cSTony Luck 	long __sfu_ret = -EFAULT;					\
2287bb8a503SAl Viro 	if (__access_ok(__sfu_from, 0))					\
2297f30491cSTony Luck 		__sfu_ret = __strncpy_from_user((to), __sfu_from, (n));	\
2307f30491cSTony Luck 	__sfu_ret;							\
2317f30491cSTony Luck })
2327f30491cSTony Luck 
2337f30491cSTony Luck /*
2347f30491cSTony Luck  * Returns: 0 if exception before NUL or reaching the supplied limit
2357f30491cSTony Luck  * (N), a value greater than N if the limit would be exceeded, else
2367f30491cSTony Luck  * strlen.
2377f30491cSTony Luck  */
2387f30491cSTony Luck extern unsigned long __strnlen_user (const char __user *, long);
2397f30491cSTony Luck 
2407f30491cSTony Luck #define strnlen_user(str, len)					\
2417f30491cSTony Luck ({								\
2427f30491cSTony Luck 	const char __user *__su_str = (str);			\
2437f30491cSTony Luck 	unsigned long __su_ret = 0;				\
2447bb8a503SAl Viro 	if (__access_ok(__su_str, 0))				\
2457f30491cSTony Luck 		__su_ret = __strnlen_user(__su_str, len);	\
2467f30491cSTony Luck 	__su_ret;						\
2477f30491cSTony Luck })
2487f30491cSTony Luck 
2497f30491cSTony Luck #define ARCH_HAS_TRANSLATE_MEM_PTR	1
250dc012014SThierry Reding static __inline__ void *
xlate_dev_mem_ptr(phys_addr_t p)251dc012014SThierry Reding xlate_dev_mem_ptr(phys_addr_t p)
2527f30491cSTony Luck {
2537f30491cSTony Luck 	struct page *page;
254dc012014SThierry Reding 	void *ptr;
2557f30491cSTony Luck 
2567f30491cSTony Luck 	page = pfn_to_page(p >> PAGE_SHIFT);
2577f30491cSTony Luck 	if (PageUncached(page))
258dc012014SThierry Reding 		ptr = (void *)p + __IA64_UNCACHED_OFFSET;
2597f30491cSTony Luck 	else
2607f30491cSTony Luck 		ptr = __va(p);
2617f30491cSTony Luck 
2627f30491cSTony Luck 	return ptr;
2637f30491cSTony Luck }
2647f30491cSTony Luck 
2657f30491cSTony Luck #endif /* _ASM_IA64_UACCESS_H */
266