uaccess_32.h 9.1 KB

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