rtuDataOperationAnalysis.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2007 Maciej W. Rozycki
  9. */
  10. #ifndef _ASM_UACCESS_H
  11. #define _ASM_UACCESS_H
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/thread_info.h>
  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. #ifdef CONFIG_32BIT
  23. #define __UA_LIMIT 0x80000000UL
  24. #define __UA_ADDR ".word"
  25. #define __UA_LA "la"
  26. #define __UA_ADDU "addu"
  27. #define __UA_t0 "$8"
  28. #define __UA_t1 "$9"
  29. #endif /* CONFIG_32BIT */
  30. #ifdef CONFIG_64BIT
  31. extern u64 __ua_limit;
  32. #define __UA_LIMIT __ua_limit
  33. #define __UA_ADDR ".dword"
  34. #define __UA_LA "dla"
  35. #define __UA_ADDU "daddu"
  36. #define __UA_t0 "$12"
  37. #define __UA_t1 "$13"
  38. #endif /* CONFIG_64BIT */
  39. /*
  40. * USER_DS is a bitmask that has the bits set that may not be set in a valid
  41. * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
  42. * the arithmetic we're doing only works if the limit is a power of two, so
  43. * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
  44. * address in this range it's the process's problem, not ours :-)
  45. */
  46. #define KERNEL_DS ((mm_segment_t) { 0UL })
  47. #define USER_DS ((mm_segment_t) { __UA_LIMIT })
  48. #define VERIFY_READ 0
  49. #define VERIFY_WRITE 1
  50. #define get_ds() (KERNEL_DS)
  51. #define get_fs() (current_thread_info()->addr_limit)
  52. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  53. #define segment_eq(a, b) ((a).seg == (b).seg)
  54. /*
  55. * Is a address valid? This does a straighforward calculation rather
  56. * than tests.
  57. *
  58. * Address valid if:
  59. * - "addr" doesn't have any high-bits set
  60. * - AND "size" doesn't have any high-bits set
  61. * - AND "addr+size" doesn't have any high-bits set
  62. * - OR we are in kernel mode.
  63. *
  64. * __ua_size() is a trick to avoid runtime checking of positive constant
  65. * sizes; for those we already know at compile time that the size is ok.
  66. */
  67. #define __ua_size(size) \
  68. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  69. /*
  70. * access_ok: - Checks if a user space pointer is valid
  71. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  72. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  73. * to write to a block, it is always safe to read from it.
  74. * @addr: User space pointer to start of block to check
  75. * @size: Size of block to check
  76. *
  77. * Context: User context only. This function may sleep.
  78. *
  79. * Checks if a pointer to a block of memory in user space is valid.
  80. *
  81. * Returns true (nonzero) if the memory block may be valid, false (zero)
  82. * if it is definitely invalid.
  83. *
  84. * Note that, depending on architecture, this function probably just
  85. * checks that the pointer is in the user space range - after calling
  86. * this function, memory access functions may still return -EFAULT.
  87. */
  88. #define __access_mask get_fs().seg
  89. #define __access_ok(addr, size, mask) \
  90. ({ \
  91. unsigned long __addr = (unsigned long) (addr); \
  92. unsigned long __size = size; \
  93. unsigned long __mask = mask; \
  94. unsigned long __ok; \
  95. \
  96. __chk_user_ptr(addr); \
  97. __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
  98. __ua_size(__size))); \
  99. __ok == 0; \
  100. })
  101. #define access_ok(type, addr, size) \
  102. likely(__access_ok((addr), (size), __access_mask))
  103. /*
  104. * put_user: - Write a simple value into user space.
  105. * @x: Value to copy to user space.
  106. * @ptr: Destination address, in user space.
  107. *
  108. * Context: User context only. This function may sleep.
  109. *
  110. * This macro copies a single simple value from kernel space to user
  111. * space. It supports simple types like char and int, but not larger
  112. * data types like structures or arrays.
  113. *
  114. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  115. * to the result of dereferencing @ptr.
  116. *
  117. * Returns zero on success, or -EFAULT on error.
  118. */
  119. #define put_user(x,ptr) \
  120. __put_user_check((x), (ptr), sizeof(*(ptr)))
  121. /*
  122. * get_user: - Get a simple variable from user space.
  123. * @x: Variable to store result.
  124. * @ptr: Source address, in user space.
  125. *
  126. * Context: User context only. This function may sleep.
  127. *
  128. * This macro copies a single simple variable from user space to kernel
  129. * space. It supports simple types like char and int, but not larger
  130. * data types like structures or arrays.
  131. *
  132. * @ptr must have pointer-to-simple-variable type, and the result of
  133. * dereferencing @ptr must be assignable to @x without a cast.
  134. *
  135. * Returns zero on success, or -EFAULT on error.
  136. * On error, the variable @x is set to zero.
  137. */
  138. #define get_user(x,ptr) \
  139. __get_user_check((x), (ptr), sizeof(*(ptr)))
  140. /*
  141. * __put_user: - Write a simple value into user space, with less checking.
  142. * @x: Value to copy to user space.
  143. * @ptr: Destination address, in user space.
  144. *
  145. * Context: User context only. This function may sleep.
  146. *
  147. * This macro copies a single simple value from kernel space to user
  148. * space. It supports simple types like char and int, but not larger
  149. * data types like structures or arrays.
  150. *
  151. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  152. * to the result of dereferencing @ptr.
  153. *
  154. * Caller must check the pointer with access_ok() before calling this
  155. * function.
  156. *
  157. * Returns zero on success, or -EFAULT on error.
  158. */
  159. #define __put_user(x,ptr) \
  160. __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  161. /*
  162. * __get_user: - Get a simple variable from user space, with less checking.
  163. * @x: Variable to store result.
  164. * @ptr: Source address, in user space.
  165. *
  166. * Context: User context only. This function may sleep.
  167. *
  168. * This macro copies a single simple variable from user space to kernel
  169. * space. It supports simple types like char and int, but not larger
  170. * data types like structures or arrays.
  171. *
  172. * @ptr must have pointer-to-simple-variable type, and the result of