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