uaccess.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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,
  43. unsigned long size)
  44. {
  45. return access_ok(type, addr, size) ? 0 : -EFAULT;
  46. }
  47. /*
  48. * The exception table consists of pairs of addresses: the first is the
  49. * address of an instruction that is allowed to fault, and the second is
  50. * the address at which the program should continue. No registers are
  51. * modified, so it is entirely up to the continuation code to figure out
  52. * what to do.
  53. *
  54. * All the routines below use bits of fixup code that are out of line
  55. * with the main instruction path. This means when everything is well,
  56. * we don't even have to jump over them. Further, they do not intrude
  57. * on our cache or tlb entries.
  58. */
  59. struct exception_table_entry {
  60. unsigned long insn, fixup;
  61. };
  62. /* Returns 0 if exception not found and fixup otherwise. */
  63. extern unsigned long search_exception_table(unsigned long);
  64. /*
  65. * These are the main single-value transfer routines. They automatically
  66. * use the right size if we just have the right pointer type.
  67. */
  68. #define put_user(x, ptr) \
  69. ({ \
  70. int __pu_err = 0; \
  71. typeof(*(ptr)) __pu_val = (x); \
  72. switch (sizeof (*(ptr))) { \
  73. case 1: \
  74. __put_user_asm(__pu_err, __pu_val, ptr, B); \
  75. break; \
  76. case 2: \
  77. __put_user_asm(__pu_err, __pu_val, ptr, W); \
  78. break; \
  79. case 4: \
  80. __put_user_asm(__pu_err, __pu_val, ptr, ); \
  81. break; \
  82. default: \
  83. __pu_err = __put_user_bad(); \
  84. break; \
  85. } \
  86. __pu_err; \
  87. })
  88. /*
  89. * [pregs] = dregs ==> 32bits
  90. * H[pregs] = dregs ==> 16bits
  91. * B[pregs] = dregs ==> 8 bits
  92. */
  93. #define __put_user(x, ptr) put_user(x, ptr)
  94. static inline int bad_user_access_length(void)
  95. {
  96. panic("bad_user_access_length");
  97. return -1;
  98. }
  99. #define __put_user_bad() (bad_user_access_length(), (-EFAULT))
  100. /*
  101. * Tell gcc we read from memory instead of writing: this is because
  102. * we do not write to any memory gcc knows about, so there are no
  103. * aliasing issues.
  104. */
  105. #define __ptr(x) ((unsigned long *)(x))
  106. #define __put_user_asm(err,x,ptr,bhw) \
  107. __asm__ (#bhw"[%1] = %0;\n\t" \
  108. : /* no outputs */ \
  109. :"d" (x),"a" (__ptr(ptr)) : "memory")
  110. #define get_user(x, ptr) \
  111. ({ \
  112. int __gu_err = 0; \
  113. typeof(*(ptr)) __gu_val = 0; \
  114. switch (sizeof(*(ptr))) { \
  115. case 1: \
  116. __get_user_asm(__gu_err, __gu_val, ptr, B, "=d",(Z)); \
  117. break; \
  118. case 2: \
  119. __get_user_asm(__gu_err, __gu_val, ptr, W, "=r",(Z)); \
  120. break; \
  121. case 4: \
  122. __get_user_asm(__gu_err, __gu_val, ptr, , "=r",); \
  123. break; \
  124. default: \
  125. __gu_val = 0; \
  126. __gu_err = __get_user_bad(); \
  127. break; \
  128. } \
  129. (x) = __gu_val; \
  130. __gu_err; \
  131. })
  132. /* dregs = [pregs] ==> 32bits
  133. * H[pregs] ==> 16bits
  134. * B[pregs] ==> 8 bits
  135. */
  136. #define __get_user(x, ptr) get_user(x, ptr)
  137. #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
  138. #define __get_user_asm(err,x,ptr,bhw,reg,option) \
  139. __asm__ ("%0 =" #bhw "[%1]"#option";\n\t" \
  140. : "=d" (x) \
  141. : "a" (__ptr(ptr)))
  142. #define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
  143. #define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
  144. #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
  145. #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
  146. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  147. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  148. /*
  149. * Copy a null terminated string from userspace.
  150. */
  151. static inline long strncpy_from_user(char *dst, const char *src,
  152. long count)
  153. {
  154. char *tmp;
  155. strncpy(dst, src, count);
  156. for (tmp = dst; *tmp && count > 0; tmp++, count--);
  157. return (tmp - dst); /* DAVIDM should we count a NUL ? check getname */
  158. }
  159. /*
  160. * Return the size of a string (including the ending 0)
  161. *
  162. * Return 0 on exception, a value greater than N if too long
  163. */
  164. static inline long strnlen_user(const char *src, long n)
  165. {
  166. return (strlen(src) + 1); /* DAVIDM make safer */
  167. }
  168. #define strlen_user(str) strnlen_user(str, 32767)
  169. /*
  170. * Zero Userspace
  171. */
  172. static inline unsigned long clear_user(void *to, unsigned long n)
  173. {
  174. memset(to, 0, n);
  175. return (0);
  176. }
  177. #endif