uaccess.h 6.8 KB

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