uaccess.h 7.4 KB

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