uaccess.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef __ASM_SH_UACCESS_H
  2. #define __ASM_SH_UACCESS_H
  3. #include <linux/errno.h>
  4. #include <linux/sched.h>
  5. #include <asm/segment.h>
  6. #define VERIFY_READ 0
  7. #define VERIFY_WRITE 1
  8. #define __addr_ok(addr) \
  9. ((unsigned long __force)(addr) < current_thread_info()->addr_limit.seg)
  10. /*
  11. * __access_ok: Check if address with size is OK or not.
  12. *
  13. * Uhhuh, this needs 33-bit arithmetic. We have a carry..
  14. *
  15. * sum := addr + size; carry? --> flag = true;
  16. * if (sum >= addr_limit) flag = true;
  17. */
  18. #define __access_ok(addr, size) \
  19. (__addr_ok((addr) + (size)))
  20. #define access_ok(type, addr, size) \
  21. (__chk_user_ptr(addr), \
  22. __access_ok((unsigned long __force)(addr), (size)))
  23. /*
  24. * Uh, these should become the main single-value transfer routines ...
  25. * They automatically use the right size if we just have the right
  26. * pointer type ...
  27. *
  28. * As SuperH uses the same address space for kernel and user data, we
  29. * can just do these as direct assignments.
  30. *
  31. * Careful to not
  32. * (a) re-use the arguments for side effects (sizeof is ok)
  33. * (b) require any knowledge of processes at this stage
  34. */
  35. #define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  36. #define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  37. /*
  38. * The "__xxx" versions do not do address space checking, useful when
  39. * doing multiple accesses to the same area (the user has to do the
  40. * checks by hand with "access_ok()")
  41. */
  42. #define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  43. #define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  44. struct __large_struct { unsigned long buf[100]; };
  45. #define __m(x) (*(struct __large_struct __user *)(x))
  46. #define __get_user_nocheck(x,ptr,size) \
  47. ({ \
  48. long __gu_err; \
  49. unsigned long __gu_val; \
  50. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  51. __chk_user_ptr(ptr); \
  52. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  53. (x) = (__typeof__(*(ptr)))__gu_val; \
  54. __gu_err; \
  55. })
  56. #define __get_user_check(x,ptr,size) \
  57. ({ \
  58. long __gu_err = -EFAULT; \
  59. unsigned long __gu_val = 0; \
  60. const __typeof__(*(ptr)) *__gu_addr = (ptr); \
  61. if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
  62. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  63. (x) = (__typeof__(*(ptr)))__gu_val; \
  64. __gu_err; \
  65. })
  66. #define __put_user_nocheck(x,ptr,size) \
  67. ({ \
  68. long __pu_err; \
  69. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  70. __typeof__(*(ptr)) __pu_val = x; \
  71. __chk_user_ptr(ptr); \
  72. __put_user_size(__pu_val, __pu_addr, (size), __pu_err); \
  73. __pu_err; \
  74. })
  75. #define __put_user_check(x,ptr,size) \
  76. ({ \
  77. long __pu_err = -EFAULT; \
  78. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  79. __typeof__(*(ptr)) __pu_val = x; \
  80. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
  81. __put_user_size(__pu_val, __pu_addr, (size), \
  82. __pu_err); \
  83. __pu_err; \
  84. })
  85. #ifdef CONFIG_SUPERH32
  86. # include "uaccess_32.h"
  87. #else
  88. # include "uaccess_64.h"
  89. #endif
  90. /* Generic arbitrary sized copy. */
  91. /* Return the number of bytes NOT copied */
  92. __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
  93. static __always_inline unsigned long
  94. __copy_from_user(void *to, const void __user *from, unsigned long n)
  95. {
  96. return __copy_user(to, (__force void *)from, n);
  97. }
  98. static __always_inline unsigned long __must_check
  99. __copy_to_user(void __user *to, const void *from, unsigned long n)
  100. {
  101. return __copy_user((__force void *)to, from, n);
  102. }
  103. #define __copy_to_user_inatomic __copy_to_user
  104. #define __copy_from_user_inatomic __copy_from_user
  105. /*
  106. * Clear the area and return remaining number of bytes
  107. * (on failure. Usually it's 0.)
  108. */
  109. __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
  110. #define clear_user(addr,n) \
  111. ({ \
  112. void __user * __cl_addr = (addr); \
  113. unsigned long __cl_size = (n); \
  114. \
  115. if (__cl_size && access_ok(VERIFY_WRITE, \
  116. ((unsigned long)(__cl_addr)), __cl_size)) \
  117. __cl_size = __clear_user(__cl_addr, __cl_size); \
  118. \
  119. __cl_size; \
  120. })
  121. /**
  122. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  123. * @dst: Destination address, in kernel space. This buffer must be at
  124. * least @count bytes long.
  125. * @src: Source address, in user space.
  126. * @count: Maximum number of bytes to copy, including the trailing NUL.
  127. *
  128. * Copies a NUL-terminated string from userspace to kernel space.
  129. *
  130. * On success, returns the length of the string (not including the trailing
  131. * NUL).
  132. *
  133. * If access to userspace fails, returns -EFAULT (some data may have been
  134. * copied).
  135. *
  136. * If @count is smaller than the length of the string, copies @count bytes
  137. * and returns @count.
  138. */
  139. #define strncpy_from_user(dest,src,count) \
  140. ({ \
  141. unsigned long __sfu_src = (unsigned long)(src); \
  142. int __sfu_count = (int)(count); \
  143. long __sfu_res = -EFAULT; \
  144. \
  145. if (__access_ok(__sfu_src, __sfu_count)) \
  146. __sfu_res = __strncpy_from_user((unsigned long)(dest), \
  147. __sfu_src, __sfu_count); \
  148. \
  149. __sfu_res; \
  150. })
  151. static inline unsigned long
  152. copy_from_user(void *to, const void __user *from, unsigned long n)
  153. {
  154. unsigned long __copy_from = (unsigned long) from;
  155. __kernel_size_t __copy_size = (__kernel_size_t) n;
  156. if (__copy_size && __access_ok(__copy_from, __copy_size))
  157. return __copy_user(to, from, __copy_size);
  158. return __copy_size;
  159. }
  160. static inline unsigned long
  161. copy_to_user(void __user *to, const void *from, unsigned long n)
  162. {
  163. unsigned long __copy_to = (unsigned long) to;
  164. __kernel_size_t __copy_size = (__kernel_size_t) n;
  165. if (__copy_size && __access_ok(__copy_to, __copy_size))
  166. return __copy_user(to, from, __copy_size);
  167. return __copy_size;
  168. }
  169. /**
  170. * strnlen_user: - Get the size of a string in user space.
  171. * @s: The string to measure.
  172. * @n: The maximum valid length
  173. *
  174. * Context: User context only. This function may sleep.
  175. *
  176. * Get the size of a NUL-terminated string in user space.
  177. *
  178. * Returns the size of the string INCLUDING the terminating NUL.
  179. * On exception, returns 0.
  180. * If the string is too long, returns a value greater than @n.
  181. */
  182. static inline long strnlen_user(const char __user *s, long n)
  183. {
  184. if (!__addr_ok(s))
  185. return 0;
  186. else
  187. return __strnlen_user(s, n);
  188. }
  189. /**
  190. * strlen_user: - Get the size of a string in user space.
  191. * @str: The string to measure.
  192. *
  193. * Context: User context only. This function may sleep.
  194. *
  195. * Get the size of a NUL-terminated string in user space.
  196. *
  197. * Returns the size of the string INCLUDING the terminating NUL.
  198. * On exception, returns 0.
  199. *
  200. * If there is a limit on the length of a valid string, you may wish to
  201. * consider using strnlen_user() instead.
  202. */
  203. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  204. /*
  205. * The exception table consists of pairs of addresses: the first is the
  206. * address of an instruction that is allowed to fault, and the second is
  207. * the address at which the program should continue. No registers are
  208. * modified, so it is entirely up to the continuation code to figure out
  209. * what to do.
  210. *
  211. * All the routines below use bits of fixup code that are out of line
  212. * with the main instruction path. This means when everything is well,
  213. * we don't even have to jump over them. Further, they do not intrude
  214. * on our cache or tlb entries.
  215. */
  216. struct exception_table_entry {
  217. unsigned long insn, fixup;
  218. };
  219. #if defined(CONFIG_SUPERH64) && defined(CONFIG_MMU)
  220. #define ARCH_HAS_SEARCH_EXTABLE
  221. #endif
  222. int fixup_exception(struct pt_regs *regs);
  223. /* Returns 0 if exception not found and fixup.unit otherwise. */
  224. unsigned long search_exception_table(unsigned long addr);
  225. const struct exception_table_entry *search_exception_tables(unsigned long addr);
  226. extern void *set_exception_table_vec(unsigned int vec, void *handler);
  227. static inline void *set_exception_table_evt(unsigned int evt, void *handler)
  228. {
  229. return set_exception_table_vec(evt >> 5, handler);
  230. }
  231. struct mem_access {
  232. unsigned long (*from)(void *dst, const void __user *src, unsigned long cnt);
  233. unsigned long (*to)(void __user *dst, const void *src, unsigned long cnt);
  234. };
  235. int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
  236. struct mem_access *ma, int, unsigned long address);
  237. #endif /* __ASM_SH_UACCESS_H */