uaccess.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /* Returns 0 if exception not found and fixup otherwise. */
  72. extern unsigned long search_exception_table(unsigned long);
  73. extern void sort_exception_table(void);
  74. /*
  75. * These are the main single-value transfer routines. They automatically
  76. * use the right size if we just have the right pointer type.
  77. *
  78. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  79. * and yet we don't want to do any pointers, because that is too much
  80. * of a performance impact. Thus we have a few rather ugly macros here,
  81. * and hide all the uglyness from the user.
  82. *
  83. * The "__xxx" versions of the user access functions are versions that
  84. * do not verify the address space, that must have been done previously
  85. * with a separate "access_ok()" call (this is used when we do multiple
  86. * accesses to the same area of user memory).
  87. *
  88. * As we use the same address space for kernel and user data on the
  89. * PowerPC, we can just do these as direct assignments. (Of course, the
  90. * exception handling means that it's no longer "just"...)
  91. */
  92. #define get_user(x, ptr) \
  93. __get_user_check((x), (ptr), sizeof(*(ptr)))
  94. #define put_user(x, ptr) \
  95. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  96. #define __get_user(x, ptr) \
  97. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  98. #define __put_user(x, ptr) \
  99. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  100. extern long __put_user_bad(void);
  101. #define __put_user_nocheck(x, ptr, size) \
  102. ({ \
  103. long __pu_err; \
  104. __put_user_size((x), (ptr), (size), __pu_err); \
  105. __pu_err; \
  106. })
  107. #define __put_user_check(x, ptr, size) \
  108. ({ \
  109. long __pu_err = -EFAULT; \
  110. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  111. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  112. __put_user_size((x), __pu_addr, (size), __pu_err); \
  113. __pu_err; \
  114. })
  115. #define __put_user_size(x, ptr, size, retval) \
  116. do { \
  117. retval = 0; \
  118. switch (size) { \
  119. case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
  120. case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
  121. case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
  122. case 8: __put_user_asm2(x, ptr, retval); break; \
  123. default: __put_user_bad(); \
  124. } \
  125. } while (0)
  126. struct __large_struct {
  127. unsigned long buf[100];
  128. };
  129. #define __m(x) (*(struct __large_struct *)(x))
  130. /*
  131. * We don't tell gcc that we are accessing memory, but this is OK
  132. * because we do not write to any memory gcc knows about, so there
  133. * are no aliasing issues.
  134. */
  135. #define __put_user_asm(x, addr, err, op) \
  136. __asm__ __volatile__( \
  137. "1: "op" 0(%2),%1\n" \
  138. "2:\n" \
  139. ".section .fixup,\"ax\"\n" \
  140. "3: l.addi %0,r0,%3\n" \
  141. " l.j 2b\n" \
  142. " l.nop\n" \
  143. ".previous\n" \
  144. ".section __ex_table,\"a\"\n" \
  145. " .align 2\n" \
  146. " .long 1b,3b\n" \
  147. ".previous" \
  148. : "=r"(err) \
  149. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  150. #define __put_user_asm2(x, addr, err) \
  151. __asm__ __volatile__( \
  152. "1: l.sw 0(%2),%1\n" \
  153. "2: l.sw 4(%2),%H1\n" \
  154. "3:\n" \
  155. ".section .fixup,\"ax\"\n" \
  156. "4: l.addi %0,r0,%3\n" \
  157. " l.j 3b\n" \
  158. " l.nop\n" \
  159. ".previous\n" \
  160. ".section __ex_table,\"a\"\n" \
  161. " .align 2\n" \
  162. " .long 1b,4b\n" \
  163. " .long 2b,4b\n" \
  164. ".previous" \
  165. : "=r"(err) \
  166. : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
  167. #define __get_user_nocheck(x, ptr, size) \
  168. ({ \
  169. long __gu_err, __gu_val; \
  170. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  171. (x) = (__typeof__(*(ptr)))__gu_val; \
  172. __gu_err; \
  173. })
  174. #define __get_user_check(x, ptr, size) \
  175. ({ \
  176. long __gu_err = -EFAULT, __gu_val = 0; \
  177. const __typeof__(*(ptr)) * __gu_addr = (ptr); \
  178. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  179. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  180. (x) = (__typeof__(*(ptr)))__gu_val; \
  181. __gu_err; \
  182. })
  183. extern long __get_user_bad(void);
  184. #define __get_user_size(x, ptr, size, retval) \
  185. do { \
  186. retval = 0; \
  187. switch (size) { \
  188. case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
  189. case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
  190. case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
  191. case 8: __get_user_asm2(x, ptr, retval); \
  192. default: (x) = __get_user_bad(); \
  193. } \
  194. } while (0)
  195. #define __get_user_asm(x, addr, err, op) \
  196. __asm__ __volatile__( \
  197. "1: "op" %1,0(%2)\n" \
  198. "2:\n" \
  199. ".section .fixup,\"ax\"\n" \
  200. "3: l.addi %0,r0,%3\n" \
  201. " l.addi %1,r0,0\n" \
  202. " l.j 2b\n" \
  203. " l.nop\n" \
  204. ".previous\n" \
  205. ".section __ex_table,\"a\"\n" \
  206. " .align 2\n" \
  207. " .long 1b,3b\n" \
  208. ".previous" \
  209. : "=r"(err), "=r"(x) \
  210. : "r"(addr), "i"(-EFAULT), "0"(err))
  211. #define __get_user_asm2(x, addr, err) \
  212. __asm__ __volatile__( \
  213. "1: l.lwz %1,0(%2)\n" \
  214. "2: l.lwz %H1,4(%2)\n" \
  215. "3:\n" \
  216. ".section .fixup,\"ax\"\n" \
  217. "4: l.addi %0,r0,%3\n" \
  218. " l.addi %1,r0,0\n" \
  219. " l.addi %H1,r0,0\n" \
  220. " l.j 3b\n" \
  221. " l.nop\n" \
  222. ".previous\n" \
  223. ".section __ex_table,\"a\"\n" \
  224. " .align 2\n" \
  225. " .long 1b,4b\n" \
  226. " .long 2b,4b\n" \
  227. ".previous" \
  228. : "=r"(err), "=&r"(x) \
  229. : "r"(addr), "i"(-EFAULT), "0"(err))
  230. /* more complex routines */
  231. extern unsigned long __must_check
  232. __copy_tofrom_user(void *to, const void *from, unsigned long size);
  233. #define __copy_from_user(to, from, size) \
  234. __copy_tofrom_user(to, from, size)
  235. #define __copy_to_user(to, from, size) \
  236. __copy_tofrom_user(to, from, size)
  237. #define __copy_to_user_inatomic __copy_to_user
  238. #define __copy_from_user_inatomic __copy_from_user
  239. static inline unsigned long
  240. copy_from_user(void *to, const void *from, unsigned long n)
  241. {
  242. unsigned long over;
  243. if (access_ok(VERIFY_READ, from, n))
  244. return __copy_tofrom_user(to, from, n);
  245. if ((unsigned long)from < TASK_SIZE) {
  246. over = (unsigned long)from + n - TASK_SIZE;
  247. return __copy_tofrom_user(to, from, n - over) + over;
  248. }
  249. return n;
  250. }
  251. static inline unsigned long
  252. copy_to_user(void *to, const void *from, unsigned long n)
  253. {
  254. unsigned long over;
  255. if (access_ok(VERIFY_WRITE, to, n))
  256. return __copy_tofrom_user(to, from, n);
  257. if ((unsigned long)to < TASK_SIZE) {
  258. over = (unsigned long)to + n - TASK_SIZE;
  259. return __copy_tofrom_user(to, from, n - over) + over;
  260. }
  261. return n;
  262. }
  263. extern unsigned long __clear_user(void *addr, unsigned long size);
  264. static inline __must_check unsigned long
  265. clear_user(void *addr, unsigned long size)
  266. {
  267. if (access_ok(VERIFY_WRITE, addr, size))
  268. return __clear_user(addr, size);
  269. if ((unsigned long)addr < TASK_SIZE) {
  270. unsigned long over = (unsigned long)addr + size - TASK_SIZE;
  271. return __clear_user(addr, size - over) + over;
  272. }
  273. return size;
  274. }
  275. extern int __strncpy_from_user(char *dst, const char *src, long count);
  276. static inline long strncpy_from_user(char *dst, const char *src, long count)
  277. {
  278. if (access_ok(VERIFY_READ, src, 1))
  279. return __strncpy_from_user(dst, src, count);
  280. return -EFAULT;
  281. }
  282. /*
  283. * Return the size of a string (including the ending 0)
  284. *
  285. * Return 0 for error
  286. */
  287. extern int __strnlen_user(const char *str, long len, unsigned long top);
  288. /*
  289. * Returns the length of the string at str (including the null byte),
  290. * or 0 if we hit a page we can't access,
  291. * or something > len if we didn't find a null byte.
  292. *
  293. * The `top' parameter to __strnlen_user is to make sure that
  294. * we can never overflow from the user area into kernel space.
  295. */
  296. static inline long strnlen_user(const char __user *str, long len)
  297. {
  298. unsigned long top = (unsigned long)get_fs();
  299. unsigned long res = 0;
  300. if (__addr_ok(str))
  301. res = __strnlen_user(str, len, top);
  302. return res;
  303. }
  304. #define strlen_user(str) strnlen_user(str, TASK_SIZE-1)
  305. #endif /* __ASM_OPENRISC_UACCESS_H */