uaccess.h 7.4 KB

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