uaccess.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef __H8300_UACCESS_H
  2. #define __H8300_UACCESS_H
  3. /*
  4. * User space memory access functions
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/mm.h>
  8. #include <linux/string.h>
  9. #include <asm/segment.h>
  10. #define VERIFY_READ 0
  11. #define VERIFY_WRITE 1
  12. /* We let the MMU do all checking */
  13. #define access_ok(type, addr, size) __access_ok((unsigned long)addr,size)
  14. static inline int __access_ok(unsigned long addr, unsigned long size)
  15. {
  16. #define RANGE_CHECK_OK(addr, size, lower, upper) \
  17. (((addr) >= (lower)) && (((addr) + (size)) < (upper)))
  18. extern unsigned long _ramend;
  19. return(RANGE_CHECK_OK(addr, size, 0L, (unsigned long)&_ramend));
  20. }
  21. /*
  22. * The exception table consists of pairs of addresses: the first is the
  23. * address of an instruction that is allowed to fault, and the second is
  24. * the address at which the program should continue. No registers are
  25. * modified, so it is entirely up to the continuation code to figure out
  26. * what to do.
  27. *
  28. * All the routines below use bits of fixup code that are out of line
  29. * with the main instruction path. This means when everything is well,
  30. * we don't even have to jump over them. Further, they do not intrude
  31. * on our cache or tlb entries.
  32. */
  33. struct exception_table_entry
  34. {
  35. unsigned long insn, fixup;
  36. };
  37. /* Returns 0 if exception not found and fixup otherwise. */
  38. extern unsigned long search_exception_table(unsigned long);
  39. /*
  40. * These are the main single-value transfer routines. They automatically
  41. * use the right size if we just have the right pointer type.
  42. */
  43. #define put_user(x, ptr) \
  44. ({ \
  45. int __pu_err = 0; \
  46. typeof(*(ptr)) __pu_val = (x); \
  47. switch (sizeof (*(ptr))) { \
  48. case 1: \
  49. case 2: \
  50. case 4: \
  51. *(ptr) = (__pu_val); \
  52. break; \
  53. case 8: \
  54. memcpy(ptr, &__pu_val, sizeof (*(ptr))); \
  55. break; \
  56. default: \
  57. __pu_err = __put_user_bad(); \
  58. break; \
  59. } \
  60. __pu_err; \
  61. })
  62. #define __put_user(x, ptr) put_user(x, ptr)
  63. extern int __put_user_bad(void);
  64. /*
  65. * Tell gcc we read from memory instead of writing: this is because
  66. * we do not write to any memory gcc knows about, so there are no
  67. * aliasing issues.
  68. */
  69. #define __ptr(x) ((unsigned long *)(x))
  70. /*
  71. * Tell gcc we read from memory instead of writing: this is because
  72. * we do not write to any memory gcc knows about, so there are no
  73. * aliasing issues.
  74. */
  75. #define get_user(x, ptr) \
  76. ({ \
  77. int __gu_err = 0; \
  78. typeof(*(ptr)) __gu_val = *ptr; \
  79. switch (sizeof(*(ptr))) { \
  80. case 1: \
  81. case 2: \
  82. case 4: \
  83. case 8: \
  84. break; \
  85. default: \
  86. __gu_err = __get_user_bad(); \
  87. __gu_val = 0; \
  88. break; \
  89. } \
  90. (x) = __gu_val; \
  91. __gu_err; \
  92. })
  93. #define __get_user(x, ptr) get_user(x, ptr)
  94. extern int __get_user_bad(void);
  95. #define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
  96. #define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
  97. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  98. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  99. #define __copy_to_user_inatomic __copy_to_user
  100. #define __copy_from_user_inatomic __copy_from_user
  101. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  102. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  103. /*
  104. * Copy a null terminated string from userspace.
  105. */
  106. static inline long
  107. strncpy_from_user(char *dst, const char *src, long count)
  108. {
  109. char *tmp;
  110. strncpy(dst, src, count);
  111. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  112. ;
  113. return(tmp - dst); /* DAVIDM should we count a NUL ? check getname */
  114. }
  115. /*
  116. * Return the size of a string (including the ending 0)
  117. *
  118. * Return 0 on exception, a value greater than N if too long
  119. */
  120. static inline long strnlen_user(const char *src, long n)
  121. {
  122. return(strlen(src) + 1); /* DAVIDM make safer */
  123. }
  124. #define strlen_user(str) strnlen_user(str, 32767)
  125. /*
  126. * Zero Userspace
  127. */
  128. static inline unsigned long
  129. clear_user(void *to, unsigned long n)
  130. {
  131. memset(to, 0, n);
  132. return 0;
  133. }
  134. #endif /* _H8300_UACCESS_H */