uaccess.h 8.9 KB

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