/kern_2.6.32/arch/blackfin/include/asm/uaccess.h

http://omnia2droid.googlecode.com/ · C++ Header · 293 lines · 179 code · 39 blank · 75 comment · 18 complexity · be1179aebee371e0af37ebe3c6c309f0 MD5 · raw file

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