uaccess.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later.
  5. *
  6. * Based on: include/asm-m68knommu/uaccess.h
  7. */
  8. #ifndef __BLACKFIN_UACCESS_H
  9. #define __BLACKFIN_UACCESS_H
  10. /*
  11. * User space memory access functions
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/string.h>
  16. #include <asm/segment.h>
  17. #include <asm/sections.h>
  18. #define get_ds() (KERNEL_DS)
  19. #define get_fs() (current_thread_info()->addr_limit)
  20. static inline void set_fs(mm_segment_t fs)
  21. {
  22. current_thread_info()->addr_limit = fs;
  23. }
  24. #define segment_eq(a,b) ((a) == (b))
  25. #define VERIFY_READ 0
  26. #define VERIFY_WRITE 1
  27. #define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
  28. /*
  29. * The fs value determines whether argument validity checking should be
  30. * performed or not. If get_fs() == USER_DS, checking is performed, with
  31. * get_fs() == KERNEL_DS, checking is bypassed.
  32. */
  33. #ifndef CONFIG_ACCESS_CHECK
  34. static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; }
  35. #else
  36. extern int _access_ok(unsigned long addr, unsigned long size);
  37. #endif
  38. /*
  39. * The exception table consists of pairs of addresses: the first is the
  40. * address of an instruction that is allowed to fault, and the second is
  41. * the address at which the program should continue. No registers are
  42. * modified, so it is entirely up to the continuation code to figure out
  43. * what to do.
  44. *
  45. * All the routines below use bits of fixup code that are out of line
  46. * with the main instruction path. This means when everything is well,
  47. * we don't even have to jump over them. Further, they do not intrude
  48. * on our cache or tlb entries.
  49. */
  50. struct exception_table_entry {
  51. unsigned long insn, fixup;
  52. };
  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. #define put_user(x,p) \
  58. ({ \
  59. int _err = 0; \
  60. typeof(*(p)) _x = (x); \
  61. typeof(*(p)) __user *_p = (p); \
  62. if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
  63. _err = -EFAULT; \
  64. } \
  65. else { \
  66. switch (sizeof (*(_p))) { \
  67. case 1: \
  68. __put_user_asm(_x, _p, B); \
  69. break; \
  70. case 2: \
  71. __put_user_asm(_x, _p, W); \
  72. break; \
  73. case 4: \
  74. __put_user_asm(_x, _p, ); \
  75. break; \
  76. case 8: { \
  77. long _xl, _xh; \
  78. _xl = ((long *)&_x)[0]; \
  79. _xh = ((long *)&_x)[1]; \
  80. __put_user_asm(_xl, ((long __user *)_p)+0, ); \
  81. __put_user_asm(_xh, ((long __user *)_p)+1, ); \
  82. } break; \
  83. default: \
  84. _err = __put_user_bad(); \
  85. break; \
  86. } \
  87. } \
  88. _err; \
  89. })
  90. #define __put_user(x,p) put_user(x,p)
  91. static inline int bad_user_access_length(void)
  92. {
  93. panic("bad_user_access_length");
  94. return -1;
  95. }
  96. #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
  97. __FILE__, __LINE__, __func__),\
  98. bad_user_access_length(), (-EFAULT))
  99. /*
  100. * Tell gcc we read from memory instead of writing: this is because
  101. * we do not write to any memory gcc knows about, so there are no
  102. * aliasing issues.
  103. */
  104. #define __ptr(x) ((unsigned long __force *)(x))
  105. #define __put_user_asm(x,p,bhw) \
  106. __asm__ (#bhw"[%1] = %0;\n\t" \
  107. : /* no outputs */ \
  108. :"d" (x),"a" (__ptr(p)) : "memory")
  109. #define get_user(x, ptr) \
  110. ({ \
  111. int _err = 0; \
  112. unsigned long _val = 0; \
  113. const typeof(*(ptr)) __user *_p = (ptr); \
  114. const size_t ptr_size = sizeof(*(_p)); \
  115. if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
  116. BUILD_BUG_ON(ptr_size >= 8); \
  117. switch (ptr_size) { \
  118. case 1: \
  119. __get_user_asm(_val, _p, B,(Z)); \
  120. break; \
  121. case 2: \
  122. __get_user_asm(_val, _p, W,(Z)); \
  123. break; \
  124. case 4: \
  125. __get_user_asm(_val, _p, , ); \
  126. break; \
  127. } \
  128. } else \
  129. _err = -EFAULT; \
  130. x = (typeof(*(ptr)))_val; \
  131. _err; \
  132. })
  133. #define __get_user(x,p) get_user(x,p)
  134. #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
  135. #define __get_user_asm(x, ptr, bhw, option) \
  136. ({ \
  137. __asm__ __volatile__ ( \
  138. "%0 =" #bhw "[%1]" #option ";" \
  139. : "=d" (x) \
  140. : "a" (__ptr(ptr))); \
  141. })
  142. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  143. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  144. #define __copy_to_user_inatomic __copy_to_user
  145. #define __copy_from_user_inatomic __copy_from_user
  146. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n))\
  147. return retval; })
  148. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n))\
  149. return retval; })
  150. static inline unsigned long __must_check
  151. copy_from_user(void *to, const void __user *from, unsigned long n)
  152. {
  153. if (access_ok(VERIFY_READ, from, n))
  154. memcpy(to, (const void __force *)from, n);
  155. else
  156. return n;
  157. return 0;
  158. }
  159. static inline unsigned long __must_check
  160. copy_to_user(void __user *to, const void *from, unsigned long n)
  161. {
  162. if (access_ok(VERIFY_WRITE, to, n))
  163. memcpy((void __force *)to, from, n);
  164. else
  165. return n;
  166. return 0;
  167. }
  168. /*
  169. * Copy a null terminated string from userspace.
  170. */
  171. static inline long __must_check
  172. strncpy_from_user(char *dst, const char __user *src, long count)
  173. {
  174. char *tmp;
  175. if (!access_ok(VERIFY_READ, src, 1))
  176. return -EFAULT;
  177. strncpy(dst, (const char __force *)src, count);
  178. for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
  179. return (tmp - dst);
  180. }
  181. /*
  182. * Get the size of a string in user space.
  183. * src: The string to measure
  184. * n: The maximum valid length
  185. *
  186. * Get the size of a NUL-terminated string in user space.
  187. *
  188. * Returns the size of the string INCLUDING the terminating NUL.
  189. * On exception, returns 0.
  190. * If the string is too long, returns a value greater than n.
  191. */
  192. static inline long __must_check strnlen_user(const char __user *src, long n)
  193. {
  194. if (!access_ok(VERIFY_READ, src, 1))
  195. return 0;
  196. return strnlen((const char __force *)src, n) + 1;
  197. }
  198. static inline long __must_check strlen_user(const char __user *src)
  199. {
  200. if (!access_ok(VERIFY_READ, src, 1))
  201. return 0;
  202. return strlen((const char __force *)src) + 1;
  203. }
  204. /*
  205. * Zero Userspace
  206. */
  207. static inline unsigned long __must_check
  208. __clear_user(void __user *to, unsigned long n)
  209. {
  210. if (!access_ok(VERIFY_WRITE, to, n))
  211. return n;
  212. memset((void __force *)to, 0, n);
  213. return 0;
  214. }
  215. #define clear_user(to, n) __clear_user(to, n)
  216. /* How to interpret these return values:
  217. * CORE: can be accessed by core load or dma memcpy
  218. * CORE_ONLY: can only be accessed by core load
  219. * DMA: can only be accessed by dma memcpy
  220. * IDMA: can only be accessed by interprocessor dma memcpy (BF561)
  221. * ITEST: can be accessed by isram memcpy or dma memcpy
  222. */
  223. enum {
  224. BFIN_MEM_ACCESS_CORE = 0,
  225. BFIN_MEM_ACCESS_CORE_ONLY,
  226. BFIN_MEM_ACCESS_DMA,
  227. BFIN_MEM_ACCESS_IDMA,
  228. BFIN_MEM_ACCESS_ITEST,
  229. };
  230. /**
  231. * bfin_mem_access_type() - what kind of memory access is required
  232. * @addr: the address to check
  233. * @size: number of bytes needed
  234. * @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above)
  235. */
  236. int bfin_mem_access_type(unsigned long addr, unsigned long size);
  237. #endif /* _BLACKFIN_UACCESS_H */