uaccess.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifdef CONFIG_ACCESS_OK_L1
  51. extern int _access_ok(unsigned long addr, unsigned long size)__attribute__((l1_text));
  52. #else
  53. extern int _access_ok(unsigned long addr, unsigned long size);
  54. #endif
  55. #endif
  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. /*
  72. * These are the main single-value transfer routines. They automatically
  73. * use the right size if we just have the right pointer type.
  74. */
  75. #define put_user(x,p) \
  76. ({ \
  77. int _err = 0; \
  78. typeof(*(p)) _x = (x); \
  79. typeof(*(p)) *_p = (p); \
  80. if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
  81. _err = -EFAULT; \
  82. } \
  83. else { \
  84. switch (sizeof (*(_p))) { \
  85. case 1: \
  86. __put_user_asm(_x, _p, B); \
  87. break; \
  88. case 2: \
  89. __put_user_asm(_x, _p, W); \
  90. break; \
  91. case 4: \
  92. __put_user_asm(_x, _p, ); \
  93. break; \
  94. case 8: { \
  95. long _xl, _xh; \
  96. _xl = ((long *)&_x)[0]; \
  97. _xh = ((long *)&_x)[1]; \
  98. __put_user_asm(_xl, ((long *)_p)+0, ); \
  99. __put_user_asm(_xh, ((long *)_p)+1, ); \
  100. } break; \
  101. default: \
  102. _err = __put_user_bad(); \
  103. break; \
  104. } \
  105. } \
  106. _err; \
  107. })
  108. #define __put_user(x,p) put_user(x,p)
  109. static inline int bad_user_access_length(void)
  110. {
  111. panic("bad_user_access_length");
  112. return -1;
  113. }
  114. #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
  115. __FILE__, __LINE__, __func__),\
  116. bad_user_access_length(), (-EFAULT))
  117. /*
  118. * Tell gcc we read from memory instead of writing: this is because
  119. * we do not write to any memory gcc knows about, so there are no
  120. * aliasing issues.
  121. */
  122. #define __ptr(x) ((unsigned long *)(x))
  123. #define __put_user_asm(x,p,bhw) \
  124. __asm__ (#bhw"[%1] = %0;\n\t" \
  125. : /* no outputs */ \
  126. :"d" (x),"a" (__ptr(p)) : "memory")
  127. #define get_user(x, ptr) \
  128. ({ \
  129. int _err = 0; \
  130. unsigned long _val = 0; \
  131. const typeof(*(ptr)) __user *_p = (ptr); \
  132. const size_t ptr_size = sizeof(*(_p)); \
  133. if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
  134. BUILD_BUG_ON(ptr_size >= 8); \
  135. switch (ptr_size) { \
  136. case 1: \
  137. __get_user_asm(_val, _p, B,(Z)); \
  138. break; \
  139. case 2: \
  140. __get_user_asm(_val, _p, W,(Z)); \
  141. break; \
  142. case 4: \
  143. __get_user_asm(_val, _p, , ); \
  144. break; \
  145. } \
  146. } else \
  147. _err = -EFAULT; \
  148. x = (typeof(*(ptr)))_val; \
  149. _err; \
  150. })
  151. #define __get_user(x,p) get_user(x,p)
  152. #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
  153. #define __get_user_asm(x, ptr, bhw, option) \
  154. ({ \
  155. __asm__ __volatile__ ( \
  156. "%0 =" #bhw "[%1]" #option ";" \
  157. : "=d" (x) \
  158. : "a" (__ptr(ptr))); \
  159. })
  160. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  161. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  162. #define __copy_to_user_inatomic __copy_to_user
  163. #define __copy_from_user_inatomic __copy_from_user
  164. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n))\
  165. return retval; })
  166. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n))\
  167. return retval; })
  168. static inline unsigned long __must_check
  169. copy_from_user(void *to, const void __user *from, unsigned long n)
  170. {
  171. if (access_ok(VERIFY_READ, from, n))
  172. memcpy(to, from, n);
  173. else
  174. return n;
  175. return 0;
  176. }
  177. static inline unsigned long __must_check
  178. copy_to_user(void *to, const void __user *from, unsigned long n)
  179. {
  180. if (access_ok(VERIFY_WRITE, to, n))
  181. memcpy(to, from, n);
  182. else
  183. return n;
  184. return 0;
  185. }
  186. /*
  187. * Copy a null terminated string from userspace.
  188. */
  189. static inline long __must_check
  190. strncpy_from_user(char *dst, const char *src, long count)
  191. {
  192. char *tmp;
  193. if (!access_ok(VERIFY_READ, src, 1))
  194. return -EFAULT;
  195. strncpy(dst, src, count);
  196. for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
  197. return (tmp - dst);
  198. }
  199. /*
  200. * Get the size of a string in user space.
  201. * src: The string to measure
  202. * n: The maximum valid length
  203. *
  204. * Get the size of a NUL-terminated string in user space.
  205. *
  206. * Returns the size of the string INCLUDING the terminating NUL.
  207. * On exception, returns 0.
  208. * If the string is too long, returns a value greater than n.
  209. */
  210. static inline long __must_check strnlen_user(const char *src, long n)
  211. {
  212. if (!access_ok(VERIFY_READ, src, 1))
  213. return 0;
  214. return strnlen(src, n) + 1;
  215. }
  216. static inline long __must_check strlen_user(const char *src)
  217. {
  218. if (!access_ok(VERIFY_READ, src, 1))
  219. return 0;
  220. return strlen(src) + 1;
  221. }
  222. /*
  223. * Zero Userspace
  224. */
  225. static inline unsigned long __must_check
  226. __clear_user(void *to, unsigned long n)
  227. {
  228. if (!access_ok(VERIFY_WRITE, to, n))
  229. return n;
  230. memset(to, 0, n);
  231. return 0;
  232. }
  233. #define clear_user(to, n) __clear_user(to, n)
  234. #endif /* _BLACKFIN_UACCESS_H */