uaccess.h 7.3 KB

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