uaccess.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #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((void __user *)(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. __chk_user_ptr(ptr); \
  83. \
  84. switch (sizeof (*(ptr))) { \
  85. case 1: \
  86. __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
  87. break; \
  88. case 2: \
  89. __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
  90. break; \
  91. case 4: \
  92. __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
  93. break; \
  94. case 8: \
  95. __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
  96. break; \
  97. default: \
  98. __pu_err = __put_user_bad(); \
  99. break; \
  100. } \
  101. __pu_err; \
  102. })
  103. #define put_user(x, ptr) \
  104. ({ \
  105. typeof(*(ptr)) __user *_p = (ptr); \
  106. int _e; \
  107. \
  108. _e = __range_ok(_p, sizeof(*_p)); \
  109. if (_e == 0) \
  110. _e = __put_user((x), _p); \
  111. _e; \
  112. })
  113. extern int __put_user_bad(void);
  114. /*
  115. * Tell gcc we read from memory instead of writing: this is because
  116. * we do not write to any memory gcc knows about, so there are no
  117. * aliasing issues.
  118. */
  119. #ifdef CONFIG_MMU
  120. #define __put_user_asm(err,x,ptr,dsize,constraint) \
  121. do { \
  122. asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
  123. "2: \n" \
  124. ".subsection 2 \n" \
  125. "3: setlos %3,%0 \n" \
  126. " bra 2b \n" \
  127. ".previous \n" \
  128. ".section __ex_table,\"a\" \n" \
  129. " .balign 8 \n" \
  130. " .long 1b,3b \n" \
  131. ".previous" \
  132. : "=r" (err) \
  133. : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
  134. : "memory"); \
  135. } while (0)
  136. #else
  137. #define __put_user_asm(err,x,ptr,bwl,con) \
  138. do { \
  139. asm(" st"bwl"%I0 %1,%M0 \n" \
  140. " membar \n" \
  141. : \
  142. : "m" (*__ptr(ptr)), con (x) \
  143. : "memory"); \
  144. } while (0)
  145. #endif
  146. /*****************************************************************************/
  147. /*
  148. *
  149. */
  150. #define __get_user(x, ptr) \
  151. ({ \
  152. int __gu_err = 0; \
  153. __chk_user_ptr(ptr); \
  154. \
  155. switch (sizeof(*(ptr))) { \
  156. case 1: { \
  157. unsigned char __gu_val; \
  158. __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
  159. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  160. break; \
  161. } \
  162. case 2: { \
  163. unsigned short __gu_val; \
  164. __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
  165. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  166. break; \
  167. } \
  168. case 4: { \
  169. unsigned int __gu_val; \
  170. __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
  171. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  172. break; \
  173. } \
  174. case 8: { \
  175. unsigned long long __gu_val; \
  176. __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
  177. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  178. break; \
  179. } \
  180. default: \
  181. __gu_err = __get_user_bad(); \
  182. break; \
  183. } \
  184. __gu_err; \
  185. })
  186. #define get_user(x, ptr) \
  187. ({ \
  188. const typeof(*(ptr)) __user *_p = (ptr);\
  189. int _e; \
  190. \
  191. _e = __range_ok(_p, sizeof(*_p)); \
  192. if (likely(_e == 0)) \
  193. _e = __get_user((x), _p); \
  194. else \
  195. (x) = (typeof(x)) 0; \
  196. _e; \
  197. })
  198. extern int __get_user_bad(void);
  199. #ifdef CONFIG_MMU
  200. #define __get_user_asm(err,x,ptr,dtype,constraint) \
  201. do { \
  202. asm("1: ld"dtype"%I2 %M2,%1 \n" \
  203. "2: \n" \
  204. ".subsection 2 \n" \
  205. "3: setlos %3,%0 \n" \
  206. " setlos #0,%1 \n" \
  207. " bra 2b \n" \
  208. ".previous \n" \
  209. ".section __ex_table,\"a\" \n" \
  210. " .balign 8 \n" \
  211. " .long 1b,3b \n" \
  212. ".previous" \
  213. : "=r" (err), constraint (x) \
  214. : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
  215. ); \
  216. } while(0)
  217. #else
  218. #define __get_user_asm(err,x,ptr,bwl,con) \
  219. asm(" ld"bwl"%I1 %M1,%0 \n" \
  220. " membar \n" \
  221. : con(x) \
  222. : "m" (*__ptr(ptr)))
  223. #endif
  224. /*****************************************************************************/
  225. /*
  226. *
  227. */
  228. #define ____force(x) (__force void *)(void __user *)(x)
  229. #ifdef CONFIG_MMU
  230. extern long __memset_user(void *dst, unsigned long count);
  231. extern long __memcpy_user(void *dst, const void *src, unsigned long count);
  232. #define clear_user(dst,count) __memset_user(____force(dst), (count))
  233. #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), ____force(from), (n))
  234. #define __copy_to_user_inatomic(to, from, n) __memcpy_user(____force(to), (from), (n))
  235. #else
  236. #define clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
  237. #define __copy_from_user_inatomic(to, from, n) (memcpy((to), ____force(from), (n)), 0)
  238. #define __copy_to_user_inatomic(to, from, n) (memcpy(____force(to), (from), (n)), 0)
  239. #endif
  240. static inline unsigned long __must_check
  241. __copy_to_user(void __user *to, const void *from, unsigned long n)
  242. {
  243. might_sleep();
  244. return __copy_to_user_inatomic(to, from, n);
  245. }
  246. static inline unsigned long
  247. __copy_from_user(void *to, const void __user *from, unsigned long n)
  248. {
  249. might_sleep();
  250. return __copy_from_user_inatomic(to, from, n);
  251. }
  252. static inline long copy_from_user(void *to, const void __user *from, unsigned long n)
  253. {
  254. unsigned long ret = n;
  255. if (likely(__access_ok(from, n)))
  256. ret = __copy_from_user(to, from, n);
  257. if (unlikely(ret != 0))
  258. memset(to + (n - ret), 0, ret);
  259. return ret;
  260. }
  261. static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
  262. {
  263. return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
  264. }
  265. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  266. extern long strnlen_user(const char __user *src, long count);
  267. #define strlen_user(str) strnlen_user(str, 32767)
  268. extern unsigned long search_exception_table(unsigned long addr);
  269. #endif /* _ASM_UACCESS_H */