uaccess_32.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * uaccess.h: User space memore access functions.
  3. *
  4. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #ifndef _ASM_UACCESS_H
  8. #define _ASM_UACCESS_H
  9. #include <linux/compiler.h>
  10. #include <linux/string.h>
  11. #include <asm/processor.h>
  12. #define ARCH_HAS_SORT_EXTABLE
  13. #define ARCH_HAS_SEARCH_EXTABLE
  14. /* Sparc is not segmented, however we need to be able to fool access_ok()
  15. * when doing system calls from kernel mode legitimately.
  16. *
  17. * "For historical reasons, these macros are grossly misnamed." -Linus
  18. */
  19. #define KERNEL_DS ((mm_segment_t) { 0 })
  20. #define USER_DS ((mm_segment_t) { -1 })
  21. #define get_ds() (KERNEL_DS)
  22. #define get_fs() (current->thread.current_ds)
  23. #define set_fs(val) ((current->thread.current_ds) = (val))
  24. #define segment_eq(a, b) ((a).seg == (b).seg)
  25. /* We have there a nice not-mapped page at PAGE_OFFSET - PAGE_SIZE, so that this test
  26. * can be fairly lightweight.
  27. * No one can read/write anything from userland in the kernel space by setting
  28. * large size and address near to PAGE_OFFSET - a fault will break his intentions.
  29. */
  30. #define __user_ok(addr, size) ({ (void)(size); (addr) < STACK_TOP; })
  31. #define __kernel_ok (uaccess_kernel())
  32. #define __access_ok(addr, size) (__user_ok((addr) & get_fs().seg, (size)))
  33. #define access_ok(type, addr, size) \
  34. ({ (void)(type); __access_ok((unsigned long)(addr), size); })
  35. /*
  36. * The exception table consists of pairs of addresses: the first is the
  37. * address of an instruction that is allowed to fault, and the second is
  38. * the address at which the program should continue. No registers are
  39. * modified, so it is entirely up to the continuation code to figure out
  40. * what to do.
  41. *
  42. * All the routines below use bits of fixup code that are out of line
  43. * with the main instruction path. This means when everything is well,
  44. * we don't even have to jump over them. Further, they do not intrude
  45. * on our cache or tlb entries.
  46. *
  47. * There is a special way how to put a range of potentially faulting
  48. * insns (like twenty ldd/std's with now intervening other instructions)
  49. * You specify address of first in insn and 0 in fixup and in the next
  50. * exception_table_entry you specify last potentially faulting insn + 1
  51. * and in fixup the routine which should handle the fault.
  52. * That fixup code will get
  53. * (faulting_insn_address - first_insn_in_the_range_address)/4
  54. * in %g2 (ie. index of the faulting instruction in the range).
  55. */
  56. struct exception_table_entry
  57. {
  58. unsigned long insn, fixup;
  59. };
  60. /* Returns 0 if exception not found and fixup otherwise. */
  61. unsigned long search_extables_range(unsigned long addr, unsigned long *g2);
  62. void __ret_efault(void);
  63. /* Uh, these should become the main single-value transfer routines..
  64. * They automatically use the right size if we just have the right
  65. * pointer type..
  66. *
  67. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  68. * and yet we don't want to do any pointers, because that is too much
  69. * of a performance impact. Thus we have a few rather ugly macros here,
  70. * and hide all the ugliness from the user.
  71. */
  72. #define put_user(x, ptr) ({ \
  73. unsigned long __pu_addr = (unsigned long)(ptr); \
  74. __chk_user_ptr(ptr); \
  75. __put_user_check((__typeof__(*(ptr)))(x), __pu_addr, sizeof(*(ptr))); \
  76. })
  77. #define get_user(x, ptr) ({ \
  78. unsigned long __gu_addr = (unsigned long)(ptr); \
  79. __chk_user_ptr(ptr); \
  80. __get_user_check((x), __gu_addr, sizeof(*(ptr)), __typeof__(*(ptr))); \
  81. })
  82. /*
  83. * The "__xxx" versions do not do address space checking, useful when
  84. * doing multiple accesses to the same area (the user has to do the
  85. * checks by hand with "access_ok()")
  86. */
  87. #define __put_user(x, ptr) \
  88. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  89. #define __get_user(x, ptr) \
  90. __get_user_nocheck((x), (ptr), sizeof(*(ptr)), __typeof__(*(ptr)))
  91. struct __large_struct { unsigned long buf[100]; };
  92. #define __m(x) ((struct __large_struct __user *)(x))
  93. #define __put_user_check(x, addr, size) ({ \
  94. register int __pu_ret; \
  95. if (__access_ok(addr, size)) { \
  96. switch (size) { \
  97. case 1: \
  98. __put_user_asm(x, b, addr, __pu_ret); \
  99. break; \
  100. case 2: \
  101. __put_user_asm(x, h, addr, __pu_ret); \
  102. break; \
  103. case 4: \
  104. __put_user_asm(x, , addr, __pu_ret); \
  105. break; \
  106. case 8: \
  107. __put_user_asm(x, d, addr, __pu_ret); \
  108. break; \
  109. default: \
  110. __pu_ret = __put_user_bad(); \
  111. break; \
  112. } \
  113. } else { \
  114. __pu_ret = -EFAULT; \
  115. } \
  116. __pu_ret; \
  117. })
  118. #define __put_user_nocheck(x, addr, size) ({ \
  119. register int __pu_ret; \
  120. switch (size) { \
  121. case 1: __put_user_asm(x, b, addr, __pu_ret); break; \
  122. case 2: __put_user_asm(x, h, addr, __pu_ret); break; \
  123. case 4: __put_user_asm(x, , addr, __pu_ret); break; \
  124. case 8: __put_user_asm(x, d, addr, __pu_ret); break; \
  125. default: __pu_ret = __put_user_bad(); break; \
  126. } \
  127. __pu_ret; \
  128. })
  129. #define __put_user_asm(x, size, addr, ret) \
  130. __asm__ __volatile__( \
  131. "/* Put user asm, inline. */\n" \
  132. "1:\t" "st"#size " %1, %2\n\t" \
  133. "clr %0\n" \
  134. "2:\n\n\t" \
  135. ".section .fixup,#alloc,#execinstr\n\t" \
  136. ".align 4\n" \
  137. "3:\n\t" \
  138. "b 2b\n\t" \
  139. " mov %3, %0\n\t" \
  140. ".previous\n\n\t" \
  141. ".section __ex_table,#alloc\n\t" \
  142. ".align 4\n\t" \
  143. ".word 1b, 3b\n\t" \
  144. ".previous\n\n\t" \
  145. : "=&r" (ret) : "r" (x), "m" (*__m(addr)), \
  146. "i" (-EFAULT))
  147. int __put_user_bad(void);
  148. #define __get_user_check(x, addr, size, type) ({ \
  149. register int __gu_ret; \
  150. register unsigned long __gu_val; \
  151. if (__access_ok(addr, size)) { \
  152. switch (size) { \
  153. case 1: \
  154. __get_user_asm(__gu_val, ub, addr, __gu_ret); \
  155. break; \
  156. case 2: \
  157. __get_user_asm(__gu_val, uh, addr, __gu_ret); \
  158. break; \
  159. case 4: \
  160. __get_user_asm(__gu_val, , addr, __gu_ret); \
  161. break; \
  162. case 8: \
  163. __get_user_asm(__gu_val, d, addr, __gu_ret); \
  164. break; \
  165. default: \
  166. __gu_val = 0; \
  167. __gu_ret = __get_user_bad(); \
  168. break; \
  169. } \
  170. } else { \
  171. __gu_val = 0; \
  172. __gu_ret = -EFAULT; \
  173. } \
  174. x = (__force type) __gu_val; \
  175. __gu_ret; \
  176. })
  177. #define __get_user_nocheck(x, addr, size, type) ({ \
  178. register int __gu_ret; \
  179. register unsigned long __gu_val; \
  180. switch (size) { \
  181. case 1: __get_user_asm(__gu_val, ub, addr, __gu_ret); break; \
  182. case 2: __get_user_asm(__gu_val, uh, addr, __gu_ret); break; \
  183. case 4: __get_user_asm(__gu_val, , addr, __gu_ret); break; \
  184. case 8: __get_user_asm(__gu_val, d, addr, __gu_ret); break; \
  185. default: \
  186. __gu_val = 0; \
  187. __gu_ret = __get_user_bad(); \
  188. break; \
  189. } \
  190. x = (__force type) __gu_val; \
  191. __gu_ret; \
  192. })
  193. #define __get_user_asm(x, size, addr, ret) \
  194. __asm__ __volatile__( \
  195. "/* Get user asm, inline. */\n" \
  196. "1:\t" "ld"#size " %2, %1\n\t" \
  197. "clr %0\n" \
  198. "2:\n\n\t" \
  199. ".section .fixup,#alloc,#execinstr\n\t" \
  200. ".align 4\n" \
  201. "3:\n\t" \
  202. "clr %1\n\t" \
  203. "b 2b\n\t" \
  204. " mov %3, %0\n\n\t" \
  205. ".previous\n\t" \
  206. ".section __ex_table,#alloc\n\t" \
  207. ".align 4\n\t" \
  208. ".word 1b, 3b\n\n\t" \
  209. ".previous\n\t" \
  210. : "=&r" (ret), "=&r" (x) : "m" (*__m(addr)), \
  211. "i" (-EFAULT))
  212. int __get_user_bad(void);
  213. unsigned long __copy_user(void __user *to, const void __user *from, unsigned long size);
  214. static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
  215. {
  216. if (n && __access_ok((unsigned long) to, n)) {
  217. check_object_size(from, n, true);
  218. return __copy_user(to, (__force void __user *) from, n);
  219. } else
  220. return n;
  221. }
  222. static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
  223. {
  224. check_object_size(from, n, true);
  225. return __copy_user(to, (__force void __user *) from, n);
  226. }
  227. static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
  228. {
  229. if (n && __access_ok((unsigned long) from, n)) {
  230. check_object_size(to, n, false);
  231. return __copy_user((__force void __user *) to, from, n);
  232. } else {
  233. memset(to, 0, n);
  234. return n;
  235. }
  236. }
  237. static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
  238. {
  239. return __copy_user((__force void __user *) to, from, n);
  240. }
  241. #define __copy_to_user_inatomic __copy_to_user
  242. #define __copy_from_user_inatomic __copy_from_user
  243. static inline unsigned long __clear_user(void __user *addr, unsigned long size)
  244. {
  245. unsigned long ret;
  246. __asm__ __volatile__ (
  247. ".section __ex_table,#alloc\n\t"
  248. ".align 4\n\t"
  249. ".word 1f,3\n\t"
  250. ".previous\n\t"
  251. "mov %2, %%o1\n"
  252. "1:\n\t"
  253. "call __bzero\n\t"
  254. " mov %1, %%o0\n\t"
  255. "mov %%o0, %0\n"
  256. : "=r" (ret) : "r" (addr), "r" (size) :
  257. "o0", "o1", "o2", "o3", "o4", "o5", "o7",
  258. "g1", "g2", "g3", "g4", "g5", "g7", "cc");
  259. return ret;
  260. }
  261. static inline unsigned long clear_user(void __user *addr, unsigned long n)
  262. {
  263. if (n && __access_ok((unsigned long) addr, n))
  264. return __clear_user(addr, n);
  265. else
  266. return n;
  267. }
  268. __must_check long strlen_user(const char __user *str);
  269. __must_check long strnlen_user(const char __user *str, long n);
  270. #endif /* _ASM_UACCESS_H */