uaccess.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * U-boot - uaccess.h
  3. *
  4. * Copyright (c) 2005 blackfin.uclinux.org
  5. *
  6. * This file is based on
  7. * Based on: include/asm-m68knommu/uaccess.h
  8. * Changes made by Lineo Inc. May 2001
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #ifndef __BLACKFIN_UACCESS_H
  29. #define __BLACKFIN_UACCESS_H
  30. /*
  31. * User space memory access functions
  32. */
  33. #include <asm/segment.h>
  34. #include <asm/errno.h>
  35. #define VERIFY_READ 0
  36. #define VERIFY_WRITE 1
  37. /* We let the MMU do all checking */
  38. static inline int access_ok(int type, const void *addr, unsigned long size)
  39. {
  40. return ((unsigned long)addr < 0x10f00000); /* need final decision - Tony */
  41. }
  42. static inline int verify_area(int type, const void *addr, unsigned long size)
  43. {
  44. return access_ok(type, addr, size) ? 0 : -EFAULT;
  45. }
  46. /*
  47. * The exception table consists of pairs of addresses: the first is the
  48. * address of an instruction that is allowed to fault, and the second is
  49. * the address at which the program should continue. No registers are
  50. * modified, so it is entirely up to the continuation code to figure out
  51. * what to do.
  52. *
  53. * All the routines below use bits of fixup code that are out of line
  54. * with the main instruction path. This means when everything is well,
  55. * we don't even have to jump over them. Further, they do not intrude
  56. * on our cache or tlb entries.
  57. */
  58. struct exception_table_entry {
  59. unsigned long insn, fixup;
  60. };
  61. /* Returns 0 if exception not found and fixup otherwise. */
  62. extern unsigned long search_exception_table(unsigned long);
  63. /*
  64. * These are the main single-value transfer routines. They automatically
  65. * use the right size if we just have the right pointer type.
  66. */
  67. #define put_user(x, ptr) \
  68. ({ \
  69. int __pu_err = 0; \
  70. typeof(*(ptr)) __pu_val = (x); \
  71. switch (sizeof (*(ptr))) { \
  72. case 1: \
  73. __put_user_asm(__pu_err, __pu_val, ptr, B); \
  74. break; \
  75. case 2: \
  76. __put_user_asm(__pu_err, __pu_val, ptr, W); \
  77. break; \
  78. case 4: \
  79. __put_user_asm(__pu_err, __pu_val, ptr, ); \
  80. break; \
  81. default: \
  82. __pu_err = __put_user_bad(); \
  83. break; \
  84. } \
  85. __pu_err; \
  86. })
  87. /*
  88. * [pregs] = dregs ==> 32bits
  89. * H[pregs] = dregs ==> 16bits
  90. * B[pregs] = dregs ==> 8 bits
  91. */
  92. #define __put_user(x, ptr) put_user(x, ptr)
  93. static inline int bad_user_access_length(void)
  94. {
  95. panic("bad_user_access_length");
  96. return -1;
  97. }
  98. #define __put_user_bad() (bad_user_access_length(), (-EFAULT))
  99. /*
  100. * Tell gcc we read from memory instead of writing: this is because
  101. * we do not write to any memory gcc knows about, so there are no
  102. * aliasing issues.
  103. */
  104. #define __ptr(x) ((unsigned long *)(x))
  105. #define __put_user_asm(err,x,ptr,bhw) \
  106. __asm__ (#bhw"[%1] = %0;\n\t" \
  107. : /* no outputs */ \
  108. :"d" (x),"a" (__ptr(ptr)) : "memory")
  109. #define get_user(x, ptr) \
  110. ({ \
  111. int __gu_err = 0; \
  112. typeof(*(ptr)) __gu_val = 0; \
  113. switch (sizeof(*(ptr))) { \
  114. case 1: \
  115. __get_user_asm(__gu_err, __gu_val, ptr, B, "=d",(Z)); \
  116. break; \
  117. case 2: \
  118. __get_user_asm(__gu_err, __gu_val, ptr, W, "=r",(Z)); \
  119. break; \
  120. case 4: \
  121. __get_user_asm(__gu_err, __gu_val, ptr, , "=r",); \
  122. break; \
  123. default: \
  124. __gu_val = 0; \
  125. __gu_err = __get_user_bad(); \
  126. break; \
  127. } \
  128. (x) = __gu_val; \
  129. __gu_err; \
  130. })
  131. /* dregs = [pregs] ==> 32bits
  132. * H[pregs] ==> 16bits
  133. * B[pregs] ==> 8 bits
  134. */
  135. #define __get_user(x, ptr) get_user(x, ptr)
  136. #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
  137. #define __get_user_asm(err,x,ptr,bhw,reg,option) \
  138. __asm__ ("%0 =" #bhw "[%1]"#option";\n\t" \
  139. : "=d" (x) \
  140. : "a" (__ptr(ptr)))
  141. #define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
  142. #define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
  143. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  144. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  145. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  146. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  147. /*
  148. * Copy a null terminated string from userspace.
  149. */
  150. static inline long strncpy_from_user(char *dst, const char *src, long count)
  151. {
  152. char *tmp;
  153. strncpy(dst, src, count);
  154. for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
  155. return (tmp - dst); /* DAVIDM should we count a NUL ? check getname */
  156. }
  157. /*
  158. * Return the size of a string (including the ending 0)
  159. *
  160. * Return 0 on exception, a value greater than N if too long
  161. */
  162. static inline long strnlen_user(const char *src, long n)
  163. {
  164. return (strlen(src) + 1); /* DAVIDM make safer */
  165. }
  166. #define strlen_user(str) strnlen_user(str, 32767)
  167. /*
  168. * Zero Userspace
  169. */
  170. static inline unsigned long clear_user(void *to, unsigned long n)
  171. {
  172. memset(to, 0, n);
  173. return (0);
  174. }
  175. #endif