lusercopy.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * User Space Access Routines
  3. *
  4. * Copyright (C) 2000-2002 Hewlett-Packard (John Marvin)
  5. * Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
  6. * Copyright (C) 2001 Matthieu Delahaye <delahaym at esiee.fr>
  7. * Copyright (C) 2003 Randolph Chung <tausq with parisc-linux.org>
  8. * Copyright (C) 2017 Helge Deller <deller@gmx.de>
  9. * Copyright (C) 2017 John David Anglin <dave.anglin@bell.net>
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. /*
  27. * These routines still have plenty of room for optimization
  28. * (word & doubleword load/store, dual issue, store hints, etc.).
  29. */
  30. /*
  31. * The following routines assume that space register 3 (sr3) contains
  32. * the space id associated with the current users address space.
  33. */
  34. .text
  35. #include <asm/assembly.h>
  36. #include <asm/errno.h>
  37. #include <linux/linkage.h>
  38. /*
  39. * get_sr gets the appropriate space value into
  40. * sr1 for kernel/user space access, depending
  41. * on the flag stored in the task structure.
  42. */
  43. .macro get_sr
  44. mfctl %cr30,%r1
  45. ldw TI_SEGMENT(%r1),%r22
  46. mfsp %sr3,%r1
  47. or,<> %r22,%r0,%r0
  48. copy %r0,%r1
  49. mtsp %r1,%sr1
  50. .endm
  51. /*
  52. * unsigned long lclear_user(void *to, unsigned long n)
  53. *
  54. * Returns 0 for success.
  55. * otherwise, returns number of bytes not transferred.
  56. */
  57. ENTRY_CFI(lclear_user)
  58. comib,=,n 0,%r25,$lclu_done
  59. get_sr
  60. $lclu_loop:
  61. addib,<> -1,%r25,$lclu_loop
  62. 1: stbs,ma %r0,1(%sr1,%r26)
  63. $lclu_done:
  64. bv %r0(%r2)
  65. copy %r25,%r28
  66. 2: b $lclu_done
  67. ldo 1(%r25),%r25
  68. ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
  69. ENDPROC_CFI(lclear_user)
  70. /*
  71. * long lstrnlen_user(char *s, long n)
  72. *
  73. * Returns 0 if exception before zero byte or reaching N,
  74. * N+1 if N would be exceeded,
  75. * else strlen + 1 (i.e. includes zero byte).
  76. */
  77. ENTRY_CFI(lstrnlen_user)
  78. comib,= 0,%r25,$lslen_nzero
  79. copy %r26,%r24
  80. get_sr
  81. 1: ldbs,ma 1(%sr1,%r26),%r1
  82. $lslen_loop:
  83. comib,=,n 0,%r1,$lslen_done
  84. addib,<> -1,%r25,$lslen_loop
  85. 2: ldbs,ma 1(%sr1,%r26),%r1
  86. $lslen_done:
  87. bv %r0(%r2)
  88. sub %r26,%r24,%r28
  89. $lslen_nzero:
  90. b $lslen_done
  91. ldo 1(%r26),%r26 /* special case for N == 0 */
  92. 3: b $lslen_done
  93. copy %r24,%r26 /* reset r26 so 0 is returned on fault */
  94. ASM_EXCEPTIONTABLE_ENTRY(1b,3b)
  95. ASM_EXCEPTIONTABLE_ENTRY(2b,3b)
  96. ENDPROC_CFI(lstrnlen_user)
  97. /*
  98. * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
  99. *
  100. * Inputs:
  101. * - sr1 already contains space of source region
  102. * - sr2 already contains space of destination region
  103. *
  104. * Returns:
  105. * - number of bytes that could not be copied.
  106. * On success, this will be zero.
  107. *
  108. * This code is based on a C-implementation of a copy routine written by
  109. * Randolph Chung, which in turn was derived from the glibc.
  110. *
  111. * Several strategies are tried to try to get the best performance for various
  112. * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
  113. * at a time using general registers. Unaligned copies are handled either by
  114. * aligning the destination and then using shift-and-write method, or in a few
  115. * cases by falling back to a byte-at-a-time copy.
  116. *
  117. * Testing with various alignments and buffer sizes shows that this code is
  118. * often >10x faster than a simple byte-at-a-time copy, even for strangely
  119. * aligned operands. It is interesting to note that the glibc version of memcpy
  120. * (written in C) is actually quite fast already. This routine is able to beat
  121. * it by 30-40% for aligned copies because of the loop unrolling, but in some
  122. * cases the glibc version is still slightly faster. This lends more
  123. * credibility that gcc can generate very good code as long as we are careful.
  124. *
  125. * Possible optimizations:
  126. * - add cache prefetching
  127. * - try not to use the post-increment address modifiers; they may create
  128. * additional interlocks. Assumption is that those were only efficient on old
  129. * machines (pre PA8000 processors)
  130. */
  131. dst = arg0
  132. src = arg1
  133. len = arg2
  134. end = arg3
  135. t1 = r19
  136. t2 = r20
  137. t3 = r21
  138. t4 = r22
  139. srcspc = sr1
  140. dstspc = sr2
  141. t0 = r1
  142. a1 = t1
  143. a2 = t2
  144. a3 = t3
  145. a0 = t4
  146. save_src = ret0
  147. save_dst = ret1
  148. save_len = r31
  149. ENTRY_CFI(pa_memcpy)
  150. /* Last destination address */
  151. add dst,len,end
  152. /* short copy with less than 16 bytes? */
  153. cmpib,COND(>>=),n 15,len,.Lbyte_loop
  154. /* same alignment? */
  155. xor src,dst,t0
  156. extru t0,31,2,t1
  157. cmpib,<>,n 0,t1,.Lunaligned_copy
  158. #ifdef CONFIG_64BIT
  159. /* only do 64-bit copies if we can get aligned. */
  160. extru t0,31,3,t1
  161. cmpib,<>,n 0,t1,.Lalign_loop32
  162. /* loop until we are 64-bit aligned */
  163. .Lalign_loop64:
  164. extru dst,31,3,t1
  165. cmpib,=,n 0,t1,.Lcopy_loop_16_start
  166. 20: ldb,ma 1(srcspc,src),t1
  167. 21: stb,ma t1,1(dstspc,dst)
  168. b .Lalign_loop64
  169. ldo -1(len),len
  170. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  171. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  172. .Lcopy_loop_16_start:
  173. ldi 31,t0
  174. .Lcopy_loop_16:
  175. cmpb,COND(>>=),n t0,len,.Lword_loop
  176. 10: ldd 0(srcspc,src),t1
  177. 11: ldd 8(srcspc,src),t2
  178. ldo 16(src),src
  179. 12: std,ma t1,8(dstspc,dst)
  180. 13: std,ma t2,8(dstspc,dst)
  181. 14: ldd 0(srcspc,src),t1
  182. 15: ldd 8(srcspc,src),t2
  183. ldo 16(src),src
  184. 16: std,ma t1,8(dstspc,dst)
  185. 17: std,ma t2,8(dstspc,dst)
  186. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  187. ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
  188. ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
  189. ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
  190. ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
  191. ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
  192. ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
  193. ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
  194. b .Lcopy_loop_16
  195. ldo -32(len),len
  196. .Lword_loop:
  197. cmpib,COND(>>=),n 3,len,.Lbyte_loop
  198. 20: ldw,ma 4(srcspc,src),t1
  199. 21: stw,ma t1,4(dstspc,dst)
  200. b .Lword_loop
  201. ldo -4(len),len
  202. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  203. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  204. #endif /* CONFIG_64BIT */
  205. /* loop until we are 32-bit aligned */
  206. .Lalign_loop32:
  207. extru dst,31,2,t1
  208. cmpib,=,n 0,t1,.Lcopy_loop_8
  209. 20: ldb,ma 1(srcspc,src),t1
  210. 21: stb,ma t1,1(dstspc,dst)
  211. b .Lalign_loop32
  212. ldo -1(len),len
  213. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  214. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  215. .Lcopy_loop_8:
  216. cmpib,COND(>>=),n 15,len,.Lbyte_loop
  217. 10: ldw 0(srcspc,src),t1
  218. 11: ldw 4(srcspc,src),t2
  219. 12: stw,ma t1,4(dstspc,dst)
  220. 13: stw,ma t2,4(dstspc,dst)
  221. 14: ldw 8(srcspc,src),t1
  222. 15: ldw 12(srcspc,src),t2
  223. ldo 16(src),src
  224. 16: stw,ma t1,4(dstspc,dst)
  225. 17: stw,ma t2,4(dstspc,dst)
  226. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  227. ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
  228. ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
  229. ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
  230. ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
  231. ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
  232. ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
  233. ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
  234. b .Lcopy_loop_8
  235. ldo -16(len),len
  236. .Lbyte_loop:
  237. cmpclr,COND(<>) len,%r0,%r0
  238. b,n .Lcopy_done
  239. 20: ldb 0(srcspc,src),t1
  240. ldo 1(src),src
  241. 21: stb,ma t1,1(dstspc,dst)
  242. b .Lbyte_loop
  243. ldo -1(len),len
  244. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  245. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  246. .Lcopy_done:
  247. bv %r0(%r2)
  248. sub end,dst,ret0
  249. /* src and dst are not aligned the same way. */
  250. /* need to go the hard way */
  251. .Lunaligned_copy:
  252. /* align until dst is 32bit-word-aligned */
  253. extru dst,31,2,t1
  254. cmpib,=,n 0,t1,.Lcopy_dstaligned
  255. 20: ldb 0(srcspc,src),t1
  256. ldo 1(src),src
  257. 21: stb,ma t1,1(dstspc,dst)
  258. b .Lunaligned_copy
  259. ldo -1(len),len
  260. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  261. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  262. .Lcopy_dstaligned:
  263. /* store src, dst and len in safe place */
  264. copy src,save_src
  265. copy dst,save_dst
  266. copy len,save_len
  267. /* len now needs give number of words to copy */
  268. SHRREG len,2,len
  269. /*
  270. * Copy from a not-aligned src to an aligned dst using shifts.
  271. * Handles 4 words per loop.
  272. */
  273. depw,z src,28,2,t0
  274. subi 32,t0,t0
  275. mtsar t0
  276. extru len,31,2,t0
  277. cmpib,= 2,t0,.Lcase2
  278. /* Make src aligned by rounding it down. */
  279. depi 0,31,2,src
  280. cmpiclr,<> 3,t0,%r0
  281. b,n .Lcase3
  282. cmpiclr,<> 1,t0,%r0
  283. b,n .Lcase1
  284. .Lcase0:
  285. cmpb,COND(=) %r0,len,.Lcda_finish
  286. nop
  287. 1: ldw,ma 4(srcspc,src), a3
  288. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  289. 1: ldw,ma 4(srcspc,src), a0
  290. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  291. b,n .Ldo3
  292. .Lcase1:
  293. 1: ldw,ma 4(srcspc,src), a2
  294. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  295. 1: ldw,ma 4(srcspc,src), a3
  296. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  297. ldo -1(len),len
  298. cmpb,COND(=),n %r0,len,.Ldo0
  299. .Ldo4:
  300. 1: ldw,ma 4(srcspc,src), a0
  301. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  302. shrpw a2, a3, %sar, t0
  303. 1: stw,ma t0, 4(dstspc,dst)
  304. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  305. .Ldo3:
  306. 1: ldw,ma 4(srcspc,src), a1
  307. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  308. shrpw a3, a0, %sar, t0
  309. 1: stw,ma t0, 4(dstspc,dst)
  310. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  311. .Ldo2:
  312. 1: ldw,ma 4(srcspc,src), a2
  313. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  314. shrpw a0, a1, %sar, t0
  315. 1: stw,ma t0, 4(dstspc,dst)
  316. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  317. .Ldo1:
  318. 1: ldw,ma 4(srcspc,src), a3
  319. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  320. shrpw a1, a2, %sar, t0
  321. 1: stw,ma t0, 4(dstspc,dst)
  322. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  323. ldo -4(len),len
  324. cmpb,COND(<>) %r0,len,.Ldo4
  325. nop
  326. .Ldo0:
  327. shrpw a2, a3, %sar, t0
  328. 1: stw,ma t0, 4(dstspc,dst)
  329. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  330. .Lcda_rdfault:
  331. .Lcda_finish:
  332. /* calculate new src, dst and len and jump to byte-copy loop */
  333. sub dst,save_dst,t0
  334. add save_src,t0,src
  335. b .Lbyte_loop
  336. sub save_len,t0,len
  337. .Lcase3:
  338. 1: ldw,ma 4(srcspc,src), a0
  339. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  340. 1: ldw,ma 4(srcspc,src), a1
  341. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  342. b .Ldo2
  343. ldo 1(len),len
  344. .Lcase2:
  345. 1: ldw,ma 4(srcspc,src), a1
  346. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  347. 1: ldw,ma 4(srcspc,src), a2
  348. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  349. b .Ldo1
  350. ldo 2(len),len
  351. /* fault exception fixup handlers: */
  352. #ifdef CONFIG_64BIT
  353. .Lcopy16_fault:
  354. b .Lcopy_done
  355. 10: std,ma t1,8(dstspc,dst)
  356. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  357. #endif
  358. .Lcopy8_fault:
  359. b .Lcopy_done
  360. 10: stw,ma t1,4(dstspc,dst)
  361. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  362. ENDPROC_CFI(pa_memcpy)
  363. .end