uaccess.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. ({ \
  53. unsigned long __ao_addr = (unsigned long)(addr); \
  54. unsigned long __ao_size = (unsigned long)(size); \
  55. __range_ok(__ao_addr, __ao_size); \
  56. })
  57. /*
  58. * These are the main single-value transfer routines. They automatically
  59. * use the right size if we just have the right pointer type.
  60. *
  61. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  62. * and yet we don't want to do any pointers, because that is too much
  63. * of a performance impact. Thus we have a few rather ugly macros here,
  64. * and hide all the uglyness from the user.
  65. *
  66. * The "__xxx" versions of the user access functions are versions that
  67. * do not verify the address space, that must have been done previously
  68. * with a separate "access_ok()" call (this is used when we do multiple
  69. * accesses to the same area of user memory).
  70. *
  71. * As we use the same address space for kernel and user data on the
  72. * PowerPC, we can just do these as direct assignments. (Of course, the
  73. * exception handling means that it's no longer "just"...)
  74. */
  75. #define get_user(x, ptr) \
  76. __get_user_check((x), (ptr), sizeof(*(ptr)))
  77. #define put_user(x, ptr) \
  78. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  79. #define __get_user(x, ptr) \
  80. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  81. #define __put_user(x, ptr) \
  82. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  83. extern long __put_user_bad(void);
  84. #define __put_user_nocheck(x, ptr, size) \
  85. ({ \
  86. long __pu_err; \
  87. __put_user_size((x), (ptr), (size), __pu_err); \
  88. __pu_err; \
  89. })
  90. #define __put_user_check(x, ptr, size) \
  91. ({ \
  92. long __pu_err = -EFAULT; \
  93. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  94. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  95. __put_user_size((x), __pu_addr, (size), __pu_err); \
  96. __pu_err; \
  97. })
  98. #define __put_user_size(x, ptr, size, retval) \
  99. do { \
  100. retval = 0; \
  101. switch (size) { \
  102. case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
  103. case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
  104. case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
  105. case 8: __put_user_asm2(x, ptr, retval); break; \
  106. default: __put_user_bad(); \
  107. } \
  108. } while (0)
  109. struct __large_struct {
  110. unsigned long buf[100];
  111. };
  112. #define __m(x) (*(struct __large_struct *)(x))
  113. /*
  114. * We don't tell gcc that we are accessing memory, but this is OK
  115. * because we do not write to any memory gcc knows about, so there
  116. * are no aliasing issues.
  117. */
  118. #define __put_user_asm(x, addr, err, op) \
  119. __asm__ __volatile__( \
  120. "1: "op" 0(%2),%1\n" \
  121. "2:\n" \
  122. ".section .fixup,\"ax\"\n" \
  123. "3: l.addi %0,r0,%3\n" \
  124. " l.j 2b\n" \
  125. " l.nop\n" \
  126. ".previous\n" \
  127. ".section __ex_table,\"a\"\n" \
  128. " .align 2\n" \
  129. " .long 1b,3b\n" \
  130. ".previous" \
  131. : "=r"(err) \
  132. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  133. #define __put_user_asm2(x, addr, err) \
  134. __asm__ __volatile__( \
  135. "1: l.sw 0(%2),%1\n" \
  136. "2: l.sw 4(%2),%H1\n" \
  137. "3:\n" \
  138. ".section .fixup,\"ax\"\n" \
  139. "4: l.addi %0,r0,%3\n" \
  140. " l.j 3b\n" \
  141. " l.nop\n" \
  142. ".previous\n" \
  143. ".section __ex_table,\"a\"\n" \
  144. " .align 2\n" \
  145. " .long 1b,4b\n" \
  146. " .long 2b,4b\n" \
  147. ".previous" \
  148. : "=r"(err) \
  149. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  150. #define __get_user_nocheck(x, ptr, size) \
  151. ({ \
  152. long __gu_err, __gu_val; \
  153. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  154. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  155. __gu_err; \
  156. })
  157. #define __get_user_check(x, ptr, size) \
  158. ({ \
  159. long __gu_err = -EFAULT, __gu_val = 0; \
  160. const __typeof__(*(ptr)) * __gu_addr = (ptr); \
  161. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  162. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  163. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  164. __gu_err; \
  165. })
  166. extern long __get_user_bad(void);
  167. #define __get_user_size(x, ptr, size, retval) \
  168. do { \
  169. retval = 0; \
  170. switch (size) { \
  171. case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
  172. case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
  173. case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
  174. case 8: __get_user_asm2(x, ptr, retval); break; \
  175. default: (x) = __get_user_bad(); \
  176. } \
  177. } while (0)
  178. #define __get_user_asm(x, addr, err, op) \
  179. __asm__ __volatile__( \
  180. "1: "op" %1,0(%2)\n" \
  181. "2:\n" \
  182. ".section .fixup,\"ax\"\n" \
  183. "3: l.addi %0,r0,%3\n" \
  184. " l.addi %1,r0,0\n" \
  185. " l.j 2b\n" \
  186. " l.nop\n" \
  187. ".previous\n" \
  188. ".section __ex_table,\"a\"\n" \
  189. " .align 2\n" \
  190. " .long 1b,3b\n" \
  191. ".previous" \
  192. : "=r"(err), "=r"(x) \
  193. : "r"(addr), "i"(-EFAULT), "0"(err))
  194. #define __get_user_asm2(x, addr, err) \
  195. __asm__ __volatile__( \
  196. "1: l.lwz %1,0(%2)\n" \
  197. "2: l.lwz %H1,4(%2)\n" \
  198. "3:\n" \
  199. ".section .fixup,\"ax\"\n" \
  200. "4: l.addi %0,r0,%3\n" \
  201. " l.addi %1,r0,0\n" \
  202. " l.addi %H1,r0,0\n" \
  203. " l.j 3b\n" \
  204. " l.nop\n" \
  205. ".previous\n" \
  206. ".section __ex_table,\"a\"\n" \
  207. " .align 2\n" \
  208. " .long 1b,4b\n" \
  209. " .long 2b,4b\n" \
  210. ".previous" \
  211. : "=r"(err), "=&r"(x) \
  212. : "r"(addr), "i"(-EFAULT), "0"(err))
  213. /* more complex routines */
  214. extern unsigned long __must_check
  215. __copy_tofrom_user(void *to, const void *from, unsigned long size);
  216. static inline unsigned long
  217. raw_copy_from_user(void *to, const void __user *from, unsigned long size)
  218. {
  219. return __copy_tofrom_user(to, (__force const void *)from, size);
  220. }
  221. static inline unsigned long
  222. raw_copy_to_user(void *to, const void __user *from, unsigned long size)
  223. {
  224. return __copy_tofrom_user((__force void *)to, from, size);
  225. }
  226. #define INLINE_COPY_FROM_USER
  227. #define INLINE_COPY_TO_USER
  228. extern unsigned long __clear_user(void *addr, unsigned long size);
  229. static inline __must_check unsigned long
  230. clear_user(void *addr, unsigned long size)
  231. {
  232. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  233. size = __clear_user(addr, size);
  234. return size;
  235. }
  236. #define user_addr_max() \
  237. (uaccess_kernel() ? ~0UL : TASK_SIZE)
  238. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  239. extern __must_check long strnlen_user(const char __user *str, long n);
  240. #endif /* __ASM_OPENRISC_UACCESS_H */