usercopy.c 14 KB

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