lusercopy.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. .proc
  59. .callinfo NO_CALLS
  60. .entry
  61. comib,=,n 0,%r25,$lclu_done
  62. get_sr
  63. $lclu_loop:
  64. addib,<> -1,%r25,$lclu_loop
  65. 1: stbs,ma %r0,1(%sr1,%r26)
  66. $lclu_done:
  67. bv %r0(%r2)
  68. copy %r25,%r28
  69. 2: b $lclu_done
  70. ldo 1(%r25),%r25
  71. ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
  72. .exit
  73. ENDPROC_CFI(lclear_user)
  74. .procend
  75. /*
  76. * long lstrnlen_user(char *s, long n)
  77. *
  78. * Returns 0 if exception before zero byte or reaching N,
  79. * N+1 if N would be exceeded,
  80. * else strlen + 1 (i.e. includes zero byte).
  81. */
  82. ENTRY_CFI(lstrnlen_user)
  83. .proc
  84. .callinfo NO_CALLS
  85. .entry
  86. comib,= 0,%r25,$lslen_nzero
  87. copy %r26,%r24
  88. get_sr
  89. 1: ldbs,ma 1(%sr1,%r26),%r1
  90. $lslen_loop:
  91. comib,=,n 0,%r1,$lslen_done
  92. addib,<> -1,%r25,$lslen_loop
  93. 2: ldbs,ma 1(%sr1,%r26),%r1
  94. $lslen_done:
  95. bv %r0(%r2)
  96. sub %r26,%r24,%r28
  97. .exit
  98. $lslen_nzero:
  99. b $lslen_done
  100. ldo 1(%r26),%r26 /* special case for N == 0 */
  101. 3: b $lslen_done
  102. copy %r24,%r26 /* reset r26 so 0 is returned on fault */
  103. ASM_EXCEPTIONTABLE_ENTRY(1b,3b)
  104. ASM_EXCEPTIONTABLE_ENTRY(2b,3b)
  105. ENDPROC_CFI(lstrnlen_user)
  106. .procend
  107. /*
  108. * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
  109. *
  110. * Inputs:
  111. * - sr1 already contains space of source region
  112. * - sr2 already contains space of destination region
  113. *
  114. * Returns:
  115. * - number of bytes that could not be copied.
  116. * On success, this will be zero.
  117. *
  118. * This code is based on a C-implementation of a copy routine written by
  119. * Randolph Chung, which in turn was derived from the glibc.
  120. *
  121. * Several strategies are tried to try to get the best performance for various
  122. * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
  123. * at a time using general registers. Unaligned copies are handled either by
  124. * aligning the destination and then using shift-and-write method, or in a few
  125. * cases by falling back to a byte-at-a-time copy.
  126. *
  127. * Testing with various alignments and buffer sizes shows that this code is
  128. * often >10x faster than a simple byte-at-a-time copy, even for strangely
  129. * aligned operands. It is interesting to note that the glibc version of memcpy
  130. * (written in C) is actually quite fast already. This routine is able to beat
  131. * it by 30-40% for aligned copies because of the loop unrolling, but in some
  132. * cases the glibc version is still slightly faster. This lends more
  133. * credibility that gcc can generate very good code as long as we are careful.
  134. *
  135. * Possible optimizations:
  136. * - add cache prefetching
  137. * - try not to use the post-increment address modifiers; they may create
  138. * additional interlocks. Assumption is that those were only efficient on old
  139. * machines (pre PA8000 processors)
  140. */
  141. dst = arg0
  142. src = arg1
  143. len = arg2
  144. end = arg3
  145. t1 = r19
  146. t2 = r20
  147. t3 = r21
  148. t4 = r22
  149. srcspc = sr1
  150. dstspc = sr2
  151. t0 = r1
  152. a1 = t1
  153. a2 = t2
  154. a3 = t3
  155. a0 = t4
  156. save_src = ret0
  157. save_dst = ret1
  158. save_len = r31
  159. ENTRY_CFI(pa_memcpy)
  160. .proc
  161. .callinfo NO_CALLS
  162. .entry
  163. /* Last destination address */
  164. add dst,len,end
  165. /* short copy with less than 16 bytes? */
  166. cmpib,COND(>>=),n 15,len,.Lbyte_loop
  167. /* same alignment? */
  168. xor src,dst,t0
  169. extru t0,31,2,t1
  170. cmpib,<>,n 0,t1,.Lunaligned_copy
  171. #ifdef CONFIG_64BIT
  172. /* only do 64-bit copies if we can get aligned. */
  173. extru t0,31,3,t1
  174. cmpib,<>,n 0,t1,.Lalign_loop32
  175. /* loop until we are 64-bit aligned */
  176. .Lalign_loop64:
  177. extru dst,31,3,t1
  178. cmpib,=,n 0,t1,.Lcopy_loop_16_start
  179. 20: ldb,ma 1(srcspc,src),t1
  180. 21: stb,ma t1,1(dstspc,dst)
  181. b .Lalign_loop64
  182. ldo -1(len),len
  183. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  184. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  185. .Lcopy_loop_16_start:
  186. ldi 31,t0
  187. .Lcopy_loop_16:
  188. cmpb,COND(>>=),n t0,len,.Lword_loop
  189. 10: ldd 0(srcspc,src),t1
  190. 11: ldd 8(srcspc,src),t2
  191. ldo 16(src),src
  192. 12: std,ma t1,8(dstspc,dst)
  193. 13: std,ma t2,8(dstspc,dst)
  194. 14: ldd 0(srcspc,src),t1
  195. 15: ldd 8(srcspc,src),t2
  196. ldo 16(src),src
  197. 16: std,ma t1,8(dstspc,dst)
  198. 17: std,ma t2,8(dstspc,dst)
  199. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  200. ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
  201. ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
  202. ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
  203. ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
  204. ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
  205. ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
  206. ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
  207. b .Lcopy_loop_16
  208. ldo -32(len),len
  209. .Lword_loop:
  210. cmpib,COND(>>=),n 3,len,.Lbyte_loop
  211. 20: ldw,ma 4(srcspc,src),t1
  212. 21: stw,ma t1,4(dstspc,dst)
  213. b .Lword_loop
  214. ldo -4(len),len
  215. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  216. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  217. #endif /* CONFIG_64BIT */
  218. /* loop until we are 32-bit aligned */
  219. .Lalign_loop32:
  220. extru dst,31,2,t1
  221. cmpib,=,n 0,t1,.Lcopy_loop_8
  222. 20: ldb,ma 1(srcspc,src),t1
  223. 21: stb,ma t1,1(dstspc,dst)
  224. b .Lalign_loop32
  225. ldo -1(len),len
  226. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  227. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  228. .Lcopy_loop_8:
  229. cmpib,COND(>>=),n 15,len,.Lbyte_loop
  230. 10: ldw 0(srcspc,src),t1
  231. 11: ldw 4(srcspc,src),t2
  232. 12: stw,ma t1,4(dstspc,dst)
  233. 13: stw,ma t2,4(dstspc,dst)
  234. 14: ldw 8(srcspc,src),t1
  235. 15: ldw 12(srcspc,src),t2
  236. ldo 16(src),src
  237. 16: stw,ma t1,4(dstspc,dst)
  238. 17: stw,ma t2,4(dstspc,dst)
  239. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  240. ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
  241. ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
  242. ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
  243. ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
  244. ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
  245. ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
  246. ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
  247. b .Lcopy_loop_8
  248. ldo -16(len),len
  249. .Lbyte_loop:
  250. cmpclr,COND(<>) len,%r0,%r0
  251. b,n .Lcopy_done
  252. 20: ldb 0(srcspc,src),t1
  253. ldo 1(src),src
  254. 21: stb,ma t1,1(dstspc,dst)
  255. b .Lbyte_loop
  256. ldo -1(len),len
  257. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  258. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  259. .Lcopy_done:
  260. bv %r0(%r2)
  261. sub end,dst,ret0
  262. /* src and dst are not aligned the same way. */
  263. /* need to go the hard way */
  264. .Lunaligned_copy:
  265. /* align until dst is 32bit-word-aligned */
  266. extru dst,31,2,t1
  267. cmpib,=,n 0,t1,.Lcopy_dstaligned
  268. 20: ldb 0(srcspc,src),t1
  269. ldo 1(src),src
  270. 21: stb,ma t1,1(dstspc,dst)
  271. b .Lunaligned_copy
  272. ldo -1(len),len
  273. ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
  274. ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
  275. .Lcopy_dstaligned:
  276. /* store src, dst and len in safe place */
  277. copy src,save_src
  278. copy dst,save_dst
  279. copy len,save_len
  280. /* len now needs give number of words to copy */
  281. SHRREG len,2,len
  282. /*
  283. * Copy from a not-aligned src to an aligned dst using shifts.
  284. * Handles 4 words per loop.
  285. */
  286. depw,z src,28,2,t0
  287. subi 32,t0,t0
  288. mtsar t0
  289. extru len,31,2,t0
  290. cmpib,= 2,t0,.Lcase2
  291. /* Make src aligned by rounding it down. */
  292. depi 0,31,2,src
  293. cmpiclr,<> 3,t0,%r0
  294. b,n .Lcase3
  295. cmpiclr,<> 1,t0,%r0
  296. b,n .Lcase1
  297. .Lcase0:
  298. cmpb,COND(=) %r0,len,.Lcda_finish
  299. nop
  300. 1: ldw,ma 4(srcspc,src), a3
  301. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  302. 1: ldw,ma 4(srcspc,src), a0
  303. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  304. b,n .Ldo3
  305. .Lcase1:
  306. 1: ldw,ma 4(srcspc,src), a2
  307. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  308. 1: ldw,ma 4(srcspc,src), a3
  309. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  310. ldo -1(len),len
  311. cmpb,COND(=),n %r0,len,.Ldo0
  312. .Ldo4:
  313. 1: ldw,ma 4(srcspc,src), a0
  314. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  315. shrpw a2, a3, %sar, t0
  316. 1: stw,ma t0, 4(dstspc,dst)
  317. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  318. .Ldo3:
  319. 1: ldw,ma 4(srcspc,src), a1
  320. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  321. shrpw a3, a0, %sar, t0
  322. 1: stw,ma t0, 4(dstspc,dst)
  323. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  324. .Ldo2:
  325. 1: ldw,ma 4(srcspc,src), a2
  326. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  327. shrpw a0, a1, %sar, t0
  328. 1: stw,ma t0, 4(dstspc,dst)
  329. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  330. .Ldo1:
  331. 1: ldw,ma 4(srcspc,src), a3
  332. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  333. shrpw a1, a2, %sar, t0
  334. 1: stw,ma t0, 4(dstspc,dst)
  335. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  336. ldo -4(len),len
  337. cmpb,COND(<>) %r0,len,.Ldo4
  338. nop
  339. .Ldo0:
  340. shrpw a2, a3, %sar, t0
  341. 1: stw,ma t0, 4(dstspc,dst)
  342. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
  343. .Lcda_rdfault:
  344. .Lcda_finish:
  345. /* calculate new src, dst and len and jump to byte-copy loop */
  346. sub dst,save_dst,t0
  347. add save_src,t0,src
  348. b .Lbyte_loop
  349. sub save_len,t0,len
  350. .Lcase3:
  351. 1: ldw,ma 4(srcspc,src), a0
  352. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  353. 1: ldw,ma 4(srcspc,src), a1
  354. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  355. b .Ldo2
  356. ldo 1(len),len
  357. .Lcase2:
  358. 1: ldw,ma 4(srcspc,src), a1
  359. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  360. 1: ldw,ma 4(srcspc,src), a2
  361. ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
  362. b .Ldo1
  363. ldo 2(len),len
  364. /* fault exception fixup handlers: */
  365. #ifdef CONFIG_64BIT
  366. .Lcopy16_fault:
  367. b .Lcopy_done
  368. 10: std,ma t1,8(dstspc,dst)
  369. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  370. #endif
  371. .Lcopy8_fault:
  372. b .Lcopy_done
  373. 10: stw,ma t1,4(dstspc,dst)
  374. ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
  375. .exit
  376. ENDPROC_CFI(pa_memcpy)
  377. .procend
  378. .end