usercopy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * User address space access functions.
  3. * The non-inlined parts of asm-cris/uaccess.h are here.
  4. *
  5. * Copyright (C) 2000, 2003 Axis Communications AB.
  6. *
  7. * Written by Hans-Peter Nilsson.
  8. * Pieces used from memcpy, originally by Kenny Ranerup long time ago.
  9. */
  10. #include <asm/uaccess.h>
  11. /* Asm:s have been tweaked (within the domain of correctness) to give
  12. satisfactory results for "gcc version 3.2.1 Axis release R53/1.53-v32".
  13. Check regularly...
  14. Note that for CRISv32, the PC saved at a bus-fault is the address
  15. *at* the faulting instruction, with a special case for instructions
  16. in delay slots: then it's the address of the branch. Note also that
  17. in contrast to v10, a postincrement in the instruction is *not*
  18. performed at a bus-fault; the register is seen having the original
  19. value in fault handlers. */
  20. /* Copy to userspace. This is based on the memcpy used for
  21. kernel-to-kernel copying; see "string.c". */
  22. unsigned long __copy_user(void __user *pdst, const void *psrc, unsigned long pn)
  23. {
  24. /* We want the parameters put in special registers.
  25. Make sure the compiler is able to make something useful of this.
  26. As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
  27. FIXME: Comment for old gcc version. Check.
  28. If gcc was alright, it really would need no temporaries, and no
  29. stack space to save stuff on. */
  30. register char *dst __asm__ ("r13") = pdst;
  31. register const char *src __asm__ ("r11") = psrc;
  32. register int n __asm__ ("r12") = pn;
  33. register int retn __asm__ ("r10") = 0;
  34. /* When src is aligned but not dst, this makes a few extra needless
  35. cycles. I believe it would take as many to check that the
  36. re-alignment was unnecessary. */
  37. if (((unsigned long) dst & 3) != 0
  38. /* Don't align if we wouldn't copy more than a few bytes; so we
  39. don't have to check further for overflows. */
  40. && n >= 3)
  41. {
  42. if ((unsigned long) dst & 1)
  43. {
  44. __asm_copy_to_user_1 (dst, src, retn);
  45. n--;
  46. }
  47. if ((unsigned long) dst & 2)
  48. {
  49. __asm_copy_to_user_2 (dst, src, retn);
  50. n -= 2;
  51. }
  52. }
  53. /* Movem is dirt cheap. The overheap is low enough to always use the
  54. minimum possible block size as the threshold. */
  55. if (n >= 44)
  56. {
  57. /* For large copies we use 'movem'. */
  58. /* It is not optimal to tell the compiler about clobbering any
  59. registers; that will move the saving/restoring of those registers
  60. to the function prologue/epilogue, and make non-movem sizes
  61. suboptimal. */
  62. __asm__ volatile ("\
  63. ;; Check that the register asm declaration got right. \n\
  64. ;; The GCC manual explicitly says TRT will happen. \n\
  65. .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
  66. .err \n\
  67. .endif \n\
  68. \n\
  69. ;; Save the registers we'll use in the movem process \n\
  70. ;; on the stack. \n\
  71. subq 11*4,$sp \n\
  72. movem $r10,[$sp] \n\
  73. \n\
  74. ;; Now we've got this: \n\
  75. ;; r11 - src \n\
  76. ;; r13 - dst \n\
  77. ;; r12 - n \n\
  78. \n\
  79. ;; Update n for the first loop \n\
  80. subq 44,$r12 \n\
  81. 0: \n\
  82. movem [$r11+],$r10 \n\
  83. subq 44,$r12 \n\
  84. 1: bge 0b \n\
  85. movem $r10,[$r13+] \n\
  86. 3: \n\
  87. addq 44,$r12 ;; compensate for last loop underflowing n \n\
  88. \n\
  89. ;; Restore registers from stack \n\
  90. movem [$sp+],$r10 \n\
  91. 2: \n\
  92. .section .fixup,\"ax\" \n\
  93. 4: \n\
  94. ; When failing on any of the 1..44 bytes in a chunk, we adjust back the \n\
  95. ; source pointer and just drop through to the by-16 and by-4 loops to \n\
  96. ; get the correct number of failing bytes. This necessarily means a \n\
  97. ; few extra exceptions, but invalid user pointers shouldn't happen in \n\
  98. ; time-critical code anyway. \n\
  99. jump 3b \n\
  100. subq 44,$r11 \n\
  101. \n\
  102. .previous \n\
  103. .section __ex_table,\"a\" \n\
  104. .dword 1b,4b \n\
  105. .previous"
  106. /* Outputs */ : "=r" (dst), "=r" (src), "=r" (n), "=r" (retn)
  107. /* Inputs */ : "0" (dst), "1" (src), "2" (n), "3" (retn));
  108. }
  109. while (n >= 16)
  110. {
  111. __asm_copy_to_user_16 (dst, src, retn);
  112. n -= 16;
  113. }
  114. /* Having a separate by-four loops cuts down on cache footprint.
  115. FIXME: Test with and without; increasing switch to be 0..15. */
  116. while (n >= 4)
  117. {
  118. __asm_copy_to_user_4 (dst, src, retn);
  119. n -= 4;
  120. }
  121. switch (n)
  122. {
  123. case 0:
  124. break;
  125. case 1:
  126. __asm_copy_to_user_1 (dst, src, retn);
  127. break;
  128. case 2:
  129. __asm_copy_to_user_2 (dst, src, retn);
  130. break;
  131. case 3:
  132. __asm_copy_to_user_3 (dst, src, retn);
  133. break;
  134. }
  135. return retn;
  136. }
  137. EXPORT_SYMBOL(__copy_user);
  138. /* Copy from user to kernel, zeroing the bytes that were inaccessible in
  139. userland. The return-value is the number of bytes that were
  140. inaccessible. */
  141. unsigned long __copy_user_zeroing(void *pdst, const void __user *psrc,
  142. unsigned long pn)
  143. {
  144. /* We want the parameters put in special registers.
  145. Make sure the compiler is able to make something useful of this.
  146. As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
  147. FIXME: Comment for old gcc version. Check.
  148. If gcc was alright, it really would need no temporaries, and no
  149. stack space to save stuff on. */
  150. register char *dst __asm__ ("r13") = pdst;
  151. register const char *src __asm__ ("r11") = psrc;
  152. register int n __asm__ ("r12") = pn;
  153. register int retn __asm__ ("r10") = 0;
  154. /* The best reason to align src is that we then know that a read-fault
  155. was for aligned bytes; there's no 1..3 remaining good bytes to
  156. pickle. */
  157. if (((unsigned long) src & 3) != 0)
  158. {
  159. if (((unsigned long) src & 1) && n != 0)
  160. {
  161. __asm_copy_from_user_1 (dst, src, retn);
  162. n--;
  163. }
  164. if (((unsigned long) src & 2) && n >= 2)
  165. {
  166. __asm_copy_from_user_2 (dst, src, retn);
  167. n -= 2;
  168. }
  169. /* We only need one check after the unalignment-adjustments, because
  170. if both adjustments were done, either both or neither reference
  171. had an exception. */
  172. if (retn != 0)
  173. goto copy_exception_bytes;
  174. }
  175. /* Movem is dirt cheap. The overheap is low enough to always use the
  176. minimum possible block size as the threshold. */
  177. if (n >= 44)
  178. {
  179. /* It is not optimal to tell the compiler about clobbering any
  180. registers; that will move the saving/restoring of those registers
  181. to the function prologue/epilogue, and make non-movem sizes
  182. suboptimal. */
  183. __asm__ volatile ("\
  184. .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
  185. .err \n\
  186. .endif \n\
  187. \n\
  188. ;; Save the registers we'll use in the movem process \n\
  189. ;; on the stack. \n\
  190. subq 11*4,$sp \n\
  191. movem $r10,[$sp] \n\
  192. \n\
  193. ;; Now we've got this: \n\
  194. ;; r11 - src \n\
  195. ;; r13 - dst \n\
  196. ;; r12 - n \n\
  197. \n\
  198. ;; Update n for the first loop \n\
  199. subq 44,$r12 \n\
  200. 0: \n\
  201. movem [$r11+],$r10 \n\
  202. \n\
  203. subq 44,$r12 \n\
  204. bge 0b \n\
  205. movem $r10,[$r13+] \n\
  206. \n\
  207. 4: \n\
  208. addq 44,$r12 ;; compensate for last loop underflowing n \n\
  209. \n\
  210. ;; Restore registers from stack \n\
  211. movem [$sp+],$r10 \n\
  212. .section .fixup,\"ax\" \n\
  213. \n\
  214. ;; Do not jump back into the loop if we fail. For some uses, we get a \n\
  215. ;; page fault somewhere on the line. Without checking for page limits, \n\
  216. ;; we don't know where, but we need to copy accurately and keep an \n\
  217. ;; accurate count; not just clear the whole line. To do that, we fall \n\
  218. ;; down in the code below, proceeding with smaller amounts. It should \n\
  219. ;; be kept in mind that we have to cater to code like what at one time \n\
  220. ;; was in fs/super.c: \n\
  221. ;; i = size - copy_from_user((void *)page, data, size); \n\
  222. ;; which would cause repeated faults while clearing the remainder of \n\
  223. ;; the SIZE bytes at PAGE after the first fault. \n\
  224. ;; A caveat here is that we must not fall through from a failing page \n\
  225. ;; to a valid page. \n\
  226. \n\
  227. 3: \n\
  228. jump 4b ;; Fall through, pretending the fault didn't happen. \n\
  229. nop \n\
  230. \n\
  231. .previous \n\
  232. .section __ex_table,\"a\" \n\
  233. .dword 0b,3b \n\
  234. .previous"
  235. /* Outputs */ : "=r" (dst), "=r" (src), "=r" (n), "=r" (retn)
  236. /* Inputs */ : "0" (dst), "1" (src), "2" (n), "3" (retn));
  237. }
  238. /* Either we directly start copying here, using dword copying in a loop,
  239. or we copy as much as possible with 'movem' and then the last block
  240. (<44 bytes) is copied here. This will work since 'movem' will have
  241. updated src, dst and n. (Except with failing src.)
  242. Since we want to keep src accurate, we can't use
  243. __asm_copy_from_user_N with N != (1, 2, 4); it updates dst and
  244. retn, but not src (by design; it's value is ignored elsewhere). */
  245. while (n >= 4)
  246. {
  247. __asm_copy_from_user_4 (dst, src, retn);
  248. n -= 4;
  249. if (retn)
  250. goto copy_exception_bytes;
  251. }
  252. /* If we get here, there were no memory read faults. */
  253. switch (n)
  254. {
  255. /* These copies are at least "naturally aligned" (so we don't have
  256. to check each byte), due to the src alignment code before the
  257. movem loop. The *_3 case *will* get the correct count for retn. */
  258. case 0:
  259. /* This case deliberately left in (if you have doubts check the
  260. generated assembly code). */
  261. break;
  262. case 1:
  263. __asm_copy_from_user_1 (dst, src, retn);
  264. break;
  265. case 2:
  266. __asm_copy_from_user_2 (dst, src, retn);
  267. break;
  268. case 3:
  269. __asm_copy_from_user_3 (dst, src, retn);
  270. break;
  271. }
  272. /* If we get here, retn correctly reflects the number of failing
  273. bytes. */
  274. return retn;
  275. copy_exception_bytes:
  276. /* We already have "retn" bytes cleared, and need to clear the
  277. remaining "n" bytes. A non-optimized simple byte-for-byte in-line
  278. memset is preferred here, since this isn't speed-critical code and
  279. we'd rather have this a leaf-function than calling memset. */
  280. {
  281. char *endp;
  282. for (endp = dst + n; dst < endp; dst++)
  283. *dst = 0;
  284. }
  285. return retn + n;
  286. }
  287. EXPORT_SYMBOL(__copy_user_zeroing);
  288. /* Zero userspace. */
  289. unsigned long __do_clear_user(void __user *pto, unsigned long pn)
  290. {
  291. /* We want the parameters put in special registers.
  292. Make sure the compiler is able to make something useful of this.
  293. As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
  294. FIXME: Comment for old gcc version. Check.
  295. If gcc was alright, it really would need no temporaries, and no
  296. stack space to save stuff on. */
  297. register char *dst __asm__ ("r13") = pto;
  298. register int n __asm__ ("r12") = pn;
  299. register int retn __asm__ ("r10") = 0;
  300. if (((unsigned long) dst & 3) != 0
  301. /* Don't align if we wouldn't copy more than a few bytes. */
  302. && n >= 3)
  303. {
  304. if ((unsigned long) dst & 1)
  305. {
  306. __asm_clear_1 (dst, retn);
  307. n--;
  308. }
  309. if ((unsigned long) dst & 2)
  310. {
  311. __asm_clear_2 (dst, retn);
  312. n -= 2;
  313. }
  314. }
  315. /* Decide which copying method to use.
  316. FIXME: This number is from the "ordinary" kernel memset. */
  317. if (n >= 48)
  318. {
  319. /* For large clears we use 'movem' */
  320. /* It is not optimal to tell the compiler about clobbering any
  321. call-saved registers; that will move the saving/restoring of
  322. those registers to the function prologue/epilogue, and make
  323. non-movem sizes suboptimal.
  324. This method is not foolproof; it assumes that the "asm reg"
  325. declarations at the beginning of the function really are used
  326. here (beware: they may be moved to temporary registers).
  327. This way, we do not have to save/move the registers around into
  328. temporaries; we can safely use them straight away.
  329. If you want to check that the allocation was right; then
  330. check the equalities in the first comment. It should say
  331. something like "r13=r13, r11=r11, r12=r12". */
  332. __asm__ volatile ("\
  333. .ifnc %0%1%2,$r13$r12$r10 \n\
  334. .err \n\
  335. .endif \n\
  336. \n\
  337. ;; Save the registers we'll clobber in the movem process \n\
  338. ;; on the stack. Don't mention them to gcc, it will only be \n\
  339. ;; upset. \n\
  340. subq 11*4,$sp \n\
  341. movem $r10,[$sp] \n\
  342. \n\
  343. clear.d $r0 \n\
  344. clear.d $r1 \n\
  345. clear.d $r2 \n\
  346. clear.d $r3 \n\
  347. clear.d $r4 \n\
  348. clear.d $r5 \n\
  349. clear.d $r6 \n\
  350. clear.d $r7 \n\
  351. clear.d $r8 \n\
  352. clear.d $r9 \n\
  353. clear.d $r10 \n\
  354. clear.d $r11 \n\
  355. \n\
  356. ;; Now we've got this: \n\
  357. ;; r13 - dst \n\
  358. ;; r12 - n \n\
  359. \n\
  360. ;; Update n for the first loop \n\
  361. subq 12*4,$r12 \n\
  362. 0: \n\
  363. subq 12*4,$r12 \n\
  364. 1: \n\
  365. bge 0b \n\
  366. movem $r11,[$r13+] \n\
  367. \n\
  368. addq 12*4,$r12 ;; compensate for last loop underflowing n \n\
  369. \n\
  370. ;; Restore registers from stack \n\
  371. movem [$sp+],$r10 \n\
  372. 2: \n\
  373. .section .fixup,\"ax\" \n\
  374. 3: \n\
  375. movem [$sp],$r10 \n\
  376. addq 12*4,$r10 \n\
  377. addq 12*4,$r13 \n\
  378. movem $r10,[$sp] \n\
  379. jump 0b \n\
  380. clear.d $r10 \n\
  381. \n\
  382. .previous \n\
  383. .section __ex_table,\"a\" \n\
  384. .dword 1b,3b \n\
  385. .previous"
  386. /* Outputs */ : "=r" (dst), "=r" (n), "=r" (retn)
  387. /* Inputs */ : "0" (dst), "1" (n), "2" (retn)
  388. /* Clobber */ : "r11");
  389. }
  390. while (n >= 16)
  391. {
  392. __asm_clear_16 (dst, retn);
  393. n -= 16;
  394. }
  395. /* Having a separate by-four loops cuts down on cache footprint.
  396. FIXME: Test with and without; increasing switch to be 0..15. */
  397. while (n >= 4)
  398. {
  399. __asm_clear_4 (dst, retn);
  400. n -= 4;
  401. }
  402. switch (n)
  403. {
  404. case 0:
  405. break;
  406. case 1:
  407. __asm_clear_1 (dst, retn);
  408. break;
  409. case 2:
  410. __asm_clear_2 (dst, retn);
  411. break;
  412. case 3:
  413. __asm_clear_3 (dst, retn);
  414. break;
  415. }
  416. return retn;
  417. }
  418. EXPORT_SYMBOL(__do_clear_user);