uaccess.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #ifndef _PPC64_UACCESS_H
  2. #define _PPC64_UACCESS_H
  3. /*
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #ifndef __ASSEMBLY__
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <asm/processor.h>
  13. #define VERIFY_READ 0
  14. #define VERIFY_WRITE 1
  15. /*
  16. * The fs value determines whether argument validity checking should be
  17. * performed or not. If get_fs() == USER_DS, checking is performed, with
  18. * get_fs() == KERNEL_DS, checking is bypassed.
  19. *
  20. * For historical reasons, these macros are grossly misnamed.
  21. */
  22. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  23. #define KERNEL_DS MAKE_MM_SEG(0UL)
  24. #define USER_DS MAKE_MM_SEG(0xf000000000000000UL)
  25. #define get_ds() (KERNEL_DS)
  26. #define get_fs() (current->thread.fs)
  27. #define set_fs(val) (current->thread.fs = (val))
  28. #define segment_eq(a,b) ((a).seg == (b).seg)
  29. /*
  30. * Use the alpha trick for checking ranges:
  31. *
  32. * Is a address valid? This does a straightforward calculation rather
  33. * than tests.
  34. *
  35. * Address valid if:
  36. * - "addr" doesn't have any high-bits set
  37. * - AND "size" doesn't have any high-bits set
  38. * - OR we are in kernel mode.
  39. *
  40. * We dont have to check for high bits in (addr+size) because the first
  41. * two checks force the maximum result to be below the start of the
  42. * kernel region.
  43. */
  44. #define __access_ok(addr,size,segment) \
  45. (((segment).seg & (addr | size )) == 0)
  46. #define access_ok(type,addr,size) \
  47. __access_ok(((__force unsigned long)(addr)),(size),get_fs())
  48. /* this function will go away soon - use access_ok() instead */
  49. static inline int __deprecated verify_area(int type, const void __user *addr, unsigned long size)
  50. {
  51. return access_ok(type,addr,size) ? 0 : -EFAULT;
  52. }
  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. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  76. * and yet we don't want to do any pointers, because that is too much
  77. * of a performance impact. Thus we have a few rather ugly macros here,
  78. * and hide all the ugliness from the user.
  79. *
  80. * The "__xxx" versions of the user access functions are versions that
  81. * do not verify the address space, that must have been done previously
  82. * with a separate "access_ok()" call (this is used when we do multiple
  83. * accesses to the same area of user memory).
  84. *
  85. * As we use the same address space for kernel and user data on the
  86. * PowerPC, we can just do these as direct assignments. (Of course, the
  87. * exception handling means that it's no longer "just"...)
  88. */
  89. #define get_user(x,ptr) \
  90. __get_user_check((x),(ptr),sizeof(*(ptr)))
  91. #define put_user(x,ptr) \
  92. __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  93. #define __get_user(x,ptr) \
  94. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  95. #define __put_user(x,ptr) \
  96. __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  97. #define __get_user_unaligned __get_user
  98. #define __put_user_unaligned __put_user
  99. extern long __put_user_bad(void);
  100. #define __put_user_nocheck(x,ptr,size) \
  101. ({ \
  102. long __pu_err; \
  103. might_sleep(); \
  104. __chk_user_ptr(ptr); \
  105. __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
  106. __pu_err; \
  107. })
  108. #define __put_user_check(x,ptr,size) \
  109. ({ \
  110. long __pu_err = -EFAULT; \
  111. void __user *__pu_addr = (ptr); \
  112. might_sleep(); \
  113. if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
  114. __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
  115. __pu_err; \
  116. })
  117. #define __put_user_size(x,ptr,size,retval,errret) \
  118. do { \
  119. retval = 0; \
  120. switch (size) { \
  121. case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \
  122. case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \
  123. case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \
  124. case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \
  125. default: __put_user_bad(); \
  126. } \
  127. } while (0)
  128. /*
  129. * We don't tell gcc that we are accessing memory, but this is OK
  130. * because we do not write to any memory gcc knows about, so there
  131. * are no aliasing issues.
  132. */
  133. #define __put_user_asm(x, addr, err, op, errret) \
  134. __asm__ __volatile__( \
  135. "1: "op" %1,0(%2) # put_user\n" \
  136. "2:\n" \
  137. ".section .fixup,\"ax\"\n" \
  138. "3: li %0,%3\n" \
  139. " b 2b\n" \
  140. ".previous\n" \
  141. ".section __ex_table,\"a\"\n" \
  142. " .align 3\n" \
  143. " .llong 1b,3b\n" \
  144. ".previous" \
  145. : "=r"(err) \
  146. : "r"(x), "b"(addr), "i"(errret), "0"(err))
  147. #define __get_user_nocheck(x,ptr,size) \
  148. ({ \
  149. long __gu_err, __gu_val; \
  150. might_sleep(); \
  151. __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
  152. (x) = (__typeof__(*(ptr)))__gu_val; \
  153. __gu_err; \
  154. })
  155. #define __get_user_check(x,ptr,size) \
  156. ({ \
  157. long __gu_err = -EFAULT, __gu_val = 0; \
  158. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  159. might_sleep(); \
  160. if (access_ok(VERIFY_READ,__gu_addr,size)) \
  161. __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
  162. (x) = (__typeof__(*(ptr)))__gu_val; \
  163. __gu_err; \
  164. })
  165. extern long __get_user_bad(void);
  166. #define __get_user_size(x,ptr,size,retval,errret) \
  167. do { \
  168. retval = 0; \
  169. __chk_user_ptr(ptr); \
  170. switch (size) { \
  171. case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \
  172. case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \
  173. case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \
  174. case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \
  175. default: (x) = __get_user_bad(); \
  176. } \
  177. } while (0)
  178. #define __get_user_asm(x, addr, err, op, errret) \
  179. __asm__ __volatile__( \
  180. "1: "op" %1,0(%2) # get_user\n" \
  181. "2:\n" \
  182. ".section .fixup,\"ax\"\n" \
  183. "3: li %0,%3\n" \
  184. " li %1,0\n" \
  185. " b 2b\n" \
  186. ".previous\n" \
  187. ".section __ex_table,\"a\"\n" \
  188. " .align 3\n" \
  189. " .llong 1b,3b\n" \
  190. ".previous" \
  191. : "=r"(err), "=r"(x) \
  192. : "b"(addr), "i"(errret), "0"(err))
  193. /* more complex routines */
  194. extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
  195. unsigned long size);
  196. static inline unsigned long
  197. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  198. {
  199. if (__builtin_constant_p(n)) {
  200. unsigned long ret;
  201. switch (n) {
  202. case 1:
  203. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  204. return ret;
  205. case 2:
  206. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  207. return ret;
  208. case 4:
  209. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  210. return ret;
  211. case 8:
  212. __get_user_size(*(u64 *)to, from, 8, ret, 8);
  213. return ret;
  214. }
  215. }
  216. return __copy_tofrom_user((__force void __user *) to, from, n);
  217. }
  218. static inline unsigned long
  219. __copy_from_user(void *to, const void __user *from, unsigned long n)
  220. {
  221. might_sleep();
  222. return __copy_from_user_inatomic(to, from, n);
  223. }
  224. static inline unsigned long
  225. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  226. {
  227. if (__builtin_constant_p(n)) {
  228. unsigned long ret;
  229. switch (n) {
  230. case 1:
  231. __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
  232. return ret;
  233. case 2:
  234. __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
  235. return ret;
  236. case 4:
  237. __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
  238. return ret;
  239. case 8:
  240. __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
  241. return ret;
  242. }
  243. }
  244. return __copy_tofrom_user(to, (__force const void __user *) from, n);
  245. }
  246. static inline unsigned long
  247. __copy_to_user(void __user *to, const void *from, unsigned long n)
  248. {
  249. might_sleep();
  250. return __copy_to_user_inatomic(to, from, n);
  251. }
  252. #define __copy_in_user(to, from, size) \
  253. __copy_tofrom_user((to), (from), (size))
  254. extern unsigned long copy_from_user(void *to, const void __user *from,
  255. unsigned long n);
  256. extern unsigned long copy_to_user(void __user *to, const void *from,
  257. unsigned long n);
  258. extern unsigned long copy_in_user(void __user *to, const void __user *from,
  259. unsigned long n);
  260. extern unsigned long __clear_user(void __user *addr, unsigned long size);
  261. static inline unsigned long
  262. clear_user(void __user *addr, unsigned long size)
  263. {
  264. might_sleep();
  265. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  266. size = __clear_user(addr, size);
  267. return size;
  268. }
  269. extern int __strncpy_from_user(char *dst, const char __user *src, long count);
  270. static inline long
  271. strncpy_from_user(char *dst, const char __user *src, long count)
  272. {
  273. might_sleep();
  274. if (likely(access_ok(VERIFY_READ, src, 1)))
  275. return __strncpy_from_user(dst, src, count);
  276. return -EFAULT;
  277. }
  278. /*
  279. * Return the size of a string (including the ending 0)
  280. *
  281. * Return 0 for error
  282. */
  283. extern int __strnlen_user(const char __user *str, long len);
  284. /*
  285. * Returns the length of the string at str (including the null byte),
  286. * or 0 if we hit a page we can't access,
  287. * or something > len if we didn't find a null byte.
  288. */
  289. static inline int strnlen_user(const char __user *str, long len)
  290. {
  291. might_sleep();
  292. if (likely(access_ok(VERIFY_READ, str, 1)))
  293. return __strnlen_user(str, len);
  294. return 0;
  295. }
  296. #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
  297. #endif /* __ASSEMBLY__ */
  298. #endif /* _PPC64_UACCESS_H */