/*1* arch/alpha/lib/ev6-copy_user.S2*3* 21264 version contributed by Rick Gorton <[email protected]>4*5* Copy to/from user space, handling exceptions as we go.. This6* isn't exactly pretty.7*8* This is essentially the same as "memcpy()", but with a few twists.9* Notably, we have to make sure that $0 is always up-to-date and10* contains the right "bytes left to copy" value (and that it is updated11* only _after_ a successful copy). There is also some rather minor12* exception setup stuff..13*14* NOTE! This is not directly C-callable, because the calling semantics are15* different:16*17* Inputs:18* length in $019* destination address in $620* source address in $721* return address in $2822*23* Outputs:24* bytes left to copy in $025*26* Clobbers:27* $1,$2,$3,$4,$5,$6,$728*29* Much of the information about 21264 scheduling/coding comes from:30* Compiler Writer's Guide for the Alpha 2126431* abbreviated as 'CWG' in other comments here32* ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html33* Scheduling notation:34* E - either cluster35* U - upper subcluster; U0 - subcluster U0; U1 - subcluster U136* L - lower subcluster; L0 - subcluster L0; L1 - subcluster L137*/3839/* Allow an exception for an insn; exit if we get one. */40#define EXI(x,y...) \4199: x,##y; \42.section __ex_table,"a"; \43.long 99b - .; \44lda $31, $exitin-99b($31); \45.previous4647#define EXO(x,y...) \4899: x,##y; \49.section __ex_table,"a"; \50.long 99b - .; \51lda $31, $exitout-99b($31); \52.previous5354.set noat55.align 456.globl __copy_user57.ent __copy_user58# Pipeline info: Slotting & Comments59__copy_user:60.prologue 061subq $0, 32, $1 # .. E .. .. : Is this going to be a small copy?62beq $0, $zerolength # U .. .. .. : U L U L6364and $6,7,$3 # .. .. .. E : is leading dest misalignment65ble $1, $onebyteloop # .. .. U .. : 1st branch : small amount of data66beq $3, $destaligned # .. U .. .. : 2nd (one cycle fetcher stall)67subq $3, 8, $3 # E .. .. .. : L U U L : trip counter68/*69* The fetcher stall also hides the 1 cycle cross-cluster stall for $3 (L --> U)70* This loop aligns the destination a byte at a time71* We know we have at least one trip through this loop72*/73$aligndest:74EXI( ldbu $1,0($7) ) # .. .. .. L : Keep loads separate from stores75addq $6,1,$6 # .. .. E .. : Section 3.8 in the CWG76addq $3,1,$3 # .. E .. .. :77nop # E .. .. .. : U L U L7879/*80* the -1 is to compensate for the inc($6) done in a previous quadpack81* which allows us zero dependencies within either quadpack in the loop82*/83EXO( stb $1,-1($6) ) # .. .. .. L :84addq $7,1,$7 # .. .. E .. : Section 3.8 in the CWG85subq $0,1,$0 # .. E .. .. :86bne $3, $aligndest # U .. .. .. : U L U L8788/*89* If we fell through into here, we have a minimum of 33 - 7 bytes90* If we arrived via branch, we have a minimum of 32 bytes91*/92$destaligned:93and $7,7,$1 # .. .. .. E : Check _current_ source alignment94bic $0,7,$4 # .. .. E .. : number bytes as a quadword loop95EXI( ldq_u $3,0($7) ) # .. L .. .. : Forward fetch for fallthrough code96beq $1,$quadaligned # U .. .. .. : U L U L9798/*99* In the worst case, we've just executed an ldq_u here from 0($7)100* and we'll repeat it once if we take the branch101*/102103/* Misaligned quadword loop - not unrolled. Leave it that way. */104$misquad:105EXI( ldq_u $2,8($7) ) # .. .. .. L :106subq $4,8,$4 # .. .. E .. :107extql $3,$7,$3 # .. U .. .. :108extqh $2,$7,$1 # U .. .. .. : U U L L109110bis $3,$1,$1 # .. .. .. E :111EXO( stq $1,0($6) ) # .. .. L .. :112addq $7,8,$7 # .. E .. .. :113subq $0,8,$0 # E .. .. .. : U L L U114115addq $6,8,$6 # .. .. .. E :116bis $2,$2,$3 # .. .. E .. :117nop # .. E .. .. :118bne $4,$misquad # U .. .. .. : U L U L119120nop # .. .. .. E121nop # .. .. E ..122nop # .. E .. ..123beq $0,$zerolength # U .. .. .. : U L U L124125/* We know we have at least one trip through the byte loop */126EXI ( ldbu $2,0($7) ) # .. .. .. L : No loads in the same quad127addq $6,1,$6 # .. .. E .. : as the store (Section 3.8 in CWG)128nop # .. E .. .. :129br $31, $dirtyentry # L0 .. .. .. : L U U L130/* Do the trailing byte loop load, then hop into the store part of the loop */131132/*133* A minimum of (33 - 7) bytes to do a quad at a time.134* Based upon the usage context, it's worth the effort to unroll this loop135* $0 - number of bytes to be moved136* $4 - number of bytes to move as quadwords137* $6 is current destination address138* $7 is current source address139*/140$quadaligned:141subq $4, 32, $2 # .. .. .. E : do not unroll for small stuff142nop # .. .. E ..143nop # .. E .. ..144blt $2, $onequad # U .. .. .. : U L U L145146/*147* There is a significant assumption here that the source and destination148* addresses differ by more than 32 bytes. In this particular case, a149* sparsity of registers further bounds this to be a minimum of 8 bytes.150* But if this isn't met, then the output result will be incorrect.151* Furthermore, due to a lack of available registers, we really can't152* unroll this to be an 8x loop (which would enable us to use the wh64153* instruction memory hint instruction).154*/155$unroll4:156EXI( ldq $1,0($7) ) # .. .. .. L157EXI( ldq $2,8($7) ) # .. .. L ..158subq $4,32,$4 # .. E .. ..159nop # E .. .. .. : U U L L160161addq $7,16,$7 # .. .. .. E162EXO( stq $1,0($6) ) # .. .. L ..163EXO( stq $2,8($6) ) # .. L .. ..164subq $0,16,$0 # E .. .. .. : U L L U165166addq $6,16,$6 # .. .. .. E167EXI( ldq $1,0($7) ) # .. .. L ..168EXI( ldq $2,8($7) ) # .. L .. ..169subq $4, 32, $3 # E .. .. .. : U U L L : is there enough for another trip?170171EXO( stq $1,0($6) ) # .. .. .. L172EXO( stq $2,8($6) ) # .. .. L ..173subq $0,16,$0 # .. E .. ..174addq $7,16,$7 # E .. .. .. : U L L U175176nop # .. .. .. E177nop # .. .. E ..178addq $6,16,$6 # .. E .. ..179bgt $3,$unroll4 # U .. .. .. : U L U L180181nop182nop183nop184beq $4, $noquads185186$onequad:187EXI( ldq $1,0($7) )188subq $4,8,$4189addq $7,8,$7190nop191192EXO( stq $1,0($6) )193subq $0,8,$0194addq $6,8,$6195bne $4,$onequad196197$noquads:198nop199nop200nop201beq $0,$zerolength202203/*204* For small copies (or the tail of a larger copy), do a very simple byte loop.205* There's no point in doing a lot of complex alignment calculations to try to206* to quadword stuff for a small amount of data.207* $0 - remaining number of bytes left to copy208* $6 - current dest addr209* $7 - current source addr210*/211212$onebyteloop:213EXI ( ldbu $2,0($7) ) # .. .. .. L : No loads in the same quad214addq $6,1,$6 # .. .. E .. : as the store (Section 3.8 in CWG)215nop # .. E .. .. :216nop # E .. .. .. : U L U L217218$dirtyentry:219/*220* the -1 is to compensate for the inc($6) done in a previous quadpack221* which allows us zero dependencies within either quadpack in the loop222*/223EXO ( stb $2,-1($6) ) # .. .. .. L :224addq $7,1,$7 # .. .. E .. : quadpack as the load225subq $0,1,$0 # .. E .. .. : change count _after_ copy226bgt $0,$onebyteloop # U .. .. .. : U L U L227228$zerolength:229$exitout: # Destination for exception recovery(?)230nop # .. .. .. E231nop # .. .. E ..232nop # .. E .. ..233ret $31,($28),1 # L0 .. .. .. : L U L U234235$exitin:236237/* A stupid byte-by-byte zeroing of the rest of the output238buffer. This cures security holes by never leaving239random kernel data around to be copied elsewhere. */240241nop242nop243nop244mov $0,$1245246$101:247EXO ( stb $31,0($6) ) # L248subq $1,1,$1 # E249addq $6,1,$6 # E250bgt $1,$101 # U251252nop253nop254nop255ret $31,($28),1 # L0256257.end __copy_user258259260261