uaccess.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file COPYING in the main directory of this archive
  4. * for more details.
  5. */
  6. #include <linux/module.h>
  7. #include <asm/uaccess.h>
  8. unsigned long __generic_copy_from_user(void *to, const void __user *from,
  9. unsigned long n)
  10. {
  11. unsigned long tmp, res;
  12. asm volatile ("\n"
  13. " tst.l %0\n"
  14. " jeq 2f\n"
  15. "1: "MOVES".l (%1)+,%3\n"
  16. " move.l %3,(%2)+\n"
  17. " subq.l #1,%0\n"
  18. " jne 1b\n"
  19. "2: btst #1,%5\n"
  20. " jeq 4f\n"
  21. "3: "MOVES".w (%1)+,%3\n"
  22. " move.w %3,(%2)+\n"
  23. "4: btst #0,%5\n"
  24. " jeq 6f\n"
  25. "5: "MOVES".b (%1)+,%3\n"
  26. " move.b %3,(%2)+\n"
  27. "6:\n"
  28. " .section .fixup,\"ax\"\n"
  29. " .even\n"
  30. "10: move.l %0,%3\n"
  31. "7: clr.l (%2)+\n"
  32. " subq.l #1,%3\n"
  33. " jne 7b\n"
  34. " lsl.l #2,%0\n"
  35. " btst #1,%5\n"
  36. " jeq 8f\n"
  37. "30: clr.w (%2)+\n"
  38. " addq.l #2,%0\n"
  39. "8: btst #0,%5\n"
  40. " jeq 6b\n"
  41. "50: clr.b (%2)+\n"
  42. " addq.l #1,%0\n"
  43. " jra 6b\n"
  44. " .previous\n"
  45. "\n"
  46. " .section __ex_table,\"a\"\n"
  47. " .align 4\n"
  48. " .long 1b,10b\n"
  49. " .long 3b,30b\n"
  50. " .long 5b,50b\n"
  51. " .previous"
  52. : "=d" (res), "+a" (from), "+a" (to), "=&r" (tmp)
  53. : "0" (n / 4), "d" (n & 3));
  54. return res;
  55. }
  56. EXPORT_SYMBOL(__generic_copy_from_user);
  57. unsigned long __generic_copy_to_user(void __user *to, const void *from,
  58. unsigned long n)
  59. {
  60. unsigned long tmp, res;
  61. asm volatile ("\n"
  62. " tst.l %0\n"
  63. " jeq 4f\n"
  64. "1: move.l (%1)+,%3\n"
  65. "2: "MOVES".l %3,(%2)+\n"
  66. "3: subq.l #1,%0\n"
  67. " jne 1b\n"
  68. "4: btst #1,%5\n"
  69. " jeq 6f\n"
  70. " move.w (%1)+,%3\n"
  71. "5: "MOVES".w %3,(%2)+\n"
  72. "6: btst #0,%5\n"
  73. " jeq 8f\n"
  74. " move.b (%1)+,%3\n"
  75. "7: "MOVES".b %3,(%2)+\n"
  76. "8:\n"
  77. " .section .fixup,\"ax\"\n"
  78. " .even\n"
  79. "20: lsl.l #2,%0\n"
  80. "50: add.l %5,%0\n"
  81. " jra 8b\n"
  82. " .previous\n"
  83. "\n"
  84. " .section __ex_table,\"a\"\n"
  85. " .align 4\n"
  86. " .long 2b,20b\n"
  87. " .long 3b,20b\n"
  88. " .long 5b,50b\n"
  89. " .long 6b,50b\n"
  90. " .long 7b,50b\n"
  91. " .long 8b,50b\n"
  92. " .previous"
  93. : "=d" (res), "+a" (from), "+a" (to), "=&r" (tmp)
  94. : "0" (n / 4), "d" (n & 3));
  95. return res;
  96. }
  97. EXPORT_SYMBOL(__generic_copy_to_user);
  98. /*
  99. * Copy a null terminated string from userspace.
  100. */
  101. long strncpy_from_user(char *dst, const char __user *src, long count)
  102. {
  103. long res;
  104. char c;
  105. if (count <= 0)
  106. return count;
  107. asm volatile ("\n"
  108. "1: "MOVES".b (%2)+,%4\n"
  109. " move.b %4,(%1)+\n"
  110. " jeq 2f\n"
  111. " subq.l #1,%3\n"
  112. " jne 1b\n"
  113. "2: sub.l %3,%0\n"
  114. "3:\n"
  115. " .section .fixup,\"ax\"\n"
  116. " .even\n"
  117. "10: move.l %5,%0\n"
  118. " jra 3b\n"
  119. " .previous\n"
  120. "\n"
  121. " .section __ex_table,\"a\"\n"
  122. " .align 4\n"
  123. " .long 1b,10b\n"
  124. " .previous"
  125. : "=d" (res), "+a" (dst), "+a" (src), "+r" (count), "=&d" (c)
  126. : "i" (-EFAULT), "0" (count));
  127. return res;
  128. }
  129. EXPORT_SYMBOL(strncpy_from_user);
  130. /*
  131. * Return the size of a string (including the ending 0)
  132. *
  133. * Return 0 on exception, a value greater than N if too long
  134. */
  135. long strnlen_user(const char __user *src, long n)
  136. {
  137. char c;
  138. long res;
  139. asm volatile ("\n"
  140. "1: subq.l #1,%1\n"
  141. " jmi 3f\n"
  142. "2: "MOVES".b (%0)+,%2\n"
  143. " tst.b %2\n"
  144. " jne 1b\n"
  145. " jra 4f\n"
  146. "\n"
  147. "3: addq.l #1,%0\n"
  148. "4: sub.l %4,%0\n"
  149. "5:\n"
  150. " .section .fixup,\"ax\"\n"
  151. " .even\n"
  152. "20: sub.l %0,%0\n"
  153. " jra 5b\n"
  154. " .previous\n"
  155. "\n"
  156. " .section __ex_table,\"a\"\n"
  157. " .align 4\n"
  158. " .long 2b,20b\n"
  159. " .previous\n"
  160. : "=&a" (res), "+d" (n), "=&d" (c)
  161. : "0" (src), "r" (src));
  162. return res;
  163. }
  164. EXPORT_SYMBOL(strnlen_user);
  165. /*
  166. * Zero Userspace
  167. */
  168. unsigned long __clear_user(void __user *to, unsigned long n)
  169. {
  170. unsigned long res;
  171. asm volatile ("\n"
  172. " tst.l %0\n"
  173. " jeq 3f\n"
  174. "1: "MOVES".l %2,(%1)+\n"
  175. "2: subq.l #1,%0\n"
  176. " jne 1b\n"
  177. "3: btst #1,%4\n"
  178. " jeq 5f\n"
  179. "4: "MOVES".w %2,(%1)+\n"
  180. "5: btst #0,%4\n"
  181. " jeq 7f\n"
  182. "6: "MOVES".b %2,(%1)\n"
  183. "7:\n"
  184. " .section .fixup,\"ax\"\n"
  185. " .even\n"
  186. "10: lsl.l #2,%0\n"
  187. "40: add.l %4,%0\n"
  188. " jra 7b\n"
  189. " .previous\n"
  190. "\n"
  191. " .section __ex_table,\"a\"\n"
  192. " .align 4\n"
  193. " .long 1b,10b\n"
  194. " .long 2b,10b\n"
  195. " .long 4b,40b\n"
  196. " .long 5b,40b\n"
  197. " .long 6b,40b\n"
  198. " .long 7b,40b\n"
  199. " .previous"
  200. : "=d" (res), "+a" (to)
  201. : "r" (0), "0" (n / 4), "d" (n & 3));
  202. return res;
  203. }
  204. EXPORT_SYMBOL(__clear_user);