uaccess.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /*
  27. * The fs value determines whether argument validity checking should be
  28. * performed or not. If get_fs() == USER_DS, checking is performed, with
  29. * get_fs() == KERNEL_DS, checking is bypassed.
  30. *
  31. * For historical reasons, these macros are grossly misnamed.
  32. */
  33. /* addr_limit is the maximum accessible address for the task. we misuse
  34. * the KERNEL_DS and USER_DS values to both assign and compare the
  35. * addr_limit values through the equally misnamed get/set_fs macros.
  36. * (see above)
  37. */
  38. #define KERNEL_DS (~0UL)
  39. #define get_ds() (KERNEL_DS)
  40. #define USER_DS (TASK_SIZE)
  41. #define get_fs() (current_thread_info()->addr_limit)
  42. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  43. #define segment_eq(a, b) ((a) == (b))
  44. /* Ensure that the range from addr to addr+size is all within the process'
  45. * address space
  46. */
  47. #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
  48. /* Ensure that addr is below task's addr_limit */
  49. #define __addr_ok(addr) ((unsigned long) addr < get_fs())
  50. #define access_ok(type, addr, size) \
  51. __range_ok((unsigned long)addr, (unsigned long)size)
  52. /*
  53. * The exception table consists of pairs of addresses: the first is the
  54. * address of an instruction that is allowed to fault, and the second is
  55. * the address at which the program should continue. No registers are
  56. * modified, so it is entirely up to the continuation code to figure out
  57. * what to do.
  58. *
  59. * All the routines below use bits of fixup code that are out of line
  60. * with the main instruction path. This means when everything is well,
  61. * we don't even have to jump over them. Further, they do not intrude
  62. * on our cache or tlb entries.
  63. */
  64. struct exception_table_entry {
  65. unsigned long insn, fixup;
  66. };
  67. /*
  68. * These are the main single-value transfer routines. They automatically
  69. * use the right size if we just have the right pointer type.
  70. *
  71. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  72. * and yet we don't want to do any pointers, because that is too much
  73. * of a performance impact. Thus we have a few rather ugly macros here,
  74. * and hide all the uglyness from the user.
  75. *
  76. * The "__xxx" versions of the user access functions are versions that
  77. * do not verify the address space, that must have been done previously
  78. * with a separate "access_ok()" call (this is used when we do multiple
  79. * accesses to the same area of user memory).
  80. *
  81. * As we use the same address space for kernel and user data on the
  82. * PowerPC, we can just do these as direct assignments. (Of course, the
  83. * exception handling means that it's no longer "just"...)
  84. */
  85. #define get_user(x, ptr) \
  86. __get_user_check((x), (ptr), sizeof(*(ptr)))
  87. #define put_user(x, ptr) \
  88. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  89. #define __get_user(x, ptr) \
  90. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  91. #define __put_user(x, ptr) \
  92. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  93. extern long __put_user_bad(void);
  94. #define __put_user_nocheck(x, ptr, size) \
  95. ({ \
  96. long __pu_err; \
  97. __put_user_size((x), (ptr), (size), __pu_err); \
  98. __pu_err; \
  99. })
  100. #define __put_user_check(x, ptr, size) \
  101. ({ \
  102. long __pu_err = -EFAULT; \
  103. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  104. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  105. __put_user_size((x), __pu_addr, (size), __pu_err); \
  106. __pu_err; \
  107. })
  108. #define __put_user_size(x, ptr, size, retval) \
  109. do { \
  110. retval = 0; \
  111. switch (size) { \
  112. case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
  113. case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
  114. case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
  115. case 8: __put_user_asm2(x, ptr, retval); break; \
  116. default: __put_user_bad(); \
  117. } \
  118. } while (0)
  119. struct __large_struct {
  120. unsigned long buf[100];
  121. };
  122. #define __m(x) (*(struct __large_struct *)(x))
  123. /*
  124. * We don't tell gcc that we are accessing memory, but this is OK
  125. * because we do not write to any memory gcc knows about, so there
  126. * are no aliasing issues.
  127. */
  128. #define __put_user_asm(x, addr, err, op) \
  129. __asm__ __volatile__( \
  130. "1: "op" 0(%2),%1\n" \
  131. "2:\n" \
  132. ".section .fixup,\"ax\"\n" \
  133. "3: l.addi %0,r0,%3\n" \
  134. " l.j 2b\n" \
  135. " l.nop\n" \
  136. ".previous\n" \
  137. ".section __ex_table,\"a\"\n" \
  138. " .align 2\n" \
  139. " .long 1b,3b\n" \
  140. ".previous" \
  141. : "=r"(err) \
  142. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  143. #define __put_user_asm2(x, addr, err) \
  144. __asm__ __volatile__( \
  145. "1: l.sw 0(%2),%1\n" \
  146. "2: l.sw 4(%2),%H1\n" \
  147. "3:\n" \
  148. ".section .fixup,\"ax\"\n" \
  149. "4: l.addi %0,r0,%3\n" \
  150. " l.j 3b\n" \
  151. " l.nop\n" \
  152. ".previous\n" \
  153. ".section __ex_table,\"a\"\n" \
  154. " .align 2\n" \
  155. " .long 1b,4b\n" \
  156. " .long 2b,4b\n" \
  157. ".previous" \
  158. : "=r"(err) \
  159. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  160. #define __get_user_nocheck(x, ptr, size) \
  161. ({ \
  162. long __gu_err, __gu_val; \
  163. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  164. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  165. __gu_err; \
  166. })
  167. #define __get_user_check(x, ptr, size) \
  168. ({ \
  169. long __gu_err = -EFAULT, __gu_val = 0; \
  170. const __typeof__(*(ptr)) * __gu_addr = (ptr); \
  171. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  172. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  173. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  174. __gu_err; \
  175. })
  176. extern long __get_user_bad(void);
  177. #define __get_user_size(x, ptr, size, retval) \
  178. do { \
  179. retval = 0; \
  180. switch (size) { \
  181. case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
  182. case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
  183. case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
  184. case 8: __get_user_asm2(x, ptr, retval); \
  185. default: (x) = __get_user_bad(); \
  186. } \
  187. } while (0)
  188. #define __get_user_asm(x, addr, err, op) \
  189. __asm__ __volatile__( \
  190. "1: "op" %1,0(%2)\n" \
  191. "2:\n" \
  192. ".section .fixup,\"ax\"\n" \
  193. "3: l.addi %0,r0,%3\n" \
  194. " l.addi %1,r0,0\n" \
  195. " l.j 2b\n" \
  196. " l.nop\n" \
  197. ".previous\n" \
  198. ".section __ex_table,\"a\"\n" \
  199. " .align 2\n" \
  200. " .long 1b,3b\n" \
  201. ".previous" \
  202. : "=r"(err), "=r"(x) \
  203. : "r"(addr), "i"(-EFAULT), "0"(err))
  204. #define __get_user_asm2(x, addr, err) \
  205. __asm__ __volatile__( \
  206. "1: l.lwz %1,0(%2)\n" \
  207. "2: l.lwz %H1,4(%2)\n" \
  208. "3:\n" \
  209. ".section .fixup,\"ax\"\n" \
  210. "4: l.addi %0,r0,%3\n" \
  211. " l.addi %1,r0,0\n" \
  212. " l.addi %H1,r0,0\n" \
  213. " l.j 3b\n" \
  214. " l.nop\n" \
  215. ".previous\n" \
  216. ".section __ex_table,\"a\"\n" \
  217. " .align 2\n" \
  218. " .long 1b,4b\n" \
  219. " .long 2b,4b\n" \
  220. ".previous" \
  221. : "=r"(err), "=&r"(x) \
  222. : "r"(addr), "i"(-EFAULT), "0"(err))
  223. /* more complex routines */
  224. extern unsigned long __must_check
  225. __copy_tofrom_user(void *to, const void *from, unsigned long size);
  226. #define __copy_from_user(to, from, size) \
  227. __copy_tofrom_user(to, from, size)
  228. #define __copy_to_user(to, from, size) \
  229. __copy_tofrom_user(to, from, size)
  230. #define __copy_to_user_inatomic __copy_to_user
  231. #define __copy_from_user_inatomic __copy_from_user
  232. static inline unsigned long
  233. copy_from_user(void *to, const void *from, unsigned long n)
  234. {
  235. unsigned long res = n;
  236. if (likely(access_ok(VERIFY_READ, from, n)))
  237. res = __copy_tofrom_user(to, from, n);
  238. if (unlikely(res))
  239. memset(to + (n - res), 0, res);
  240. return res;
  241. }
  242. static inline unsigned long
  243. copy_to_user(void *to, const void *from, unsigned long n)
  244. {
  245. if (likely(access_ok(VERIFY_WRITE, to, n)))
  246. n = __copy_tofrom_user(to, from, n);
  247. return n;
  248. }
  249. extern unsigned long __clear_user(void *addr, unsigned long size);
  250. static inline __must_check unsigned long
  251. clear_user(void *addr, unsigned long size)
  252. {
  253. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  254. size = __clear_user(addr, size);
  255. return size;
  256. }
  257. #define user_addr_max() \
  258. (uaccess_kernel() ? ~0UL : TASK_SIZE)
  259. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  260. extern __must_check long strlen_user(const char __user *str);
  261. extern __must_check long strnlen_user(const char __user *str, long n);
  262. #endif /* __ASM_OPENRISC_UACCESS_H */