uaccess.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* this function will go away soon - use access_ok() instead */
  20. extern inline int __deprecated verify_area(int type, const void * addr, unsigned long size)
  21. {
  22. return access_ok(type,addr,size)?0:-EFAULT;
  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. #define put_user(x, ptr) \
  47. ({ \
  48. int __pu_err = 0; \
  49. typeof(*(ptr)) __pu_val = (x); \
  50. switch (sizeof (*(ptr))) { \
  51. case 1: \
  52. __put_user_asm(__pu_err, __pu_val, ptr, b); \
  53. break; \
  54. case 2: \
  55. __put_user_asm(__pu_err, __pu_val, ptr, w); \
  56. break; \
  57. case 4: \
  58. __put_user_asm(__pu_err, __pu_val, ptr, l); \
  59. break; \
  60. case 8: \
  61. memcpy(ptr, &__pu_val, sizeof (*(ptr))); \
  62. break; \
  63. default: \
  64. __pu_err = __put_user_bad(); \
  65. break; \
  66. } \
  67. __pu_err; \
  68. })
  69. #define __put_user(x, ptr) put_user(x, ptr)
  70. extern int __put_user_bad(void);
  71. /*
  72. * Tell gcc we read from memory instead of writing: this is because
  73. * we do not write to any memory gcc knows about, so there are no
  74. * aliasing issues.
  75. */
  76. #define __ptr(x) ((unsigned long *)(x))
  77. #define __put_user_asm(err,x,ptr,bwl) \
  78. __asm__ ("move" #bwl " %0,%1" \
  79. : /* no outputs */ \
  80. :"d" (x),"m" (*__ptr(ptr)) : "memory")
  81. #define get_user(x, ptr) \
  82. ({ \
  83. int __gu_err = 0; \
  84. typeof(*(ptr)) __gu_val = 0; \
  85. switch (sizeof(*(ptr))) { \
  86. case 1: \
  87. __get_user_asm(__gu_err, __gu_val, ptr, b, "=d"); \
  88. break; \
  89. case 2: \
  90. __get_user_asm(__gu_err, __gu_val, ptr, w, "=r"); \
  91. break; \
  92. case 4: \
  93. __get_user_asm(__gu_err, __gu_val, ptr, l, "=r"); \
  94. break; \
  95. case 8: \
  96. memcpy(&__gu_val, ptr, sizeof (*(ptr))); \
  97. break; \
  98. default: \
  99. __gu_val = 0; \
  100. __gu_err = __get_user_bad(); \
  101. break; \
  102. } \
  103. (x) = __gu_val; \
  104. __gu_err; \
  105. })
  106. #define __get_user(x, ptr) get_user(x, ptr)
  107. extern int __get_user_bad(void);
  108. #define __get_user_asm(err,x,ptr,bwl,reg) \
  109. __asm__ ("move" #bwl " %1,%0" \
  110. : "=d" (x) \
  111. : "m" (*__ptr(ptr)))
  112. #define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
  113. #define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
  114. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  115. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  116. #define __copy_to_user_inatomic __copy_to_user
  117. #define __copy_from_user_inatomic __copy_from_user
  118. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  119. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  120. /*
  121. * Copy a null terminated string from userspace.
  122. */
  123. static inline long
  124. strncpy_from_user(char *dst, const char *src, long count)
  125. {
  126. char *tmp;
  127. strncpy(dst, src, count);
  128. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  129. ;
  130. return(tmp - dst); /* DAVIDM should we count a NUL ? check getname */
  131. }
  132. /*
  133. * Return the size of a string (including the ending 0)
  134. *
  135. * Return 0 on exception, a value greater than N if too long
  136. */
  137. static inline long strnlen_user(const char *src, long n)
  138. {
  139. return(strlen(src) + 1); /* DAVIDM make safer */
  140. }
  141. #define strlen_user(str) strnlen_user(str, 32767)
  142. /*
  143. * Zero Userspace
  144. */
  145. static inline unsigned long
  146. clear_user(void *to, unsigned long n)
  147. {
  148. memset(to, 0, n);
  149. return 0;
  150. }
  151. #endif /* _M68KNOMMU_UACCESS_H */