uaccess.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef __V850_UACCESS_H__
  2. #define __V850_UACCESS_H__
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/string.h>
  8. #include <asm/segment.h>
  9. #include <asm/machdep.h>
  10. #define VERIFY_READ 0
  11. #define VERIFY_WRITE 1
  12. extern inline int access_ok (int type, const void *addr, unsigned long size)
  13. {
  14. /* XXX I guess we should check against real ram bounds at least, and
  15. possibly make sure ADDR is not within the kernel.
  16. For now we just check to make sure it's not a small positive
  17. or negative value, as that will at least catch some kinds of
  18. error. In particular, we make sure that ADDR's not within the
  19. interrupt vector area, which we know starts at zero, or within the
  20. peripheral-I/O area, which is located just _before_ zero. */
  21. unsigned long val = (unsigned long)addr;
  22. return val >= (0x80 + NUM_CPU_IRQS*16) && val < 0xFFFFF000;
  23. }
  24. /*
  25. * The exception table consists of pairs of addresses: the first is the
  26. * address of an instruction that is allowed to fault, and the second is
  27. * the address at which the program should continue. No registers are
  28. * modified, so it is entirely up to the continuation code to figure out
  29. * what to do.
  30. *
  31. * All the routines below use bits of fixup code that are out of line
  32. * with the main instruction path. This means when everything is well,
  33. * we don't even have to jump over them. Further, they do not intrude
  34. * on our cache or tlb entries.
  35. */
  36. struct exception_table_entry
  37. {
  38. unsigned long insn, fixup;
  39. };
  40. /* Returns 0 if exception not found and fixup otherwise. */
  41. extern unsigned long search_exception_table (unsigned long);
  42. /*
  43. * These are the main single-value transfer routines. They automatically
  44. * use the right size if we just have the right pointer type.
  45. */
  46. extern int bad_user_access_length (void);
  47. #define __get_user(var, ptr) \
  48. ({ \
  49. int __gu_err = 0; \
  50. typeof(*(ptr)) __gu_val = 0; \
  51. switch (sizeof (*(ptr))) { \
  52. case 1: \
  53. case 2: \
  54. case 4: \
  55. __gu_val = *(ptr); \
  56. break; \
  57. case 8: \
  58. memcpy(&__gu_val, ptr, sizeof(__gu_val)); \
  59. break; \
  60. default: \
  61. __gu_val = 0; \
  62. __gu_err = __get_user_bad (); \
  63. break; \
  64. } \
  65. (var) = __gu_val; \
  66. __gu_err; \
  67. })
  68. #define __get_user_bad() (bad_user_access_length (), (-EFAULT))
  69. #define __put_user(var, ptr) \
  70. ({ \
  71. int __pu_err = 0; \
  72. switch (sizeof (*(ptr))) { \
  73. case 1: \
  74. case 2: \
  75. case 4: \
  76. *(ptr) = (var); \
  77. break; \
  78. case 8: { \
  79. typeof(*(ptr)) __pu_val = 0; \
  80. memcpy(ptr, &__pu_val, sizeof(__pu_val)); \
  81. } \
  82. break; \
  83. default: \
  84. __pu_err = __put_user_bad (); \
  85. break; \
  86. } \
  87. __pu_err; \
  88. })
  89. #define __put_user_bad() (bad_user_access_length (), (-EFAULT))
  90. #define put_user(x, ptr) __put_user(x, ptr)
  91. #define get_user(x, ptr) __get_user(x, ptr)
  92. #define __copy_from_user(to, from, n) (memcpy (to, from, n), 0)
  93. #define __copy_to_user(to, from, n) (memcpy(to, from, n), 0)
  94. #define __copy_to_user_inatomic __copy_to_user
  95. #define __copy_from_user_inatomic __copy_from_user
  96. #define copy_from_user(to, from, n) __copy_from_user (to, from, n)
  97. #define copy_to_user(to, from, n) __copy_to_user(to, from, n)
  98. #define copy_to_user_ret(to,from,n,retval) \
  99. ({ if (copy_to_user (to,from,n)) return retval; })
  100. #define copy_from_user_ret(to,from,n,retval) \
  101. ({ if (copy_from_user (to,from,n)) return retval; })
  102. /*
  103. * Copy a null terminated string from userspace.
  104. */
  105. static inline long
  106. strncpy_from_user (char *dst, const char *src, long count)
  107. {
  108. char *tmp;
  109. strncpy (dst, src, count);
  110. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  111. ;
  112. return tmp - dst;
  113. }
  114. /*
  115. * Return the size of a string (including the ending 0)
  116. *
  117. * Return 0 on exception, a value greater than N if too long
  118. */
  119. static inline long strnlen_user (const char *src, long n)
  120. {
  121. return strlen (src) + 1;
  122. }
  123. #define strlen_user(str) strnlen_user (str, 32767)
  124. /*
  125. * Zero Userspace
  126. */
  127. static inline unsigned long
  128. clear_user (void *to, unsigned long n)
  129. {
  130. memset (to, 0, n);
  131. return 0;
  132. }
  133. #endif /* __V850_UACCESS_H__ */