uaccess.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 __force *)(x))
  22. #define VERIFY_READ 0
  23. #define VERIFY_WRITE 1
  24. /*
  25. * check that a range of addresses falls within the current address limit
  26. */
  27. static inline int ___range_ok(unsigned long addr, unsigned long size)
  28. {
  29. #ifdef CONFIG_MMU
  30. int flag = -EFAULT, tmp;
  31. asm volatile (
  32. " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
  33. " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
  34. " bc icc0,#0,0f \n"
  35. " bhi icc1,#0,0f \n"
  36. " setlos #0,%0 \n" /* mark okay */
  37. "0: \n"
  38. : "=r"(flag), "=&r"(tmp)
  39. : "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
  40. );
  41. return flag;
  42. #else
  43. if (addr < memory_start ||
  44. addr > memory_end ||
  45. size > memory_end - memory_start ||
  46. addr + size > memory_end)
  47. return -EFAULT;
  48. return 0;
  49. #endif
  50. }
  51. #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
  52. #define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
  53. #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
  54. /*
  55. * The exception table consists of pairs of addresses: the first is the
  56. * address of an instruction that is allowed to fault, and the second is
  57. * the address at which the program should continue. No registers are
  58. * modified, so it is entirely up to the continuation code to figure out
  59. * what to do.
  60. *
  61. * All the routines below use bits of fixup code that are out of line
  62. * with the main instruction path. This means when everything is well,
  63. * we don't even have to jump over them. Further, they do not intrude
  64. * on our cache or tlb entries.
  65. */
  66. struct exception_table_entry
  67. {
  68. unsigned long insn, fixup;
  69. };
  70. /* Returns 0 if exception not found and fixup otherwise. */
  71. extern unsigned long search_exception_table(unsigned long);
  72. /*
  73. * These are the main single-value transfer routines. They automatically
  74. * use the right size if we just have the right pointer type.
  75. */
  76. #define __put_user(x, ptr) \
  77. ({ \
  78. int __pu_err = 0; \
  79. \
  80. typeof(*(ptr)) __pu_val = (x); \
  81. __chk_user_ptr(ptr); \
  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)) __user *_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. int __gu_err = 0; \
  152. __chk_user_ptr(ptr); \
  153. \
  154. switch (sizeof(*(ptr))) { \
  155. case 1: { \
  156. unsigned char __gu_val; \
  157. __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
  158. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  159. break; \
  160. } \
  161. case 2: { \
  162. unsigned short __gu_val; \
  163. __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
  164. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  165. break; \
  166. } \
  167. case 4: { \
  168. unsigned int __gu_val; \
  169. __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
  170. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  171. break; \
  172. } \
  173. case 8: { \
  174. unsigned long long __gu_val; \
  175. __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
  176. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  177. break; \
  178. } \
  179. default: \
  180. __gu_err = __get_user_bad(); \
  181. break; \
  182. } \
  183. __gu_err; \
  184. })
  185. #define get_user(x, ptr) \
  186. ({ \
  187. const typeof(*(ptr)) __user *_p = (ptr);\
  188. int _e; \
  189. \
  190. _e = __range_ok(_p, sizeof(*_p)); \
  191. if (likely(_e == 0)) \
  192. _e = __get_user((x), _p); \
  193. else \
  194. (x) = (typeof(x)) 0; \
  195. _e; \
  196. })
  197. extern int __get_user_bad(void);
  198. #ifdef CONFIG_MMU
  199. #define __get_user_asm(err,x,ptr,dtype,constraint) \
  200. do { \
  201. asm("1: ld"dtype"%I2 %M2,%1 \n" \
  202. "2: \n" \
  203. ".subsection 2 \n" \
  204. "3: setlos %3,%0 \n" \
  205. " setlos #0,%1 \n" \
  206. " bra 2b \n" \
  207. ".previous \n" \
  208. ".section __ex_table,\"a\" \n" \
  209. " .balign 8 \n" \
  210. " .long 1b,3b \n" \
  211. ".previous" \
  212. : "=r" (err), constraint (x) \
  213. : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
  214. ); \
  215. } while(0)
  216. #else
  217. #define __get_user_asm(err,x,ptr,bwl,con) \
  218. asm(" ld"bwl"%I1 %M1,%0 \n" \
  219. " membar \n" \
  220. : con(x) \
  221. : "m" (*__ptr(ptr)))
  222. #endif
  223. /*****************************************************************************/
  224. /*
  225. *
  226. */
  227. #define ____force(x) (__force void *)(void __user *)(x)
  228. #ifdef CONFIG_MMU
  229. extern long __memset_user(void *dst, unsigned long count);
  230. extern long __memcpy_user(void *dst, const void *src, unsigned long count);
  231. #define __clear_user(dst,count) __memset_user(____force(dst), (count))
  232. #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), ____force(from), (n))
  233. #define __copy_to_user_inatomic(to, from, n) __memcpy_user(____force(to), (from), (n))
  234. #else
  235. #define __clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
  236. #define __copy_from_user_inatomic(to, from, n) (memcpy((to), ____force(from), (n)), 0)
  237. #define __copy_to_user_inatomic(to, from, n) (memcpy(____force(to), (from), (n)), 0)
  238. #endif
  239. static inline unsigned long __must_check
  240. clear_user(void __user *to, unsigned long n)
  241. {
  242. if (likely(__access_ok(to, n)))
  243. n = __clear_user(to, n);
  244. return n;
  245. }
  246. static inline unsigned long __must_check
  247. __copy_to_user(void __user *to, const void *from, unsigned long n)
  248. {
  249. might_fault();
  250. return __copy_to_user_inatomic(to, from, n);
  251. }
  252. static inline unsigned long
  253. __copy_from_user(void *to, const void __user *from, unsigned long n)
  254. {
  255. might_fault();
  256. return __copy_from_user_inatomic(to, from, n);
  257. }
  258. static inline long copy_from_user(void *to, const void __user *from, unsigned long n)
  259. {
  260. unsigned long ret = n;
  261. if (likely(__access_ok(from, n)))
  262. ret = __copy_from_user(to, from, n);
  263. if (unlikely(ret != 0))
  264. memset(to + (n - ret), 0, ret);
  265. return ret;
  266. }
  267. static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
  268. {
  269. return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
  270. }
  271. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  272. extern long strnlen_user(const char __user *src, long count);
  273. #define strlen_user(str) strnlen_user(str, 32767)
  274. extern unsigned long search_exception_table(unsigned long addr);
  275. #endif /* _ASM_UACCESS_H */