uaccess.h 4.5 KB

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