/* SPDX-License-Identifier: GPL-2.0 */1/*2* arch/alpha/lib/ev6-copy_user.S3*4* 21264 version contributed by Rick Gorton <[email protected]>5*6* Copy to/from user space, handling exceptions as we go.. This7* isn't exactly pretty.8*9* This is essentially the same as "memcpy()", but with a few twists.10* Notably, we have to make sure that $0 is always up-to-date and11* contains the right "bytes left to copy" value (and that it is updated12* only _after_ a successful copy). There is also some rather minor13* exception setup stuff..14*15* Much of the information about 21264 scheduling/coding comes from:16* Compiler Writer's Guide for the Alpha 2126417* abbreviated as 'CWG' in other comments here18* ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html19* Scheduling notation:20* E - either cluster21* U - upper subcluster; U0 - subcluster U0; U1 - subcluster U122* L - lower subcluster; L0 - subcluster L0; L1 - subcluster L123*/2425#include <linux/export.h>26/* Allow an exception for an insn; exit if we get one. */27#define EXI(x,y...) \2899: x,##y; \29.section __ex_table,"a"; \30.long 99b - .; \31lda $31, $exitin-99b($31); \32.previous3334#define EXO(x,y...) \3599: x,##y; \36.section __ex_table,"a"; \37.long 99b - .; \38lda $31, $exitout-99b($31); \39.previous4041.set noat42.align 443.globl __copy_user44.ent __copy_user45# Pipeline info: Slotting & Comments46__copy_user:47.prologue 048mov $18, $0 # .. .. .. E49subq $18, 32, $1 # .. .. E. .. : Is this going to be a small copy?50nop # .. E .. ..51beq $18, $zerolength # U .. .. .. : U L U L5253and $16,7,$3 # .. .. .. E : is leading dest misalignment54ble $1, $onebyteloop # .. .. U .. : 1st branch : small amount of data55beq $3, $destaligned # .. U .. .. : 2nd (one cycle fetcher stall)56subq $3, 8, $3 # E .. .. .. : L U U L : trip counter57/*58* The fetcher stall also hides the 1 cycle cross-cluster stall for $3 (L --> U)59* This loop aligns the destination a byte at a time60* We know we have at least one trip through this loop61*/62$aligndest:63EXI( ldbu $1,0($17) ) # .. .. .. L : Keep loads separate from stores64addq $16,1,$16 # .. .. E .. : Section 3.8 in the CWG65addq $3,1,$3 # .. E .. .. :66nop # E .. .. .. : U L U L6768/*69* the -1 is to compensate for the inc($16) done in a previous quadpack70* which allows us zero dependencies within either quadpack in the loop71*/72EXO( stb $1,-1($16) ) # .. .. .. L :73addq $17,1,$17 # .. .. E .. : Section 3.8 in the CWG74subq $0,1,$0 # .. E .. .. :75bne $3, $aligndest # U .. .. .. : U L U L7677/*78* If we fell through into here, we have a minimum of 33 - 7 bytes79* If we arrived via branch, we have a minimum of 32 bytes80*/81$destaligned:82and $17,7,$1 # .. .. .. E : Check _current_ source alignment83bic $0,7,$4 # .. .. E .. : number bytes as a quadword loop84EXI( ldq_u $3,0($17) ) # .. L .. .. : Forward fetch for fallthrough code85beq $1,$quadaligned # U .. .. .. : U L U L8687/*88* In the worst case, we've just executed an ldq_u here from 0($17)89* and we'll repeat it once if we take the branch90*/9192/* Misaligned quadword loop - not unrolled. Leave it that way. */93$misquad:94EXI( ldq_u $2,8($17) ) # .. .. .. L :95subq $4,8,$4 # .. .. E .. :96extql $3,$17,$3 # .. U .. .. :97extqh $2,$17,$1 # U .. .. .. : U U L L9899bis $3,$1,$1 # .. .. .. E :100EXO( stq $1,0($16) ) # .. .. L .. :101addq $17,8,$17 # .. E .. .. :102subq $0,8,$0 # E .. .. .. : U L L U103104addq $16,8,$16 # .. .. .. E :105bis $2,$2,$3 # .. .. E .. :106nop # .. E .. .. :107bne $4,$misquad # U .. .. .. : U L U L108109nop # .. .. .. E110nop # .. .. E ..111nop # .. E .. ..112beq $0,$zerolength # U .. .. .. : U L U L113114/* We know we have at least one trip through the byte loop */115EXI ( ldbu $2,0($17) ) # .. .. .. L : No loads in the same quad116addq $16,1,$16 # .. .. E .. : as the store (Section 3.8 in CWG)117nop # .. E .. .. :118br $31, $dirtyentry # L0 .. .. .. : L U U L119/* Do the trailing byte loop load, then hop into the store part of the loop */120121/*122* A minimum of (33 - 7) bytes to do a quad at a time.123* Based upon the usage context, it's worth the effort to unroll this loop124* $0 - number of bytes to be moved125* $4 - number of bytes to move as quadwords126* $16 is current destination address127* $17 is current source address128*/129$quadaligned:130subq $4, 32, $2 # .. .. .. E : do not unroll for small stuff131nop # .. .. E ..132nop # .. E .. ..133blt $2, $onequad # U .. .. .. : U L U L134135/*136* There is a significant assumption here that the source and destination137* addresses differ by more than 32 bytes. In this particular case, a138* sparsity of registers further bounds this to be a minimum of 8 bytes.139* But if this isn't met, then the output result will be incorrect.140* Furthermore, due to a lack of available registers, we really can't141* unroll this to be an 8x loop (which would enable us to use the wh64142* instruction memory hint instruction).143*/144$unroll4:145EXI( ldq $1,0($17) ) # .. .. .. L146EXI( ldq $2,8($17) ) # .. .. L ..147subq $4,32,$4 # .. E .. ..148nop # E .. .. .. : U U L L149150addq $17,16,$17 # .. .. .. E151EXO( stq $1,0($16) ) # .. .. L ..152EXO( stq $2,8($16) ) # .. L .. ..153subq $0,16,$0 # E .. .. .. : U L L U154155addq $16,16,$16 # .. .. .. E156EXI( ldq $1,0($17) ) # .. .. L ..157EXI( ldq $2,8($17) ) # .. L .. ..158subq $4, 32, $3 # E .. .. .. : U U L L : is there enough for another trip?159160EXO( stq $1,0($16) ) # .. .. .. L161EXO( stq $2,8($16) ) # .. .. L ..162subq $0,16,$0 # .. E .. ..163addq $17,16,$17 # E .. .. .. : U L L U164165nop # .. .. .. E166nop # .. .. E ..167addq $16,16,$16 # .. E .. ..168bgt $3,$unroll4 # U .. .. .. : U L U L169170nop171nop172nop173beq $4, $noquads174175$onequad:176EXI( ldq $1,0($17) )177subq $4,8,$4178addq $17,8,$17179nop180181EXO( stq $1,0($16) )182subq $0,8,$0183addq $16,8,$16184bne $4,$onequad185186$noquads:187nop188nop189nop190beq $0,$zerolength191192/*193* For small copies (or the tail of a larger copy), do a very simple byte loop.194* There's no point in doing a lot of complex alignment calculations to try to195* to quadword stuff for a small amount of data.196* $0 - remaining number of bytes left to copy197* $16 - current dest addr198* $17 - current source addr199*/200201$onebyteloop:202EXI ( ldbu $2,0($17) ) # .. .. .. L : No loads in the same quad203addq $16,1,$16 # .. .. E .. : as the store (Section 3.8 in CWG)204nop # .. E .. .. :205nop # E .. .. .. : U L U L206207$dirtyentry:208/*209* the -1 is to compensate for the inc($16) done in a previous quadpack210* which allows us zero dependencies within either quadpack in the loop211*/212EXO ( stb $2,-1($16) ) # .. .. .. L :213addq $17,1,$17 # .. .. E .. : quadpack as the load214subq $0,1,$0 # .. E .. .. : change count _after_ copy215bgt $0,$onebyteloop # U .. .. .. : U L U L216217$zerolength:218$exitin:219$exitout: # Destination for exception recovery(?)220nop # .. .. .. E221nop # .. .. E ..222nop # .. E .. ..223ret $31,($26),1 # L0 .. .. .. : L U L U224225.end __copy_user226EXPORT_SYMBOL(__copy_user)227228229