uaccess.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #ifndef __UM_UACCESS_H
  6. #define __UM_UACCESS_H
  7. /* thread_info has a mm_segment_t in it, so put the definition up here */
  8. typedef struct {
  9. unsigned long seg;
  10. } mm_segment_t;
  11. #include <linux/thread_info.h>
  12. #include <linux/errno.h>
  13. #include <asm/processor.h>
  14. #include <asm/elf.h>
  15. #define VERIFY_READ 0
  16. #define VERIFY_WRITE 1
  17. /*
  18. * The fs value determines whether argument validity checking should be
  19. * performed or not. If get_fs() == USER_DS, checking is performed, with
  20. * get_fs() == KERNEL_DS, checking is bypassed.
  21. *
  22. * For historical reasons, these macros are grossly misnamed.
  23. */
  24. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  25. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
  26. #define USER_DS MAKE_MM_SEG(TASK_SIZE)
  27. #define get_ds() (KERNEL_DS)
  28. #define get_fs() (current_thread_info()->addr_limit)
  29. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  30. #define segment_eq(a, b) ((a).seg == (b).seg)
  31. #define __under_task_size(addr, size) \
  32. (((unsigned long) (addr) < TASK_SIZE) && \
  33. (((unsigned long) (addr) + (size)) < TASK_SIZE))
  34. #define __access_ok_vsyscall(type, addr, size) \
  35. ((type == VERIFY_READ) && \
  36. ((unsigned long) (addr) >= FIXADDR_USER_START) && \
  37. ((unsigned long) (addr) + (size) <= FIXADDR_USER_END) && \
  38. ((unsigned long) (addr) + (size) >= (unsigned long)(addr)))
  39. #define __addr_range_nowrap(addr, size) \
  40. ((unsigned long) (addr) <= ((unsigned long) (addr) + (size)))
  41. #define access_ok(type, addr, size) \
  42. (__addr_range_nowrap(addr, size) && \
  43. (__under_task_size(addr, size) || \
  44. __access_ok_vsyscall(type, addr, size) || \
  45. segment_eq(get_fs(), KERNEL_DS)))
  46. extern int copy_from_user(void *to, const void __user *from, int n);
  47. extern int copy_to_user(void __user *to, const void *from, int n);
  48. /*
  49. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  50. * @dst: Destination address, in kernel space. This buffer must be at
  51. * least @count bytes long.
  52. * @src: Source address, in user space.
  53. * @count: Maximum number of bytes to copy, including the trailing NUL.
  54. *
  55. * Copies a NUL-terminated string from userspace to kernel space.
  56. *
  57. * On success, returns the length of the string (not including the trailing
  58. * NUL).
  59. *
  60. * If access to userspace fails, returns -EFAULT (some data may have been
  61. * copied).
  62. *
  63. * If @count is smaller than the length of the string, copies @count bytes
  64. * and returns @count.
  65. */
  66. extern int strncpy_from_user(char *dst, const char __user *src, int count);
  67. /*
  68. * __clear_user: - Zero a block of memory in user space, with less checking.
  69. * @to: Destination address, in user space.
  70. * @n: Number of bytes to zero.
  71. *
  72. * Zero a block of memory in user space. Caller must check
  73. * the specified block with access_ok() before calling this function.
  74. *
  75. * Returns number of bytes that could not be cleared.
  76. * On success, this will be zero.
  77. */
  78. extern int __clear_user(void __user *mem, int len);
  79. /*
  80. * clear_user: - Zero a block of memory in user space.
  81. * @to: Destination address, in user space.
  82. * @n: Number of bytes to zero.
  83. *
  84. * Zero a block of memory in user space.
  85. *
  86. * Returns number of bytes that could not be cleared.
  87. * On success, this will be zero.
  88. */
  89. extern int clear_user(void __user *mem, int len);
  90. /*
  91. * strlen_user: - Get the size of a string in user space.
  92. * @str: The string to measure.
  93. * @n: The maximum valid length
  94. *
  95. * Get the size of a NUL-terminated string in user space.
  96. *
  97. * Returns the size of the string INCLUDING the terminating NUL.
  98. * On exception, returns 0.
  99. * If the string is too long, returns a value greater than @n.
  100. */
  101. extern int strnlen_user(const void __user *str, int len);
  102. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  103. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  104. #define __copy_to_user_inatomic __copy_to_user
  105. #define __copy_from_user_inatomic __copy_from_user
  106. #define __get_user(x, ptr) \
  107. ({ \
  108. const __typeof__(*(ptr)) __user *__private_ptr = (ptr); \
  109. __typeof__(x) __private_val; \
  110. int __private_ret = -EFAULT; \
  111. (x) = (__typeof__(*(__private_ptr)))0; \
  112. if (__copy_from_user((__force void *)&__private_val, (__private_ptr),\
  113. sizeof(*(__private_ptr))) == 0) { \
  114. (x) = (__typeof__(*(__private_ptr))) __private_val; \
  115. __private_ret = 0; \
  116. } \
  117. __private_ret; \
  118. })
  119. #define get_user(x, ptr) \
  120. ({ \
  121. const __typeof__((*(ptr))) __user *private_ptr = (ptr); \
  122. (access_ok(VERIFY_READ, private_ptr, sizeof(*private_ptr)) ? \
  123. __get_user(x, private_ptr) : ((x) = (__typeof__(*ptr))0, -EFAULT)); \
  124. })
  125. #define __put_user(x, ptr) \
  126. ({ \
  127. __typeof__(*(ptr)) __user *__private_ptr = ptr; \
  128. __typeof__(*(__private_ptr)) __private_val; \
  129. int __private_ret = -EFAULT; \
  130. __private_val = (__typeof__(*(__private_ptr))) (x); \
  131. if (__copy_to_user((__private_ptr), &__private_val, \
  132. sizeof(*(__private_ptr))) == 0) { \
  133. __private_ret = 0; \
  134. } \
  135. __private_ret; \
  136. })
  137. #define put_user(x, ptr) \
  138. ({ \
  139. __typeof__(*(ptr)) __user *private_ptr = (ptr); \
  140. (access_ok(VERIFY_WRITE, private_ptr, sizeof(*private_ptr)) ? \
  141. __put_user(x, private_ptr) : -EFAULT); \
  142. })
  143. #define strlen_user(str) strnlen_user(str, ~0U >> 1)
  144. struct exception_table_entry
  145. {
  146. unsigned long insn;
  147. unsigned long fixup;
  148. };
  149. #endif