uaccess.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 __ptr(x) ((unsigned long __force *)(x))
  21. #define VERIFY_READ 0
  22. #define VERIFY_WRITE 1
  23. /*
  24. * check that a range of addresses falls within the current address limit
  25. */
  26. static inline int ___range_ok(unsigned long addr, unsigned long size)
  27. {
  28. #ifdef CONFIG_MMU
  29. int flag = -EFAULT, tmp;
  30. asm volatile (
  31. " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
  32. " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
  33. " bc icc0,#0,0f \n"
  34. " bhi icc1,#0,0f \n"
  35. " setlos #0,%0 \n" /* mark okay */
  36. "0: \n"
  37. : "=r"(flag), "=&r"(tmp)
  38. : "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
  39. );
  40. return flag;
  41. #else
  42. if (addr < memory_start ||
  43. addr > memory_end ||
  44. size > memory_end - memory_start ||
  45. addr + size > memory_end)
  46. return -EFAULT;
  47. return 0;
  48. #endif
  49. }
  50. #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
  51. #define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
  52. #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
  53. /*
  54. * The exception table consists of pairs of addresses: the first is the
  55. * address of an instruction that is allowed to fault, and the second is
  56. * the address at which the program should continue. No registers are
  57. * modified, so it is entirely up to the continuation code to figure out
  58. * what to do.
  59. *
  60. * All the routines below use bits of fixup code that are out of line
  61. * with the main instruction path. This means when everything is well,
  62. * we don't even have to jump over them. Further, they do not intrude
  63. * on our cache or tlb entries.
  64. */
  65. struct exception_table_entry
  66. {
  67. unsigned long insn, fixup;
  68. };
  69. /* Returns 0 if exception not found and fixup otherwise. */
  70. extern unsigned long search_exception_table(unsigned long);
  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, ptr) \
  76. ({ \
  77. int __pu_err = 0; \
  78. \
  79. typeof(*(ptr)) __pu_val = (x); \
  80. __chk_user_ptr(ptr); \
  81. \
  82. switch (sizeof (*(ptr))) { \
  83. case 1: \
  84. __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
  85. break; \
  86. case 2: \
  87. __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
  88. break; \
  89. case 4: \
  90. __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
  91. break; \
  92. case 8: \
  93. __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
  94. break; \
  95. default: \
  96. __pu_err = __put_user_bad(); \
  97. break; \
  98. } \
  99. __pu_err; \
  100. })
  101. #define put_user(x, ptr) \
  102. ({ \
  103. typeof(*(ptr)) __user *_p = (ptr); \
  104. int _e; \
  105. \
  106. _e = __range_ok(_p, sizeof(*_p)); \
  107. if (_e == 0) \
  108. _e = __put_user((x), _p); \
  109. _e; \
  110. })
  111. extern int __put_user_bad(void);
  112. /*
  113. * Tell gcc we read from memory instead of writing: this is because
  114. * we do not write to any memory gcc knows about, so there are no
  115. * aliasing issues.
  116. */
  117. #ifdef CONFIG_MMU
  118. #define __put_user_asm(err,x,ptr,dsize,constraint) \
  119. do { \
  120. asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
  121. "2: \n" \
  122. ".subsection 2 \n" \
  123. "3: setlos %3,%0 \n" \
  124. " bra 2b \n" \
  125. ".previous \n" \
  126. ".section __ex_table,\"a\" \n" \
  127. " .balign 8 \n" \
  128. " .long 1b,3b \n" \
  129. ".previous" \
  130. : "=r" (err) \
  131. : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
  132. : "memory"); \
  133. } while (0)
  134. #else
  135. #define __put_user_asm(err,x,ptr,bwl,con) \
  136. do { \
  137. asm(" st"bwl"%I0 %1,%M0 \n" \
  138. " membar \n" \
  139. : \
  140. : "m" (*__ptr(ptr)), con (x) \
  141. : "memory"); \
  142. } while (0)
  143. #endif
  144. /*****************************************************************************/
  145. /*
  146. *
  147. */
  148. #define __get_user(x, ptr) \
  149. ({ \
  150. int __gu_err = 0; \
  151. __chk_user_ptr(ptr); \
  152. \
  153. switch (sizeof(*(ptr))) { \
  154. case 1: { \
  155. unsigned char __gu_val; \
  156. __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
  157. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  158. break; \
  159. } \
  160. case 2: { \
  161. unsigned short __gu_val; \
  162. __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
  163. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  164. break; \
  165. } \
  166. case 4: { \
  167. unsigned int __gu_val; \
  168. __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
  169. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  170. break; \
  171. } \
  172. case 8: { \
  173. unsigned long long __gu_val; \
  174. __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
  175. (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
  176. break; \
  177. } \
  178. default: \
  179. __gu_err = __get_user_bad(); \
  180. break; \
  181. } \
  182. __gu_err; \
  183. })
  184. #define get_user(x, ptr) \
  185. ({ \
  186. const typeof(*(ptr)) __user *_p = (ptr);\
  187. int _e; \
  188. \
  189. _e = __range_ok(_p, sizeof(*_p)); \
  190. if (likely(_e == 0)) \
  191. _e = __get_user((x), _p); \
  192. else \
  193. (x) = (typeof(x)) 0; \
  194. _e; \
  195. })
  196. extern int __get_user_bad(void);
  197. #ifdef CONFIG_MMU
  198. #define __get_user_asm(err,x,ptr,dtype,constraint) \
  199. do { \
  200. asm("1: ld"dtype"%I2 %M2,%1 \n" \
  201. "2: \n" \
  202. ".subsection 2 \n" \
  203. "3: setlos %3,%0 \n" \
  204. " setlos #0,%1 \n" \
  205. " bra 2b \n" \
  206. ".previous \n" \
  207. ".section __ex_table,\"a\" \n" \
  208. " .balign 8 \n" \
  209. " .long 1b,3b \n" \
  210. ".previous" \
  211. : "=r" (err), constraint (x) \
  212. : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
  213. ); \
  214. } while(0)
  215. #else
  216. #define __get_user_asm(err,x,ptr,bwl,con) \
  217. asm(" ld"bwl"%I1 %M1,%0 \n" \
  218. " membar \n" \
  219. : con(x) \
  220. : "m" (*__ptr(ptr)))
  221. #endif
  222. /*****************************************************************************/
  223. /*
  224. *
  225. */
  226. #define ____force(x) (__force void *)(void __user *)(x)
  227. #ifdef CONFIG_MMU
  228. extern long __memset_user(void *dst, unsigned long count);
  229. extern long __memcpy_user(void *dst, const void *src, unsigned long count);
  230. #define __clear_user(dst,count) __memset_user(____force(dst), (count))
  231. #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), ____force(from), (n))
  232. #define __copy_to_user_inatomic(to, from, n) __memcpy_user(____force(to), (from), (n))
  233. #else
  234. #define __clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
  235. #define __copy_from_user_inatomic(to, from, n) (memcpy((to), ____force(from), (n)), 0)
  236. #define __copy_to_user_inatomic(to, from, n) (memcpy(____force(to), (from), (n)), 0)
  237. #endif
  238. static inline unsigned long __must_check
  239. clear_user(void __user *to, unsigned long n)
  240. {
  241. if (likely(__access_ok(to, n)))
  242. n = __clear_user(to, n);
  243. return n;
  244. }
  245. static inline unsigned long __must_check
  246. __copy_to_user(void __user *to, const void *from, unsigned long n)
  247. {
  248. might_fault();
  249. return __copy_to_user_inatomic(to, from, n);
  250. }
  251. static inline unsigned long
  252. __copy_from_user(void *to, const void __user *from, unsigned long n)
  253. {
  254. might_fault();
  255. return __copy_from_user_inatomic(to, from, n);
  256. }
  257. static inline long copy_from_user(void *to, const void __user *from, unsigned long n)
  258. {
  259. unsigned long ret = n;
  260. if (likely(__access_ok(from, n)))
  261. ret = __copy_from_user(to, from, n);
  262. if (unlikely(ret != 0))
  263. memset(to + (n - ret), 0, ret);
  264. return ret;
  265. }
  266. static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
  267. {
  268. return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
  269. }
  270. extern long strncpy_from_user(char *dst, const char __user *src, long count);
  271. extern long strnlen_user(const char __user *src, long count);
  272. #define strlen_user(str) strnlen_user(str, 32767)
  273. extern unsigned long search_exception_table(unsigned long addr);
  274. #endif /* _ASM_UACCESS_H */