uaccess.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * OpenRISC Linux
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * OpenRISC implementation:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. * et al.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #ifndef __ASM_OPENRISC_UACCESS_H
  19. #define __ASM_OPENRISC_UACCESS_H
  20. /*
  21. * User space memory access functions
  22. */
  23. #include <linux/prefetch.h>
  24. #include <linux/string.h>
  25. #include <asm/page.h>
  26. #include <asm/extable.h>
  27. /*
  28. * The fs value determines whether argument validity checking should be
  29. * performed or not. If get_fs() == USER_DS, checking is performed, with
  30. * get_fs() == KERNEL_DS, checking is bypassed.
  31. *
  32. * For historical reasons, these macros are grossly misnamed.
  33. */
  34. /* addr_limit is the maximum accessible address for the task. we misuse
  35. * the KERNEL_DS and USER_DS values to both assign and compare the
  36. * addr_limit values through the equally misnamed get/set_fs macros.
  37. * (see above)
  38. */
  39. #define KERNEL_DS (~0UL)
  40. #define get_ds() (KERNEL_DS)
  41. #define USER_DS (TASK_SIZE)
  42. #define get_fs() (current_thread_info()->addr_limit)
  43. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  44. #define segment_eq(a, b) ((a) == (b))
  45. /* Ensure that the range from addr to addr+size is all within the process'
  46. * address space
  47. */
  48. #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
  49. /* Ensure that addr is below task's addr_limit */
  50. #define __addr_ok(addr) ((unsigned long) addr < get_fs())
  51. #define access_ok(type, addr, size) \
  52. __range_ok((unsigned long)addr, (unsigned long)size)
  53. /*
  54. * These are the main single-value transfer routines. They automatically
  55. * use the right size if we just have the right pointer type.
  56. *
  57. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  58. * and yet we don't want to do any pointers, because that is too much
  59. * of a performance impact. Thus we have a few rather ugly macros here,
  60. * and hide all the uglyness from the user.
  61. *
  62. * The "__xxx" versions of the user access functions are versions that
  63. * do not verify the address space, that must have been done previously
  64. * with a separate "access_ok()" call (this is used when we do multiple
  65. * accesses to the same area of user memory).
  66. *
  67. * As we use the same address space for kernel and user data on the
  68. * PowerPC, we can just do these as direct assignments. (Of course, the
  69. * exception handling means that it's no longer "just"...)
  70. */
  71. #define get_user(x, ptr) \
  72. __get_user_check((x), (ptr), sizeof(*(ptr)))
  73. #define put_user(x, ptr) \
  74. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  75. #define __get_user(x, ptr) \
  76. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  77. #define __put_user(x, ptr) \
  78. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  79. extern long __put_user_bad(void);
  80. #define __put_user_nocheck(x, ptr, size) \
  81. ({ \
  82. long __pu_err; \
  83. __put_user_size((x), (ptr), (size), __pu_err); \
  84. __pu_err; \
  85. })
  86. #define __put_user_check(x, ptr, size) \
  87. ({ \
  88. long __pu_err = -EFAULT; \
  89. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  90. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  91. __put_user_size((x), __pu_addr, (size), __pu_err); \
  92. __pu_err; \
  93. })
  94. #define __put_user_size(x, ptr, size, retval) \
  95. do { \
  96. retval = 0; \
  97. switch (size) { \
  98. case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
  99. case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
  100. case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
  101. case 8: __put_user_asm2(x, ptr, retval); break; \
  102. default: __put_user_bad(); \
  103. } \
  104. } while (0)
  105. struct __large_struct {
  106. unsigned long buf[100];
  107. };
  108. #define __m(x) (*(struct __large_struct *)(x))
  109. /*
  110. * We don't tell gcc that we are accessing memory, but this is OK
  111. * because we do not write to any memory gcc knows about, so there
  112. * are no aliasing issues.
  113. */
  114. #define __put_user_asm(x, addr, err, op) \
  115. __asm__ __volatile__( \
  116. "1: "op" 0(%2),%1\n" \
  117. "2:\n" \
  118. ".section .fixup,\"ax\"\n" \
  119. "3: l.addi %0,r0,%3\n" \
  120. " l.j 2b\n" \
  121. " l.nop\n" \
  122. ".previous\n" \
  123. ".section __ex_table,\"a\"\n" \
  124. " .align 2\n" \
  125. " .long 1b,3b\n" \
  126. ".previous" \
  127. : "=r"(err) \
  128. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  129. #define __put_user_asm2(x, addr, err) \
  130. __asm__ __volatile__( \
  131. "1: l.sw 0(%2),%1\n" \
  132. "2: l.sw 4(%2),%H1\n" \
  133. "3:\n" \
  134. ".section .fixup,\"ax\"\n" \
  135. "4: l.addi %0,r0,%3\n" \
  136. " l.j 3b\n" \
  137. " l.nop\n" \
  138. ".previous\n" \
  139. ".section __ex_table,\"a\"\n" \
  140. " .align 2\n" \
  141. " .long 1b,4b\n" \
  142. " .long 2b,4b\n" \
  143. ".previous" \
  144. : "=r"(err) \
  145. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  146. #define __get_user_nocheck(x, ptr, size) \
  147. ({ \
  148. long __gu_err, __gu_val; \
  149. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  150. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  151. __gu_err; \
  152. })
  153. #define __get_user_check(x, ptr, size) \
  154. ({ \
  155. long __gu_err = -EFAULT, __gu_val = 0; \
  156. const __typeof__(*(ptr)) * __gu_addr = (ptr); \
  157. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  158. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  159. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  160. __gu_err; \
  161. })
  162. extern long __get_user_bad(void);
  163. #define __get_user_size(x, ptr, size, retval) \
  164. do { \
  165. retval = 0; \
  166. switch (size) { \
  167. case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
  168. case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
  169. case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
  170. case 8: __get_user_asm2(x, ptr, retval); break; \
  171. default: (x) = __get_user_bad(); \
  172. } \
  173. } while (0)
  174. #define __get_user_asm(x, addr, err, op) \
  175. __asm__ __volatile__( \
  176. "1: "op" %1,0(%2)\n" \
  177. "2:\n" \
  178. ".section .fixup,\"ax\"\n" \
  179. "3: l.addi %0,r0,%3\n" \
  180. " l.addi %1,r0,0\n" \
  181. " l.j 2b\n" \
  182. " l.nop\n" \
  183. ".previous\n" \
  184. ".section __ex_table,\"a\"\n" \
  185. " .align 2\n" \
  186. " .long 1b,3b\n" \
  187. ".previous" \
  188. : "=r"(err), "=r"(x) \
  189. : "r"(addr), "i"(-EFAULT), "0"(err))
  190. #define __get_user_asm2(x, addr, err) \
  191. __asm__ __volatile__( \
  192. "1: l.lwz %1,0(%2)\n" \
  193. "2: l.lwz %H1,4(%2)\n" \
  194. "3:\n" \
  195. ".section .fixup,\"ax\"\n" \
  196. "4: l.addi %0,r0,%3\n" \
  197. " l.addi %1,r0,0\n" \
  198. " l.addi %H1,r0,0\n" \
  199. " l.j 3b\n" \
  200. " l.nop\n" \
  201. ".previous\n" \
  202. ".section __ex_table,\"a\"\n" \
  203. " .align 2\n" \
  204. " .long 1b,4b\n" \
  205. " .long 2b,4b\n" \
  206. ".previous" \
  207. : "=r"(err), "=&r"(x) \
  208. : "r"(addr), "i"(-EFAULT), "0"(err))
  209. /* more complex routines */
  210. extern unsigned long __must_check
  211. __copy_tofrom_user(void *to, const void *from, unsigned long size);
  212. static inline unsigned long
  213. raw_copy_from_user(void *to, const void __user *from, unsigned long size)
  214. {
  215. return __copy_tofrom_user(to, (__force const void *)from, size);
  216. }
  217. static inline unsigned long
  218. raw_copy_to_user(void *to, const void __user *from, unsigned long size)
  219. {
  220. return __copy_tofrom_user((__force void *)to, from, size);
  221. }
  222. #define INLINE_COPY_FROM_USER
  223. #define INLINE_COPY_TO_USER
  224. extern unsigned long __clear_user(void *addr, unsigned long size);
  225. static inline __must_check unsigned long
  226. clear_user(void *addr, unsigned long size)
  227. {
  228. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  229. size = __clear_user(addr, size);
  230. return size;
  231. }
  232. #define user_addr_max() \
  233. (uaccess_kernel() ? ~0UL : TASK_SIZE)
  234. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  235. extern __must_check long strnlen_user(const char __user *str, long n);
  236. #endif /* __ASM_OPENRISC_UACCESS_H */