Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Itanium 2-optimized version of memcpy and copy_user function |
| 3 | * |
| 4 | * Inputs: |
| 5 | * in0: destination address |
| 6 | * in1: source address |
| 7 | * in2: number of bytes to copy |
| 8 | * Output: |
Chen, Kenneth W | 3e6e155 | 2006-05-03 11:53:43 -0700 | [diff] [blame] | 9 | * for memcpy: return dest |
| 10 | * for copy_user: return 0 if success, |
| 11 | * or number of byte NOT copied if error occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 13 | * Copyright (C) 2002 Intel Corp. |
| 14 | * Copyright (C) 2002 Ken Chen <kenneth.w.chen@intel.com> |
| 15 | */ |
| 16 | #include <linux/config.h> |
| 17 | #include <asm/asmmacro.h> |
| 18 | #include <asm/page.h> |
| 19 | |
| 20 | #define EK(y...) EX(y) |
| 21 | |
| 22 | /* McKinley specific optimization */ |
| 23 | |
| 24 | #define retval r8 |
| 25 | #define saved_pfs r31 |
| 26 | #define saved_lc r10 |
| 27 | #define saved_pr r11 |
| 28 | #define saved_in0 r14 |
| 29 | #define saved_in1 r15 |
| 30 | #define saved_in2 r16 |
| 31 | |
| 32 | #define src0 r2 |
| 33 | #define src1 r3 |
| 34 | #define dst0 r17 |
| 35 | #define dst1 r18 |
| 36 | #define cnt r9 |
| 37 | |
| 38 | /* r19-r30 are temp for each code section */ |
| 39 | #define PREFETCH_DIST 8 |
| 40 | #define src_pre_mem r19 |
| 41 | #define dst_pre_mem r20 |
| 42 | #define src_pre_l2 r21 |
| 43 | #define dst_pre_l2 r22 |
| 44 | #define t1 r23 |
| 45 | #define t2 r24 |
| 46 | #define t3 r25 |
| 47 | #define t4 r26 |
| 48 | #define t5 t1 // alias! |
| 49 | #define t6 t2 // alias! |
| 50 | #define t7 t3 // alias! |
| 51 | #define n8 r27 |
| 52 | #define t9 t5 // alias! |
| 53 | #define t10 t4 // alias! |
| 54 | #define t11 t7 // alias! |
| 55 | #define t12 t6 // alias! |
| 56 | #define t14 t10 // alias! |
| 57 | #define t13 r28 |
| 58 | #define t15 r29 |
| 59 | #define tmp r30 |
| 60 | |
| 61 | /* defines for long_copy block */ |
| 62 | #define A 0 |
| 63 | #define B (PREFETCH_DIST) |
| 64 | #define C (B + PREFETCH_DIST) |
| 65 | #define D (C + 1) |
| 66 | #define N (D + 1) |
| 67 | #define Nrot ((N + 7) & ~7) |
| 68 | |
| 69 | /* alias */ |
| 70 | #define in0 r32 |
| 71 | #define in1 r33 |
| 72 | #define in2 r34 |
| 73 | |
| 74 | GLOBAL_ENTRY(memcpy) |
| 75 | and r28=0x7,in0 |
| 76 | and r29=0x7,in1 |
| 77 | mov f6=f0 |
Chen, Kenneth W | 3e6e155 | 2006-05-03 11:53:43 -0700 | [diff] [blame] | 78 | mov retval=in0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | br.cond.sptk .common_code |
| 80 | ;; |
Andreas Schwab | 512f642 | 2005-04-26 23:00:00 -0700 | [diff] [blame] | 81 | END(memcpy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | GLOBAL_ENTRY(__copy_user) |
| 83 | .prologue |
| 84 | // check dest alignment |
| 85 | and r28=0x7,in0 |
| 86 | and r29=0x7,in1 |
| 87 | mov f6=f1 |
| 88 | mov saved_in0=in0 // save dest pointer |
| 89 | mov saved_in1=in1 // save src pointer |
Chen, Kenneth W | 3e6e155 | 2006-05-03 11:53:43 -0700 | [diff] [blame] | 90 | mov retval=r0 // initialize return value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | ;; |
| 92 | .common_code: |
| 93 | cmp.gt p15,p0=8,in2 // check for small size |
| 94 | cmp.ne p13,p0=0,r28 // check dest alignment |
| 95 | cmp.ne p14,p0=0,r29 // check src alignment |
| 96 | add src0=0,in1 |
| 97 | sub r30=8,r28 // for .align_dest |
Chen, Kenneth W | 3e6e155 | 2006-05-03 11:53:43 -0700 | [diff] [blame] | 98 | mov saved_in2=in2 // save len |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | ;; |
| 100 | add dst0=0,in0 |
| 101 | add dst1=1,in0 // dest odd index |
| 102 | cmp.le p6,p0 = 1,r30 // for .align_dest |
| 103 | (p15) br.cond.dpnt .memcpy_short |
| 104 | (p13) br.cond.dpnt .align_dest |
| 105 | (p14) br.cond.dpnt .unaligned_src |
| 106 | ;; |
| 107 | |
| 108 | // both dest and src are aligned on 8-byte boundary |
| 109 | .aligned_src: |
| 110 | .save ar.pfs, saved_pfs |
| 111 | alloc saved_pfs=ar.pfs,3,Nrot-3,0,Nrot |
| 112 | .save pr, saved_pr |
| 113 | mov saved_pr=pr |
| 114 | |
| 115 | shr.u cnt=in2,7 // this much cache line |
| 116 | ;; |
| 117 | cmp.lt p6,p0=2*PREFETCH_DIST,cnt |
| 118 | cmp.lt p7,p8=1,cnt |
| 119 | .save ar.lc, saved_lc |
| 120 | mov saved_lc=ar.lc |
| 121 | .body |
| 122 | add cnt=-1,cnt |
| 123 | add src_pre_mem=0,in1 // prefetch src pointer |
| 124 | add dst_pre_mem=0,in0 // prefetch dest pointer |
| 125 | ;; |
| 126 | (p7) mov ar.lc=cnt // prefetch count |
| 127 | (p8) mov ar.lc=r0 |
| 128 | (p6) br.cond.dpnt .long_copy |
| 129 | ;; |
| 130 | |
| 131 | .prefetch: |
| 132 | lfetch.fault [src_pre_mem], 128 |
| 133 | lfetch.fault.excl [dst_pre_mem], 128 |
| 134 | br.cloop.dptk.few .prefetch |
| 135 | ;; |
| 136 | |
| 137 | .medium_copy: |
| 138 | and tmp=31,in2 // copy length after iteration |
| 139 | shr.u r29=in2,5 // number of 32-byte iteration |
| 140 | add dst1=8,dst0 // 2nd dest pointer |
| 141 | ;; |
| 142 | add cnt=-1,r29 // ctop iteration adjustment |
| 143 | cmp.eq p10,p0=r29,r0 // do we really need to loop? |
| 144 | add src1=8,src0 // 2nd src pointer |
| 145 | cmp.le p6,p0=8,tmp |
| 146 | ;; |
| 147 | cmp.le p7,p0=16,tmp |
| 148 | mov ar.lc=cnt // loop setup |
| 149 | cmp.eq p16,p17 = r0,r0 |
| 150 | mov ar.ec=2 |
| 151 | (p10) br.dpnt.few .aligned_src_tail |
| 152 | ;; |
| 153 | TEXT_ALIGN(32) |
| 154 | 1: |
| 155 | EX(.ex_handler, (p16) ld8 r34=[src0],16) |
| 156 | EK(.ex_handler, (p16) ld8 r38=[src1],16) |
| 157 | EX(.ex_handler, (p17) st8 [dst0]=r33,16) |
| 158 | EK(.ex_handler, (p17) st8 [dst1]=r37,16) |
| 159 | ;; |
| 160 | EX(.ex_handler, (p16) ld8 r32=[src0],16) |
| 161 | EK(.ex_handler, (p16) ld8 r36=[src1],16) |
| 162 | EX(.ex_handler, (p16) st8 [dst0]=r34,16) |
| 163 | EK(.ex_handler, (p16) st8 [dst1]=r38,16) |
| 164 | br.ctop.dptk.few 1b |
| 165 | ;; |
| 166 | |
| 167 | .aligned_src_tail: |
| 168 | EX(.ex_handler, (p6) ld8 t1=[src0]) |
| 169 | mov ar.lc=saved_lc |
| 170 | mov ar.pfs=saved_pfs |
| 171 | EX(.ex_hndlr_s, (p7) ld8 t2=[src1],8) |
| 172 | cmp.le p8,p0=24,tmp |
| 173 | and r21=-8,tmp |
| 174 | ;; |
| 175 | EX(.ex_hndlr_s, (p8) ld8 t3=[src1]) |
| 176 | EX(.ex_handler, (p6) st8 [dst0]=t1) // store byte 1 |
| 177 | and in2=7,tmp // remaining length |
| 178 | EX(.ex_hndlr_d, (p7) st8 [dst1]=t2,8) // store byte 2 |
| 179 | add src0=src0,r21 // setting up src pointer |
| 180 | add dst0=dst0,r21 // setting up dest pointer |
| 181 | ;; |
| 182 | EX(.ex_handler, (p8) st8 [dst1]=t3) // store byte 3 |
| 183 | mov pr=saved_pr,-1 |
| 184 | br.dptk.many .memcpy_short |
| 185 | ;; |
| 186 | |
| 187 | /* code taken from copy_page_mck */ |
| 188 | .long_copy: |
| 189 | .rotr v[2*PREFETCH_DIST] |
| 190 | .rotp p[N] |
| 191 | |
| 192 | mov src_pre_mem = src0 |
| 193 | mov pr.rot = 0x10000 |
| 194 | mov ar.ec = 1 // special unrolled loop |
| 195 | |
| 196 | mov dst_pre_mem = dst0 |
| 197 | |
| 198 | add src_pre_l2 = 8*8, src0 |
| 199 | add dst_pre_l2 = 8*8, dst0 |
| 200 | ;; |
| 201 | add src0 = 8, src_pre_mem // first t1 src |
| 202 | mov ar.lc = 2*PREFETCH_DIST - 1 |
| 203 | shr.u cnt=in2,7 // number of lines |
| 204 | add src1 = 3*8, src_pre_mem // first t3 src |
| 205 | add dst0 = 8, dst_pre_mem // first t1 dst |
| 206 | add dst1 = 3*8, dst_pre_mem // first t3 dst |
| 207 | ;; |
| 208 | and tmp=127,in2 // remaining bytes after this block |
| 209 | add cnt = -(2*PREFETCH_DIST) - 1, cnt |
| 210 | // same as .line_copy loop, but with all predicated-off instructions removed: |
| 211 | .prefetch_loop: |
| 212 | EX(.ex_hndlr_lcpy_1, (p[A]) ld8 v[A] = [src_pre_mem], 128) // M0 |
| 213 | EK(.ex_hndlr_lcpy_1, (p[B]) st8 [dst_pre_mem] = v[B], 128) // M2 |
| 214 | br.ctop.sptk .prefetch_loop |
| 215 | ;; |
| 216 | cmp.eq p16, p0 = r0, r0 // reset p16 to 1 |
| 217 | mov ar.lc = cnt |
| 218 | mov ar.ec = N // # of stages in pipeline |
| 219 | ;; |
| 220 | .line_copy: |
| 221 | EX(.ex_handler, (p[D]) ld8 t2 = [src0], 3*8) // M0 |
| 222 | EK(.ex_handler, (p[D]) ld8 t4 = [src1], 3*8) // M1 |
| 223 | EX(.ex_handler_lcpy, (p[B]) st8 [dst_pre_mem] = v[B], 128) // M2 prefetch dst from memory |
| 224 | EK(.ex_handler_lcpy, (p[D]) st8 [dst_pre_l2] = n8, 128) // M3 prefetch dst from L2 |
| 225 | ;; |
| 226 | EX(.ex_handler_lcpy, (p[A]) ld8 v[A] = [src_pre_mem], 128) // M0 prefetch src from memory |
| 227 | EK(.ex_handler_lcpy, (p[C]) ld8 n8 = [src_pre_l2], 128) // M1 prefetch src from L2 |
| 228 | EX(.ex_handler, (p[D]) st8 [dst0] = t1, 8) // M2 |
| 229 | EK(.ex_handler, (p[D]) st8 [dst1] = t3, 8) // M3 |
| 230 | ;; |
| 231 | EX(.ex_handler, (p[D]) ld8 t5 = [src0], 8) |
| 232 | EK(.ex_handler, (p[D]) ld8 t7 = [src1], 3*8) |
| 233 | EX(.ex_handler, (p[D]) st8 [dst0] = t2, 3*8) |
| 234 | EK(.ex_handler, (p[D]) st8 [dst1] = t4, 3*8) |
| 235 | ;; |
| 236 | EX(.ex_handler, (p[D]) ld8 t6 = [src0], 3*8) |
| 237 | EK(.ex_handler, (p[D]) ld8 t10 = [src1], 8) |
| 238 | EX(.ex_handler, (p[D]) st8 [dst0] = t5, 8) |
| 239 | EK(.ex_handler, (p[D]) st8 [dst1] = t7, 3*8) |
| 240 | ;; |
| 241 | EX(.ex_handler, (p[D]) ld8 t9 = [src0], 3*8) |
| 242 | EK(.ex_handler, (p[D]) ld8 t11 = [src1], 3*8) |
| 243 | EX(.ex_handler, (p[D]) st8 [dst0] = t6, 3*8) |
| 244 | EK(.ex_handler, (p[D]) st8 [dst1] = t10, 8) |
| 245 | ;; |
| 246 | EX(.ex_handler, (p[D]) ld8 t12 = [src0], 8) |
| 247 | EK(.ex_handler, (p[D]) ld8 t14 = [src1], 8) |
| 248 | EX(.ex_handler, (p[D]) st8 [dst0] = t9, 3*8) |
| 249 | EK(.ex_handler, (p[D]) st8 [dst1] = t11, 3*8) |
| 250 | ;; |
| 251 | EX(.ex_handler, (p[D]) ld8 t13 = [src0], 4*8) |
| 252 | EK(.ex_handler, (p[D]) ld8 t15 = [src1], 4*8) |
| 253 | EX(.ex_handler, (p[D]) st8 [dst0] = t12, 8) |
| 254 | EK(.ex_handler, (p[D]) st8 [dst1] = t14, 8) |
| 255 | ;; |
| 256 | EX(.ex_handler, (p[C]) ld8 t1 = [src0], 8) |
| 257 | EK(.ex_handler, (p[C]) ld8 t3 = [src1], 8) |
| 258 | EX(.ex_handler, (p[D]) st8 [dst0] = t13, 4*8) |
| 259 | EK(.ex_handler, (p[D]) st8 [dst1] = t15, 4*8) |
| 260 | br.ctop.sptk .line_copy |
| 261 | ;; |
| 262 | |
| 263 | add dst0=-8,dst0 |
| 264 | add src0=-8,src0 |
| 265 | mov in2=tmp |
| 266 | .restore sp |
| 267 | br.sptk.many .medium_copy |
| 268 | ;; |
| 269 | |
| 270 | #define BLOCK_SIZE 128*32 |
| 271 | #define blocksize r23 |
| 272 | #define curlen r24 |
| 273 | |
| 274 | // dest is on 8-byte boundary, src is not. We need to do |
| 275 | // ld8-ld8, shrp, then st8. Max 8 byte copy per cycle. |
| 276 | .unaligned_src: |
| 277 | .prologue |
| 278 | .save ar.pfs, saved_pfs |
| 279 | alloc saved_pfs=ar.pfs,3,5,0,8 |
| 280 | .save ar.lc, saved_lc |
| 281 | mov saved_lc=ar.lc |
| 282 | .save pr, saved_pr |
| 283 | mov saved_pr=pr |
| 284 | .body |
| 285 | .4k_block: |
| 286 | mov saved_in0=dst0 // need to save all input arguments |
| 287 | mov saved_in2=in2 |
| 288 | mov blocksize=BLOCK_SIZE |
| 289 | ;; |
| 290 | cmp.lt p6,p7=blocksize,in2 |
| 291 | mov saved_in1=src0 |
| 292 | ;; |
| 293 | (p6) mov in2=blocksize |
| 294 | ;; |
| 295 | shr.u r21=in2,7 // this much cache line |
| 296 | shr.u r22=in2,4 // number of 16-byte iteration |
| 297 | and curlen=15,in2 // copy length after iteration |
| 298 | and r30=7,src0 // source alignment |
| 299 | ;; |
| 300 | cmp.lt p7,p8=1,r21 |
| 301 | add cnt=-1,r21 |
| 302 | ;; |
| 303 | |
| 304 | add src_pre_mem=0,src0 // prefetch src pointer |
| 305 | add dst_pre_mem=0,dst0 // prefetch dest pointer |
| 306 | and src0=-8,src0 // 1st src pointer |
Keith Owens | 6118ec8 | 2005-04-25 13:23:47 -0700 | [diff] [blame] | 307 | (p7) mov ar.lc = cnt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | (p8) mov ar.lc = r0 |
| 309 | ;; |
| 310 | TEXT_ALIGN(32) |
| 311 | 1: lfetch.fault [src_pre_mem], 128 |
| 312 | lfetch.fault.excl [dst_pre_mem], 128 |
| 313 | br.cloop.dptk.few 1b |
| 314 | ;; |
| 315 | |
| 316 | shladd dst1=r22,3,dst0 // 2nd dest pointer |
| 317 | shladd src1=r22,3,src0 // 2nd src pointer |
| 318 | cmp.eq p8,p9=r22,r0 // do we really need to loop? |
| 319 | cmp.le p6,p7=8,curlen; // have at least 8 byte remaining? |
| 320 | add cnt=-1,r22 // ctop iteration adjustment |
| 321 | ;; |
| 322 | EX(.ex_handler, (p9) ld8 r33=[src0],8) // loop primer |
| 323 | EK(.ex_handler, (p9) ld8 r37=[src1],8) |
| 324 | (p8) br.dpnt.few .noloop |
| 325 | ;; |
| 326 | |
| 327 | // The jump address is calculated based on src alignment. The COPYU |
| 328 | // macro below need to confine its size to power of two, so an entry |
| 329 | // can be caulated using shl instead of an expensive multiply. The |
| 330 | // size is then hard coded by the following #define to match the |
| 331 | // actual size. This make it somewhat tedious when COPYU macro gets |
| 332 | // changed and this need to be adjusted to match. |
| 333 | #define LOOP_SIZE 6 |
| 334 | 1: |
| 335 | mov r29=ip // jmp_table thread |
| 336 | mov ar.lc=cnt |
| 337 | ;; |
| 338 | add r29=.jump_table - 1b - (.jmp1-.jump_table), r29 |
| 339 | shl r28=r30, LOOP_SIZE // jmp_table thread |
| 340 | mov ar.ec=2 // loop setup |
| 341 | ;; |
| 342 | add r29=r29,r28 // jmp_table thread |
| 343 | cmp.eq p16,p17=r0,r0 |
| 344 | ;; |
| 345 | mov b6=r29 // jmp_table thread |
| 346 | ;; |
| 347 | br.cond.sptk.few b6 |
| 348 | |
| 349 | // for 8-15 byte case |
| 350 | // We will skip the loop, but need to replicate the side effect |
| 351 | // that the loop produces. |
| 352 | .noloop: |
| 353 | EX(.ex_handler, (p6) ld8 r37=[src1],8) |
| 354 | add src0=8,src0 |
| 355 | (p6) shl r25=r30,3 |
| 356 | ;; |
| 357 | EX(.ex_handler, (p6) ld8 r27=[src1]) |
| 358 | (p6) shr.u r28=r37,r25 |
| 359 | (p6) sub r26=64,r25 |
| 360 | ;; |
| 361 | (p6) shl r27=r27,r26 |
| 362 | ;; |
| 363 | (p6) or r21=r28,r27 |
| 364 | |
| 365 | .unaligned_src_tail: |
| 366 | /* check if we have more than blocksize to copy, if so go back */ |
| 367 | cmp.gt p8,p0=saved_in2,blocksize |
| 368 | ;; |
| 369 | (p8) add dst0=saved_in0,blocksize |
| 370 | (p8) add src0=saved_in1,blocksize |
| 371 | (p8) sub in2=saved_in2,blocksize |
| 372 | (p8) br.dpnt .4k_block |
| 373 | ;; |
| 374 | |
| 375 | /* we have up to 15 byte to copy in the tail. |
| 376 | * part of work is already done in the jump table code |
| 377 | * we are at the following state. |
| 378 | * src side: |
| 379 | * |
| 380 | * xxxxxx xx <----- r21 has xxxxxxxx already |
| 381 | * -------- -------- -------- |
| 382 | * 0 8 16 |
| 383 | * ^ |
| 384 | * | |
| 385 | * src1 |
| 386 | * |
| 387 | * dst |
| 388 | * -------- -------- -------- |
| 389 | * ^ |
| 390 | * | |
| 391 | * dst1 |
| 392 | */ |
| 393 | EX(.ex_handler, (p6) st8 [dst1]=r21,8) // more than 8 byte to copy |
| 394 | (p6) add curlen=-8,curlen // update length |
| 395 | mov ar.pfs=saved_pfs |
| 396 | ;; |
| 397 | mov ar.lc=saved_lc |
| 398 | mov pr=saved_pr,-1 |
| 399 | mov in2=curlen // remaining length |
| 400 | mov dst0=dst1 // dest pointer |
| 401 | add src0=src1,r30 // forward by src alignment |
| 402 | ;; |
| 403 | |
| 404 | // 7 byte or smaller. |
| 405 | .memcpy_short: |
| 406 | cmp.le p8,p9 = 1,in2 |
| 407 | cmp.le p10,p11 = 2,in2 |
| 408 | cmp.le p12,p13 = 3,in2 |
| 409 | cmp.le p14,p15 = 4,in2 |
| 410 | add src1=1,src0 // second src pointer |
| 411 | add dst1=1,dst0 // second dest pointer |
| 412 | ;; |
| 413 | |
| 414 | EX(.ex_handler_short, (p8) ld1 t1=[src0],2) |
| 415 | EK(.ex_handler_short, (p10) ld1 t2=[src1],2) |
| 416 | (p9) br.ret.dpnt rp // 0 byte copy |
| 417 | ;; |
| 418 | |
| 419 | EX(.ex_handler_short, (p8) st1 [dst0]=t1,2) |
| 420 | EK(.ex_handler_short, (p10) st1 [dst1]=t2,2) |
| 421 | (p11) br.ret.dpnt rp // 1 byte copy |
| 422 | |
| 423 | EX(.ex_handler_short, (p12) ld1 t3=[src0],2) |
| 424 | EK(.ex_handler_short, (p14) ld1 t4=[src1],2) |
| 425 | (p13) br.ret.dpnt rp // 2 byte copy |
| 426 | ;; |
| 427 | |
| 428 | cmp.le p6,p7 = 5,in2 |
| 429 | cmp.le p8,p9 = 6,in2 |
| 430 | cmp.le p10,p11 = 7,in2 |
| 431 | |
| 432 | EX(.ex_handler_short, (p12) st1 [dst0]=t3,2) |
| 433 | EK(.ex_handler_short, (p14) st1 [dst1]=t4,2) |
| 434 | (p15) br.ret.dpnt rp // 3 byte copy |
| 435 | ;; |
| 436 | |
| 437 | EX(.ex_handler_short, (p6) ld1 t5=[src0],2) |
| 438 | EK(.ex_handler_short, (p8) ld1 t6=[src1],2) |
| 439 | (p7) br.ret.dpnt rp // 4 byte copy |
| 440 | ;; |
| 441 | |
| 442 | EX(.ex_handler_short, (p6) st1 [dst0]=t5,2) |
| 443 | EK(.ex_handler_short, (p8) st1 [dst1]=t6,2) |
| 444 | (p9) br.ret.dptk rp // 5 byte copy |
| 445 | |
| 446 | EX(.ex_handler_short, (p10) ld1 t7=[src0],2) |
| 447 | (p11) br.ret.dptk rp // 6 byte copy |
| 448 | ;; |
| 449 | |
| 450 | EX(.ex_handler_short, (p10) st1 [dst0]=t7,2) |
| 451 | br.ret.dptk rp // done all cases |
| 452 | |
| 453 | |
| 454 | /* Align dest to nearest 8-byte boundary. We know we have at |
| 455 | * least 7 bytes to copy, enough to crawl to 8-byte boundary. |
| 456 | * Actual number of byte to crawl depend on the dest alignment. |
| 457 | * 7 byte or less is taken care at .memcpy_short |
| 458 | |
| 459 | * src0 - source even index |
| 460 | * src1 - source odd index |
| 461 | * dst0 - dest even index |
| 462 | * dst1 - dest odd index |
| 463 | * r30 - distance to 8-byte boundary |
| 464 | */ |
| 465 | |
| 466 | .align_dest: |
| 467 | add src1=1,in1 // source odd index |
| 468 | cmp.le p7,p0 = 2,r30 // for .align_dest |
| 469 | cmp.le p8,p0 = 3,r30 // for .align_dest |
| 470 | EX(.ex_handler_short, (p6) ld1 t1=[src0],2) |
| 471 | cmp.le p9,p0 = 4,r30 // for .align_dest |
| 472 | cmp.le p10,p0 = 5,r30 |
| 473 | ;; |
| 474 | EX(.ex_handler_short, (p7) ld1 t2=[src1],2) |
| 475 | EK(.ex_handler_short, (p8) ld1 t3=[src0],2) |
| 476 | cmp.le p11,p0 = 6,r30 |
| 477 | EX(.ex_handler_short, (p6) st1 [dst0] = t1,2) |
| 478 | cmp.le p12,p0 = 7,r30 |
| 479 | ;; |
| 480 | EX(.ex_handler_short, (p9) ld1 t4=[src1],2) |
| 481 | EK(.ex_handler_short, (p10) ld1 t5=[src0],2) |
| 482 | EX(.ex_handler_short, (p7) st1 [dst1] = t2,2) |
| 483 | EK(.ex_handler_short, (p8) st1 [dst0] = t3,2) |
| 484 | ;; |
| 485 | EX(.ex_handler_short, (p11) ld1 t6=[src1],2) |
| 486 | EK(.ex_handler_short, (p12) ld1 t7=[src0],2) |
| 487 | cmp.eq p6,p7=r28,r29 |
| 488 | EX(.ex_handler_short, (p9) st1 [dst1] = t4,2) |
| 489 | EK(.ex_handler_short, (p10) st1 [dst0] = t5,2) |
| 490 | sub in2=in2,r30 |
| 491 | ;; |
| 492 | EX(.ex_handler_short, (p11) st1 [dst1] = t6,2) |
| 493 | EK(.ex_handler_short, (p12) st1 [dst0] = t7) |
| 494 | add dst0=in0,r30 // setup arguments |
| 495 | add src0=in1,r30 |
| 496 | (p6) br.cond.dptk .aligned_src |
| 497 | (p7) br.cond.dpnt .unaligned_src |
| 498 | ;; |
| 499 | |
| 500 | /* main loop body in jump table format */ |
| 501 | #define COPYU(shift) \ |
| 502 | 1: \ |
| 503 | EX(.ex_handler, (p16) ld8 r32=[src0],8); /* 1 */ \ |
| 504 | EK(.ex_handler, (p16) ld8 r36=[src1],8); \ |
| 505 | (p17) shrp r35=r33,r34,shift;; /* 1 */ \ |
| 506 | EX(.ex_handler, (p6) ld8 r22=[src1]); /* common, prime for tail section */ \ |
| 507 | nop.m 0; \ |
| 508 | (p16) shrp r38=r36,r37,shift; \ |
| 509 | EX(.ex_handler, (p17) st8 [dst0]=r35,8); /* 1 */ \ |
| 510 | EK(.ex_handler, (p17) st8 [dst1]=r39,8); \ |
| 511 | br.ctop.dptk.few 1b;; \ |
| 512 | (p7) add src1=-8,src1; /* back out for <8 byte case */ \ |
| 513 | shrp r21=r22,r38,shift; /* speculative work */ \ |
| 514 | br.sptk.few .unaligned_src_tail /* branch out of jump table */ \ |
| 515 | ;; |
| 516 | TEXT_ALIGN(32) |
| 517 | .jump_table: |
| 518 | COPYU(8) // unaligned cases |
| 519 | .jmp1: |
| 520 | COPYU(16) |
| 521 | COPYU(24) |
| 522 | COPYU(32) |
| 523 | COPYU(40) |
| 524 | COPYU(48) |
| 525 | COPYU(56) |
| 526 | |
| 527 | #undef A |
| 528 | #undef B |
| 529 | #undef C |
| 530 | #undef D |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Due to lack of local tag support in gcc 2.x assembler, it is not clear which |
| 534 | * instruction failed in the bundle. The exception algorithm is that we |
| 535 | * first figure out the faulting address, then detect if there is any |
| 536 | * progress made on the copy, if so, redo the copy from last known copied |
| 537 | * location up to the faulting address (exclusive). In the copy_from_user |
| 538 | * case, remaining byte in kernel buffer will be zeroed. |
| 539 | * |
| 540 | * Take copy_from_user as an example, in the code there are multiple loads |
| 541 | * in a bundle and those multiple loads could span over two pages, the |
| 542 | * faulting address is calculated as page_round_down(max(src0, src1)). |
| 543 | * This is based on knowledge that if we can access one byte in a page, we |
| 544 | * can access any byte in that page. |
| 545 | * |
| 546 | * predicate used in the exception handler: |
| 547 | * p6-p7: direction |
| 548 | * p10-p11: src faulting addr calculation |
| 549 | * p12-p13: dst faulting addr calculation |
| 550 | */ |
| 551 | |
| 552 | #define A r19 |
| 553 | #define B r20 |
| 554 | #define C r21 |
| 555 | #define D r22 |
| 556 | #define F r28 |
| 557 | |
| 558 | #define memset_arg0 r32 |
| 559 | #define memset_arg2 r33 |
| 560 | |
| 561 | #define saved_retval loc0 |
| 562 | #define saved_rtlink loc1 |
| 563 | #define saved_pfs_stack loc2 |
| 564 | |
| 565 | .ex_hndlr_s: |
| 566 | add src0=8,src0 |
| 567 | br.sptk .ex_handler |
| 568 | ;; |
| 569 | .ex_hndlr_d: |
| 570 | add dst0=8,dst0 |
| 571 | br.sptk .ex_handler |
| 572 | ;; |
| 573 | .ex_hndlr_lcpy_1: |
| 574 | mov src1=src_pre_mem |
| 575 | mov dst1=dst_pre_mem |
| 576 | cmp.gtu p10,p11=src_pre_mem,saved_in1 |
| 577 | cmp.gtu p12,p13=dst_pre_mem,saved_in0 |
| 578 | ;; |
| 579 | (p10) add src0=8,saved_in1 |
| 580 | (p11) mov src0=saved_in1 |
| 581 | (p12) add dst0=8,saved_in0 |
| 582 | (p13) mov dst0=saved_in0 |
| 583 | br.sptk .ex_handler |
| 584 | .ex_handler_lcpy: |
| 585 | // in line_copy block, the preload addresses should always ahead |
| 586 | // of the other two src/dst pointers. Furthermore, src1/dst1 should |
| 587 | // always ahead of src0/dst0. |
| 588 | mov src1=src_pre_mem |
| 589 | mov dst1=dst_pre_mem |
| 590 | .ex_handler: |
| 591 | mov pr=saved_pr,-1 // first restore pr, lc, and pfs |
| 592 | mov ar.lc=saved_lc |
| 593 | mov ar.pfs=saved_pfs |
| 594 | ;; |
| 595 | .ex_handler_short: // fault occurred in these sections didn't change pr, lc, pfs |
| 596 | cmp.ltu p6,p7=saved_in0, saved_in1 // get the copy direction |
| 597 | cmp.ltu p10,p11=src0,src1 |
| 598 | cmp.ltu p12,p13=dst0,dst1 |
| 599 | fcmp.eq p8,p0=f6,f0 // is it memcpy? |
| 600 | mov tmp = dst0 |
| 601 | ;; |
| 602 | (p11) mov src1 = src0 // pick the larger of the two |
| 603 | (p13) mov dst0 = dst1 // make dst0 the smaller one |
| 604 | (p13) mov dst1 = tmp // and dst1 the larger one |
| 605 | ;; |
| 606 | (p6) dep F = r0,dst1,0,PAGE_SHIFT // usr dst round down to page boundary |
| 607 | (p7) dep F = r0,src1,0,PAGE_SHIFT // usr src round down to page boundary |
| 608 | ;; |
| 609 | (p6) cmp.le p14,p0=dst0,saved_in0 // no progress has been made on store |
| 610 | (p7) cmp.le p14,p0=src0,saved_in1 // no progress has been made on load |
| 611 | mov retval=saved_in2 |
| 612 | (p8) ld1 tmp=[src1] // force an oops for memcpy call |
| 613 | (p8) st1 [dst1]=r0 // force an oops for memcpy call |
| 614 | (p14) br.ret.sptk.many rp |
| 615 | |
| 616 | /* |
| 617 | * The remaining byte to copy is calculated as: |
| 618 | * |
| 619 | * A = (faulting_addr - orig_src) -> len to faulting ld address |
| 620 | * or |
| 621 | * (faulting_addr - orig_dst) -> len to faulting st address |
| 622 | * B = (cur_dst - orig_dst) -> len copied so far |
| 623 | * C = A - B -> len need to be copied |
| 624 | * D = orig_len - A -> len need to be zeroed |
| 625 | */ |
| 626 | (p6) sub A = F, saved_in0 |
| 627 | (p7) sub A = F, saved_in1 |
| 628 | clrrrb |
| 629 | ;; |
| 630 | alloc saved_pfs_stack=ar.pfs,3,3,3,0 |
Chen, Kenneth W | 295bd89 | 2005-09-06 16:05:23 -0700 | [diff] [blame] | 631 | cmp.lt p8,p0=A,r0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | sub B = dst0, saved_in0 // how many byte copied so far |
| 633 | ;; |
Chen, Kenneth W | 295bd89 | 2005-09-06 16:05:23 -0700 | [diff] [blame] | 634 | (p8) mov A = 0; // A shouldn't be negative, cap it |
| 635 | ;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | sub C = A, B |
| 637 | sub D = saved_in2, A |
| 638 | ;; |
| 639 | cmp.gt p8,p0=C,r0 // more than 1 byte? |
| 640 | add memset_arg0=saved_in0, A |
| 641 | (p6) mov memset_arg2=0 // copy_to_user should not call memset |
| 642 | (p7) mov memset_arg2=D // copy_from_user need to have kbuf zeroed |
| 643 | mov r8=0 |
| 644 | mov saved_retval = D |
| 645 | mov saved_rtlink = b0 |
| 646 | |
| 647 | add out0=saved_in0, B |
| 648 | add out1=saved_in1, B |
| 649 | mov out2=C |
| 650 | (p8) br.call.sptk.few b0=__copy_user // recursive call |
| 651 | ;; |
| 652 | |
| 653 | add saved_retval=saved_retval,r8 // above might return non-zero value |
| 654 | cmp.gt p8,p0=memset_arg2,r0 // more than 1 byte? |
| 655 | mov out0=memset_arg0 // *s |
| 656 | mov out1=r0 // c |
| 657 | mov out2=memset_arg2 // n |
| 658 | (p8) br.call.sptk.few b0=memset |
| 659 | ;; |
| 660 | |
| 661 | mov retval=saved_retval |
| 662 | mov ar.pfs=saved_pfs_stack |
| 663 | mov b0=saved_rtlink |
| 664 | br.ret.sptk.many rp |
| 665 | |
| 666 | /* end of McKinley specific optimization */ |
| 667 | END(__copy_user) |