uaccess.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /*
  49. * The exception table consists of pairs of addresses: the first is the
  50. * address of an instruction that is allowed to fault, and the second is
  51. * the address at which the program should continue. No registers are
  52. * modified, so it is entirely up to the continuation code to figure out
  53. * what to do.
  54. *
  55. * All the routines below use bits of fixup code that are out of line
  56. * with the main instruction path. This means when everything is well,
  57. * we don't even have to jump over them. Further, they do not intrude
  58. * on our cache or tlb entries.
  59. */
  60. struct exception_table_entry
  61. {
  62. unsigned long insn, fixup;
  63. };
  64. /* Returns 0 if exception not found and fixup otherwise. */
  65. extern unsigned long search_exception_table(unsigned long);
  66. /*
  67. * These are the main single-value transfer routines. They automatically
  68. * use the right size if we just have the right pointer type.
  69. *
  70. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  71. * and yet we don't want to do any pointers, because that is too much
  72. * of a performance impact. Thus we have a few rather ugly macros here,
  73. * and hide all the ugliness from the user.
  74. *
  75. * The "__xxx" versions of the user access functions are versions that
  76. * do not verify the address space, that must have been done previously
  77. * with a separate "access_ok()" call (this is used when we do multiple
  78. * accesses to the same area of user memory).
  79. *
  80. * As we use the same address space for kernel and user data on the
  81. * PowerPC, we can just do these as direct assignments. (Of course, the
  82. * exception handling means that it's no longer "just"...)
  83. */
  84. #define get_user(x,ptr) \
  85. __get_user_check((x),(ptr),sizeof(*(ptr)))
  86. #define put_user(x,ptr) \
  87. __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  88. #define __get_user(x,ptr) \
  89. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  90. #define __put_user(x,ptr) \
  91. __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  92. #define __get_user_unaligned __get_user
  93. #define __put_user_unaligned __put_user
  94. extern long __put_user_bad(void);
  95. #define __put_user_nocheck(x,ptr,size) \
  96. ({ \
  97. long __pu_err; \
  98. might_sleep(); \
  99. __chk_user_ptr(ptr); \
  100. __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
  101. __pu_err; \
  102. })
  103. #define __put_user_check(x,ptr,size) \
  104. ({ \
  105. long __pu_err = -EFAULT; \
  106. void __user *__pu_addr = (ptr); \
  107. might_sleep(); \
  108. if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
  109. __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
  110. __pu_err; \
  111. })
  112. #define __put_user_size(x,ptr,size,retval,errret) \
  113. do { \
  114. retval = 0; \
  115. switch (size) { \
  116. case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \
  117. case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \
  118. case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \
  119. case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \
  120. default: __put_user_bad(); \
  121. } \
  122. } while (0)
  123. /*
  124. * We don't tell gcc that we are accessing memory, but this is OK
  125. * because we do not write to any memory gcc knows about, so there
  126. * are no aliasing issues.
  127. */
  128. #define __put_user_asm(x, addr, err, op, errret) \
  129. __asm__ __volatile__( \
  130. "1: "op" %1,0(%2) # put_user\n" \
  131. "2:\n" \
  132. ".section .fixup,\"ax\"\n" \
  133. "3: li %0,%3\n" \
  134. " b 2b\n" \
  135. ".previous\n" \
  136. ".section __ex_table,\"a\"\n" \
  137. " .align 3\n" \
  138. " .llong 1b,3b\n" \
  139. ".previous" \
  140. : "=r"(err) \
  141. : "r"(x), "b"(addr), "i"(errret), "0"(err))
  142. #define __get_user_nocheck(x,ptr,size) \
  143. ({ \
  144. long __gu_err, __gu_val; \
  145. might_sleep(); \
  146. __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
  147. (x) = (__typeof__(*(ptr)))__gu_val; \
  148. __gu_err; \
  149. })
  150. #define __get_user_check(x,ptr,size) \
  151. ({ \
  152. long __gu_err = -EFAULT, __gu_val = 0; \
  153. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  154. might_sleep(); \
  155. if (access_ok(VERIFY_READ,__gu_addr,size)) \
  156. __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
  157. (x) = (__typeof__(*(ptr)))__gu_val; \
  158. __gu_err; \
  159. })
  160. extern long __get_user_bad(void);
  161. #define __get_user_size(x,ptr,size,retval,errret) \
  162. do { \
  163. retval = 0; \
  164. __chk_user_ptr(ptr); \
  165. switch (size) { \
  166. case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \
  167. case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \
  168. case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \
  169. case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \
  170. default: (x) = __get_user_bad(); \
  171. } \
  172. } while (0)
  173. #define __get_user_asm(x, addr, err, op, errret) \
  174. __asm__ __volatile__( \
  175. "1: "op" %1,0(%2) # get_user\n" \
  176. "2:\n" \
  177. ".section .fixup,\"ax\"\n" \
  178. "3: li %0,%3\n" \
  179. " li %1,0\n" \
  180. " b 2b\n" \
  181. ".previous\n" \
  182. ".section __ex_table,\"a\"\n" \
  183. " .align 3\n" \
  184. " .llong 1b,3b\n" \
  185. ".previous" \
  186. : "=r"(err), "=r"(x) \
  187. : "b"(addr), "i"(errret), "0"(err))
  188. /* more complex routines */
  189. extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
  190. unsigned long size);
  191. static inline unsigned long
  192. __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
  193. {
  194. if (__builtin_constant_p(n)) {
  195. unsigned long ret;
  196. switch (n) {
  197. case 1:
  198. __get_user_size(*(u8 *)to, from, 1, ret, 1);
  199. return ret;
  200. case 2:
  201. __get_user_size(*(u16 *)to, from, 2, ret, 2);
  202. return ret;
  203. case 4:
  204. __get_user_size(*(u32 *)to, from, 4, ret, 4);
  205. return ret;
  206. case 8:
  207. __get_user_size(*(u64 *)to, from, 8, ret, 8);
  208. return ret;
  209. }
  210. }
  211. return __copy_tofrom_user((__force void __user *) to, from, n);
  212. }
  213. static inline unsigned long
  214. __copy_from_user(void *to, const void __user *from, unsigned long n)
  215. {
  216. might_sleep();
  217. return __copy_from_user_inatomic(to, from, n);
  218. }
  219. static inline unsigned long
  220. __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
  221. {
  222. if (__builtin_constant_p(n)) {
  223. unsigned long ret;
  224. switch (n) {
  225. case 1:
  226. __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
  227. return ret;
  228. case 2:
  229. __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
  230. return ret;
  231. case 4:
  232. __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
  233. return ret;
  234. case 8:
  235. __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
  236. return ret;
  237. }
  238. }
  239. return __copy_tofrom_user(to, (__force const void __user *) from, n);
  240. }
  241. static inline unsigned long
  242. __copy_to_user(void __user *to, const void *from, unsigned long n)
  243. {
  244. might_sleep();
  245. return __copy_to_user_inatomic(to, from, n);
  246. }
  247. #define __copy_in_user(to, from, size) \
  248. __copy_tofrom_user((to), (from), (size))
  249. extern unsigned long copy_from_user(void *to, const void __user *from,
  250. unsigned long n);
  251. extern unsigned long copy_to_user(void __user *to, const void *from,
  252. unsigned long n);
  253. extern unsigned long copy_in_user(void __user *to, const void __user *from,
  254. unsigned long n);
  255. extern unsigned long __clear_user(void __user *addr, unsigned long size);
  256. static inline unsigned long
  257. clear_user(void __user *addr, unsigned long size)
  258. {
  259. might_sleep();
  260. if (likely(access_ok(VERIFY_WRITE, addr, size)))
  261. size = __clear_user(addr, size);
  262. return size;
  263. }
  264. extern int __strncpy_from_user(char *dst, const char __user *src, long count);
  265. static inline long
  266. strncpy_from_user(char *dst, const char __user *src, long count)
  267. {
  268. might_sleep();
  269. if (likely(access_ok(VERIFY_READ, src, 1)))
  270. return __strncpy_from_user(dst, src, count);
  271. return -EFAULT;
  272. }
  273. /*
  274. * Return the size of a string (including the ending 0)
  275. *
  276. * Return 0 for error
  277. */
  278. extern int __strnlen_user(const char __user *str, long len);
  279. /*
  280. * Returns the length of the string at str (including the null byte),
  281. * or 0 if we hit a page we can't access,
  282. * or something > len if we didn't find a null byte.
  283. */
  284. static inline int strnlen_user(const char __user *str, long len)
  285. {
  286. might_sleep();
  287. if (likely(access_ok(VERIFY_READ, str, 1)))
  288. return __strnlen_user(str, len);
  289. return 0;
  290. }
  291. #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
  292. #endif /* __ASSEMBLY__ */
  293. #endif /* _PPC64_UACCESS_H */