uaccess.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* uaccess.h: userspace accessor functions
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_UACCESS_H
  12. #define _ASM_UACCESS_H
  13. /*
  14. * User space memory access functions
  15. */
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <asm/segment.h>
  19. #include <asm/sections.h>
  20. #define HAVE_ARCH_UNMAPPED_AREA /* we decide where to put mmaps */
  21. #define __ptr(x) ((unsigned long *)(x))
  22. #define VERIFY_READ 0
  23. #define VERIFY_WRITE 1
  24. #define __addr_ok(addr) ((unsigned long)(addr) < get_addr_limit())
  25. /*
  26. * check that a range of addresses falls within the current address limit
  27. */
  28. static inline int ___range_ok(unsigned long addr, unsigned long size)
  29. {
  30. #ifdef CONFIG_MMU
  31. int flag = -EFAULT, tmp;
  32. asm volatile (
  33. " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
  34. " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
  35. " bc icc0,#0,0f \n"
  36. " bhi icc1,#0,0f \n"
  37. " setlos #0,%0 \n" /* mark okay */
  38. "0: \n"
  39. : "=r"(flag), "=&r"(tmp)
  40. : "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
  41. );
  42. return flag;
  43. #else
  44. if (addr < memory_start ||
  45. addr > memory_end ||
  46. size > memory_end - memory_start ||
  47. addr + size > memory_end)
  48. return -EFAULT;
  49. return 0;
  50. #endif
  51. }
  52. #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
  53. #define access_ok(type,addr,size) (__range_ok((addr), (size)) == 0)
  54. #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
  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. {
  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, ptr) \
  78. ({ \
  79. int __pu_err = 0; \
  80. \
  81. typeof(*(ptr)) __pu_val = (x); \
  82. \
  83. switch (sizeof (*(ptr))) { \
  84. case 1: \
  85. __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
  86. break; \
  87. case 2: \
  88. __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
  89. break; \
  90. case 4: \
  91. __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
  92. break; \
  93. case 8: \
  94. __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
  95. break; \
  96. default: \
  97. __pu_err = __put_user_bad(); \
  98. break; \
  99. } \
  100. __pu_err; \
  101. })
  102. #define put_user(x, ptr) \
  103. ({ \
  104. typeof(&*ptr) _p = (ptr); \
  105. int _e; \
  106. \
  107. _e = __range_ok(_p, sizeof(*_p)); \
  108. if (_e == 0) \
  109. _e = __put_user((x), _p); \
  110. _e; \
  111. })
  112. extern int __put_user_bad(void);
  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. #ifdef CONFIG_MMU
  119. #define __put_user_asm(err,x,ptr,dsize,constraint) \
  120. do { \
  121. asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
  122. "2: \n" \
  123. ".subsection 2 \n" \
  124. "3: setlos %3,%0 \n" \
  125. " bra 2b \n" \
  126. ".previous \n" \
  127. ".section __ex_table,\"a\" \n" \
  128. " .balign 8 \n" \
  129. " .long 1b,3b \n" \
  130. ".previous" \
  131. : "=r" (err) \
  132. : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
  133. : "memory"); \
  134. } while (0)
  135. #else
  136. #define __put_user_asm(err,x,ptr,bwl,con) \
  137. do { \
  138. asm(" st"bwl"%I0 %1,%M0 \n" \
  139. " membar \n" \
  140. : \
  141. : "m" (*__ptr(ptr)), con (x) \
  142. : "memory"); \
  143. } while (0)
  144. #endif
  145. /*****************************************************************************/
  146. /*
  147. *
  148. */
  149. #define __get_user(x, ptr) \
  150. ({ \
  151. typeof(*(ptr)) __gu_val = 0; \
  152. int __gu_err = 0; \
  153. \
  154. switch (sizeof(*(ptr))) { \
  155. case 1: \
  156. __get_user_asm(__gu_err, *(u8*)&__gu_val, ptr, "ub", "=r"); \
  157. break; \
  158. case 2: \
  159. __get_user_asm(__gu_err, *(u16*)&__gu_val, ptr, "uh", "=r"); \
  160. break; \
  161. case 4: \
  162. __get_user_asm(__gu_err, *(u32*)&__gu_val, ptr, "", "=r"); \
  163. break; \
  164. case 8: \
  165. __get_user_asm(__gu_err, *(u64*)&__gu_val, ptr, "d", "=e"); \
  166. break; \
  167. default: \
  168. __gu_err = __get_user_bad(); \
  169. break; \
  170. } \
  171. (x) = __gu_val; \
  172. __gu_err; \
  173. })
  174. #define get_user(x, ptr) \
  175. ({ \
  176. typeof(&*ptr) _p = (ptr); \
  177. int _e; \
  178. \
  179. _e = __range_ok(_p, sizeof(*_p)); \
  180. if (likely(_e == 0)) \
  181. _e = __get_user((x), _p); \
  182. else \
  183. (x) = (typeof(x)) 0; \
  184. _e; \
  185. })
  186. extern int __get_user_bad(void);
  187. #ifdef CONFIG_MMU
  188. #define __get_user_asm(err,x,ptr,dtype,constraint) \
  189. do { \
  190. asm("1: ld"dtype"%I2 %M2,%1 \n" \
  191. "2: \n" \
  192. ".subsection 2 \n" \
  193. "3: setlos %3,%0 \n" \
  194. " setlos #0,%1 \n" \
  195. " bra 2b \n" \
  196. ".previous \n" \
  197. ".section __ex_table,\"a\" \n" \
  198. " .balign 8 \n" \
  199. " .long 1b,3b \n" \
  200. ".previous" \
  201. : "=r" (err), constraint (x) \
  202. : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
  203. ); \
  204. } while(0)
  205. #else
  206. #define __get_user_asm(err,x,ptr,bwl,con) \
  207. asm(" ld"bwl"%I1 %M1,%0 \n" \
  208. " membar \n" \
  209. : con(x) \
  210. : "m" (*__ptr(ptr)))
  211. #endif
  212. /*****************************************************************************/
  213. /*
  214. *
  215. */
  216. #ifdef CONFIG_MMU
  217. extern long __memset_user(void *dst, unsigned long count);
  218. extern long __memcpy_user(void *dst, const void *src, unsigned long count);
  219. #define clear_user(dst,count) __memset_user((dst), (count))
  220. #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), (from), (n))
  221. #define __copy_to_user_inatomic(to, from, n) __memcpy_user((to), (from), (n))
  222. #else
  223. #define clear_user(dst,count) (memset((dst), 0, (count)), 0)
  224. #define __copy_from_user_inatomic(to, from, n) (memcpy((to), (from), (n)), 0)
  225. #define __copy_to_user_inatomic(to, from, n) (memcpy((to), (from), (n)), 0)
  226. #endif
  227. static inline unsigned long __must_check
  228. __copy_to_user(void __user *to, const void *from, unsigned long n)
  229. {
  230. might_sleep();
  231. return __copy_to_user_inatomic(to, from, n);
  232. }
  233. static inline unsigned long
  234. __copy_from_user(void *to, const void __user *from, unsigned long n)
  235. {
  236. might_sleep();
  237. return __copy_from_user_inatomic(to, from, n);
  238. }
  239. static inline long copy_from_user(void *to, const void *from, unsigned long n)
  240. {
  241. unsigned long ret = n;
  242. if (likely(__access_ok(from, n)))
  243. ret = __copy_from_user(to, from, n);
  244. if (unlikely(ret != 0))
  245. memset(to + (n - ret), 0, ret);
  246. return ret;
  247. }
  248. static inline long copy_to_user(void *to, const void *from, unsigned long n)
  249. {
  250. return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
  251. }
  252. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  253. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  254. extern long strncpy_from_user(char *dst, const char *src, long count);
  255. extern long strnlen_user(const char *src, long count);
  256. #define strlen_user(str) strnlen_user(str, 32767)
  257. extern unsigned long search_exception_table(unsigned long addr);
  258. #endif /* _ASM_UACCESS_H */