uaccess.h 4.4 KB

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