Merge tag 'fscrypt_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[sfrench/cifs-2.6.git] / arch / alpha / lib / ev6-copy_user.S
1 /*
2  * arch/alpha/lib/ev6-copy_user.S
3  *
4  * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
5  *
6  * Copy to/from user space, handling exceptions as we go..  This
7  * 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 and
11  * contains the right "bytes left to copy" value (and that it is updated
12  * only _after_ a successful copy). There is also some rather minor
13  * exception setup stuff..
14  *
15  * Much of the information about 21264 scheduling/coding comes from:
16  *      Compiler Writer's Guide for the Alpha 21264
17  *      abbreviated as 'CWG' in other comments here
18  *      ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
19  * Scheduling notation:
20  *      E       - either cluster
21  *      U       - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
22  *      L       - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
23  */
24
25 #include <asm/export.h>
26 /* Allow an exception for an insn; exit if we get one.  */
27 #define EXI(x,y...)                     \
28         99: x,##y;                      \
29         .section __ex_table,"a";        \
30         .long 99b - .;                  \
31         lda $31, $exitin-99b($31);      \
32         .previous
33
34 #define EXO(x,y...)                     \
35         99: x,##y;                      \
36         .section __ex_table,"a";        \
37         .long 99b - .;                  \
38         lda $31, $exitout-99b($31);     \
39         .previous
40
41         .set noat
42         .align 4
43         .globl __copy_user
44         .ent __copy_user
45                                 # Pipeline info: Slotting & Comments
46 __copy_user:
47         .prologue 0
48         andq $18, $18, $0
49         subq $18, 32, $1        # .. E  .. ..   : Is this going to be a small copy?
50         beq $0, $zerolength     # U  .. .. ..   : U L U L
51
52         and $16,7,$3            # .. .. .. E    : is leading dest misalignment
53         ble $1, $onebyteloop    # .. .. U  ..   : 1st branch : small amount of data
54         beq $3, $destaligned    # .. U  .. ..   : 2nd (one cycle fetcher stall)
55         subq $3, 8, $3          # E  .. .. ..   : L U U L : trip counter
56 /*
57  * The fetcher stall also hides the 1 cycle cross-cluster stall for $3 (L --> U)
58  * This loop aligns the destination a byte at a time
59  * We know we have at least one trip through this loop
60  */
61 $aligndest:
62         EXI( ldbu $1,0($17) )   # .. .. .. L    : Keep loads separate from stores
63         addq $16,1,$16          # .. .. E  ..   : Section 3.8 in the CWG
64         addq $3,1,$3            # .. E  .. ..   :
65         nop                     # E  .. .. ..   : U L U L
66
67 /*
68  * the -1 is to compensate for the inc($16) done in a previous quadpack
69  * which allows us zero dependencies within either quadpack in the loop
70  */
71         EXO( stb $1,-1($16) )   # .. .. .. L    :
72         addq $17,1,$17          # .. .. E  ..   : Section 3.8 in the CWG
73         subq $0,1,$0            # .. E  .. ..   :
74         bne $3, $aligndest      # U  .. .. ..   : U L U L
75
76 /*
77  * If we fell through into here, we have a minimum of 33 - 7 bytes
78  * If we arrived via branch, we have a minimum of 32 bytes
79  */
80 $destaligned:
81         and $17,7,$1            # .. .. .. E    : Check _current_ source alignment
82         bic $0,7,$4             # .. .. E  ..   : number bytes as a quadword loop
83         EXI( ldq_u $3,0($17) )  # .. L  .. ..   : Forward fetch for fallthrough code
84         beq $1,$quadaligned     # U  .. .. ..   : U L U L
85
86 /*
87  * In the worst case, we've just executed an ldq_u here from 0($17)
88  * and we'll repeat it once if we take the branch
89  */
90
91 /* Misaligned quadword loop - not unrolled.  Leave it that way. */
92 $misquad:
93         EXI( ldq_u $2,8($17) )  # .. .. .. L    :
94         subq $4,8,$4            # .. .. E  ..   :
95         extql $3,$17,$3         # .. U  .. ..   :
96         extqh $2,$17,$1         # U  .. .. ..   : U U L L
97
98         bis $3,$1,$1            # .. .. .. E    :
99         EXO( stq $1,0($16) )    # .. .. L  ..   :
100         addq $17,8,$17          # .. E  .. ..   :
101         subq $0,8,$0            # E  .. .. ..   : U L L U
102
103         addq $16,8,$16          # .. .. .. E    :
104         bis $2,$2,$3            # .. .. E  ..   :
105         nop                     # .. E  .. ..   :
106         bne $4,$misquad         # U  .. .. ..   : U L U L
107
108         nop                     # .. .. .. E
109         nop                     # .. .. E  ..
110         nop                     # .. E  .. ..
111         beq $0,$zerolength      # U  .. .. ..   : U L U L
112
113 /* We know we have at least one trip through the byte loop */
114         EXI ( ldbu $2,0($17) )  # .. .. .. L    : No loads in the same quad
115         addq $16,1,$16          # .. .. E  ..   : as the store (Section 3.8 in CWG)
116         nop                     # .. E  .. ..   :
117         br $31, $dirtyentry     # L0 .. .. ..   : L U U L
118 /* Do the trailing byte loop load, then hop into the store part of the loop */
119
120 /*
121  * A minimum of (33 - 7) bytes to do a quad at a time.
122  * Based upon the usage context, it's worth the effort to unroll this loop
123  * $0 - number of bytes to be moved
124  * $4 - number of bytes to move as quadwords
125  * $16 is current destination address
126  * $17 is current source address
127  */
128 $quadaligned:
129         subq    $4, 32, $2      # .. .. .. E    : do not unroll for small stuff
130         nop                     # .. .. E  ..
131         nop                     # .. E  .. ..
132         blt     $2, $onequad    # U  .. .. ..   : U L U L
133
134 /*
135  * There is a significant assumption here that the source and destination
136  * addresses differ by more than 32 bytes.  In this particular case, a
137  * sparsity of registers further bounds this to be a minimum of 8 bytes.
138  * But if this isn't met, then the output result will be incorrect.
139  * Furthermore, due to a lack of available registers, we really can't
140  * unroll this to be an 8x loop (which would enable us to use the wh64
141  * instruction memory hint instruction).
142  */
143 $unroll4:
144         EXI( ldq $1,0($17) )    # .. .. .. L
145         EXI( ldq $2,8($17) )    # .. .. L  ..
146         subq    $4,32,$4        # .. E  .. ..
147         nop                     # E  .. .. ..   : U U L L
148
149         addq    $17,16,$17      # .. .. .. E
150         EXO( stq $1,0($16) )    # .. .. L  ..
151         EXO( stq $2,8($16) )    # .. L  .. ..
152         subq    $0,16,$0        # E  .. .. ..   : U L L U
153
154         addq    $16,16,$16      # .. .. .. E
155         EXI( ldq $1,0($17) )    # .. .. L  ..
156         EXI( ldq $2,8($17) )    # .. L  .. ..
157         subq    $4, 32, $3      # E  .. .. ..   : U U L L : is there enough for another trip?
158
159         EXO( stq $1,0($16) )    # .. .. .. L
160         EXO( stq $2,8($16) )    # .. .. L  ..
161         subq    $0,16,$0        # .. E  .. ..
162         addq    $17,16,$17      # E  .. .. ..   : U L L U
163
164         nop                     # .. .. .. E
165         nop                     # .. .. E  ..
166         addq    $16,16,$16      # .. E  .. ..
167         bgt     $3,$unroll4     # U  .. .. ..   : U L U L
168
169         nop
170         nop
171         nop
172         beq     $4, $noquads
173
174 $onequad:
175         EXI( ldq $1,0($17) )
176         subq    $4,8,$4
177         addq    $17,8,$17
178         nop
179
180         EXO( stq $1,0($16) )
181         subq    $0,8,$0
182         addq    $16,8,$16
183         bne     $4,$onequad
184
185 $noquads:
186         nop
187         nop
188         nop
189         beq $0,$zerolength
190
191 /*
192  * For small copies (or the tail of a larger copy), do a very simple byte loop.
193  * There's no point in doing a lot of complex alignment calculations to try to
194  * to quadword stuff for a small amount of data.
195  *      $0 - remaining number of bytes left to copy
196  *      $16 - current dest addr
197  *      $17 - current source addr
198  */
199
200 $onebyteloop:
201         EXI ( ldbu $2,0($17) )  # .. .. .. L    : No loads in the same quad
202         addq $16,1,$16          # .. .. E  ..   : as the store (Section 3.8 in CWG)
203         nop                     # .. E  .. ..   :
204         nop                     # E  .. .. ..   : U L U L
205
206 $dirtyentry:
207 /*
208  * the -1 is to compensate for the inc($16) done in a previous quadpack
209  * which allows us zero dependencies within either quadpack in the loop
210  */
211         EXO ( stb $2,-1($16) )  # .. .. .. L    :
212         addq $17,1,$17          # .. .. E  ..   : quadpack as the load
213         subq $0,1,$0            # .. E  .. ..   : change count _after_ copy
214         bgt $0,$onebyteloop     # U  .. .. ..   : U L U L
215
216 $zerolength:
217 $exitin:
218 $exitout:                       # Destination for exception recovery(?)
219         nop                     # .. .. .. E
220         nop                     # .. .. E  ..
221         nop                     # .. E  .. ..
222         ret $31,($26),1         # L0 .. .. ..   : L U L U
223
224         .end __copy_user
225         EXPORT_SYMBOL(__copy_user)