uaccess.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_IA64_UACCESS_H
  3. #define _ASM_IA64_UACCESS_H
  4. /*
  5. * This file defines various macros to transfer memory areas across
  6. * the user/kernel boundary. This needs to be done carefully because
  7. * this code is executed in kernel mode and uses user-specified
  8. * addresses. Thus, we need to be careful not to let the user to
  9. * trick us into accessing kernel memory that would normally be
  10. * inaccessible. This code is also fairly performance sensitive,
  11. * so we want to spend as little time doing safety checks as
  12. * possible.
  13. *
  14. * To make matters a bit more interesting, these macros sometimes also
  15. * called from within the kernel itself, in which case the address
  16. * validity check must be skipped. The get_fs() macro tells us what
  17. * to do: if get_fs()==USER_DS, checking is performed, if
  18. * get_fs()==KERNEL_DS, checking is bypassed.
  19. *
  20. * Note that even if the memory area specified by the user is in a
  21. * valid address range, it is still possible that we'll get a page
  22. * fault while accessing it. This is handled by filling out an
  23. * exception handler fixup entry for each instruction that has the
  24. * potential to fault. When such a fault occurs, the page fault
  25. * handler checks to see whether the faulting instruction has a fixup
  26. * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
  27. * then resumes execution at the continuation point.
  28. *
  29. * Based on <asm-alpha/uaccess.h>.
  30. *
  31. * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
  32. * David Mosberger-Tang <[email protected]>
  33. */
  34. #include <linux/compiler.h>
  35. #include <linux/page-flags.h>
  36. #include <asm/intrinsics.h>
  37. #include <linux/pgtable.h>
  38. #include <asm/io.h>
  39. #include <asm/extable.h>
  40. /*
  41. * When accessing user memory, we need to make sure the entire area really is
  42. * in user-level space. We also need to make sure that the address doesn't
  43. * point inside the virtually mapped linear page table.
  44. */
  45. static inline int __access_ok(const void __user *p, unsigned long size)
  46. {
  47. unsigned long limit = TASK_SIZE;
  48. unsigned long addr = (unsigned long)p;
  49. return likely((size <= limit) && (addr <= (limit - size)) &&
  50. likely(REGION_OFFSET(addr) < RGN_MAP_LIMIT));
  51. }
  52. #define __access_ok __access_ok
  53. #include <asm-generic/access_ok.h>
  54. /*
  55. * These are the main single-value transfer routines. They automatically
  56. * use the right size if we just have the right pointer type.
  57. *
  58. * Careful to not
  59. * (a) re-use the arguments for side effects (sizeof/typeof is ok)
  60. * (b) require any knowledge of processes at this stage
  61. */
  62. #define put_user(x, ptr) __put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
  63. #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  64. /*
  65. * The "__xxx" versions do not do address space checking, useful when
  66. * doing multiple accesses to the same area (the programmer has to do the
  67. * checks by hand with "access_ok()")
  68. */
  69. #define __put_user(x, ptr) __put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
  70. #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  71. #ifdef ASM_SUPPORTED
  72. struct __large_struct { unsigned long buf[100]; };
  73. # define __m(x) (*(struct __large_struct __user *)(x))
  74. /* We need to declare the __ex_table section before we can use it in .xdata. */
  75. asm (".section \"__ex_table\", \"a\"\n\t.previous");
  76. # define __get_user_size(val, addr, n, err) \
  77. do { \
  78. register long __gu_r8 asm ("r8") = 0; \
  79. register long __gu_r9 asm ("r9"); \
  80. asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n" \
  81. "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n" \
  82. "[1:]" \
  83. : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8)); \
  84. (err) = __gu_r8; \
  85. (val) = __gu_r9; \
  86. } while (0)
  87. /*
  88. * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it. This
  89. * is because they do not write to any memory gcc knows about, so there are no aliasing
  90. * issues.
  91. */
  92. # define __put_user_size(val, addr, n, err) \
  93. do { \
  94. register long __pu_r8 asm ("r8") = 0; \
  95. asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
  96. "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
  97. "[1:]" \
  98. : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8)); \
  99. (err) = __pu_r8; \
  100. } while (0)
  101. #else /* !ASM_SUPPORTED */
  102. # define RELOC_TYPE 2 /* ip-rel */
  103. # define __get_user_size(val, addr, n, err) \
  104. do { \
  105. __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE); \
  106. (err) = ia64_getreg(_IA64_REG_R8); \
  107. (val) = ia64_getreg(_IA64_REG_R9); \
  108. } while (0)
  109. # define __put_user_size(val, addr, n, err) \
  110. do { \
  111. __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, \
  112. (__force unsigned long) (val)); \
  113. (err) = ia64_getreg(_IA64_REG_R8); \
  114. } while (0)
  115. #endif /* !ASM_SUPPORTED */
  116. extern void __get_user_unknown (void);
  117. /*
  118. * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
  119. * could clobber r8 and r9 (among others). Thus, be careful not to evaluate it while
  120. * using r8/r9.
  121. */
  122. #define __do_get_user(check, x, ptr, size) \
  123. ({ \
  124. const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  125. __typeof__ (size) __gu_size = (size); \
  126. long __gu_err = -EFAULT; \
  127. unsigned long __gu_val = 0; \
  128. if (!check || __access_ok(__gu_ptr, size)) \
  129. switch (__gu_size) { \
  130. case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break; \
  131. case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break; \
  132. case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break; \
  133. case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break; \
  134. default: __get_user_unknown(); break; \
  135. } \
  136. (x) = (__force __typeof__(*(__gu_ptr))) __gu_val; \
  137. __gu_err; \
  138. })
  139. #define __get_user_nocheck(x, ptr, size) __do_get_user(0, x, ptr, size)
  140. #define __get_user_check(x, ptr, size) __do_get_user(1, x, ptr, size)
  141. extern void __put_user_unknown (void);
  142. /*
  143. * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
  144. * could clobber r8 (among others). Thus, be careful not to evaluate them while using r8.
  145. */
  146. #define __do_put_user(check, x, ptr, size) \
  147. ({ \
  148. __typeof__ (x) __pu_x = (x); \
  149. __typeof__ (*(ptr)) __user *__pu_ptr = (ptr); \
  150. __typeof__ (size) __pu_size = (size); \
  151. long __pu_err = -EFAULT; \
  152. \
  153. if (!check || __access_ok(__pu_ptr, __pu_size)) \
  154. switch (__pu_size) { \
  155. case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break; \
  156. case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break; \
  157. case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break; \
  158. case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break; \
  159. default: __put_user_unknown(); break; \
  160. } \
  161. __pu_err; \
  162. })
  163. #define __put_user_nocheck(x, ptr, size) __do_put_user(0, x, ptr, size)
  164. #define __put_user_check(x, ptr, size) __do_put_user(1, x, ptr, size)
  165. /*
  166. * Complex access routines
  167. */
  168. extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
  169. unsigned long count);
  170. static inline unsigned long
  171. raw_copy_to_user(void __user *to, const void *from, unsigned long count)
  172. {
  173. return __copy_user(to, (__force void __user *) from, count);
  174. }
  175. static inline unsigned long
  176. raw_copy_from_user(void *to, const void __user *from, unsigned long count)
  177. {
  178. return __copy_user((__force void __user *) to, from, count);
  179. }
  180. #define INLINE_COPY_FROM_USER
  181. #define INLINE_COPY_TO_USER
  182. extern unsigned long __do_clear_user (void __user *, unsigned long);
  183. #define __clear_user(to, n) __do_clear_user(to, n)
  184. #define clear_user(to, n) \
  185. ({ \
  186. unsigned long __cu_len = (n); \
  187. if (__access_ok(to, __cu_len)) \
  188. __cu_len = __do_clear_user(to, __cu_len); \
  189. __cu_len; \
  190. })
  191. /*
  192. * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
  193. * strlen.
  194. */
  195. extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
  196. #define strncpy_from_user(to, from, n) \
  197. ({ \
  198. const char __user * __sfu_from = (from); \
  199. long __sfu_ret = -EFAULT; \
  200. if (__access_ok(__sfu_from, 0)) \
  201. __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
  202. __sfu_ret; \
  203. })
  204. /*
  205. * Returns: 0 if exception before NUL or reaching the supplied limit
  206. * (N), a value greater than N if the limit would be exceeded, else
  207. * strlen.
  208. */
  209. extern unsigned long __strnlen_user (const char __user *, long);
  210. #define strnlen_user(str, len) \
  211. ({ \
  212. const char __user *__su_str = (str); \
  213. unsigned long __su_ret = 0; \
  214. if (__access_ok(__su_str, 0)) \
  215. __su_ret = __strnlen_user(__su_str, len); \
  216. __su_ret; \
  217. })
  218. #define ARCH_HAS_TRANSLATE_MEM_PTR 1
  219. static __inline__ void *
  220. xlate_dev_mem_ptr(phys_addr_t p)
  221. {
  222. struct page *page;
  223. void *ptr;
  224. page = pfn_to_page(p >> PAGE_SHIFT);
  225. if (PageUncached(page))
  226. ptr = (void *)p + __IA64_UNCACHED_OFFSET;
  227. else
  228. ptr = __va(p);
  229. return ptr;
  230. }
  231. #endif /* _ASM_IA64_UACCESS_H */