hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 *    User Space Access Routines
 *
 *    Copyright (C) 2000-2002 Hewlett-Packard (John Marvin)
 *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
 *    Copyright (C) 2001 Matthieu Delahaye <delahaym at esiee.fr>
 *    Copyright (C) 2003 Randolph Chung <tausq with parisc-linux.org>
 *    Copyright (C) 2017 Helge Deller <deller@gmx.de>
 *    Copyright (C) 2017 John David Anglin <dave.anglin@bell.net>
 */
 
/*
 * These routines still have plenty of room for optimization
 * (word & doubleword load/store, dual issue, store hints, etc.).
 */
 
/*
 * The following routines assume that space register 3 (sr3) contains
 * the space id associated with the current users address space.
 */
 
 
   .text
   
#include <asm/assembly.h>
#include <asm/errno.h>
#include <linux/linkage.h>
 
   /*
    * get_sr gets the appropriate space value into
    * sr1 for kernel/user space access, depending
    * on the flag stored in the task structure.
    */
 
   .macro  get_sr
   mfctl       %cr30,%r1
   ldw         TI_SEGMENT(%r1),%r22
   mfsp        %sr3,%r1
   or,<>       %r22,%r0,%r0
   copy        %r0,%r1
   mtsp        %r1,%sr1
   .endm
 
   /*
    * unsigned long lclear_user(void *to, unsigned long n)
    *
    * Returns 0 for success.
    * otherwise, returns number of bytes not transferred.
    */
 
ENTRY_CFI(lclear_user)
   comib,=,n   0,%r25,$lclu_done
   get_sr
$lclu_loop:
   addib,<>    -1,%r25,$lclu_loop
1:      stbs,ma     %r0,1(%sr1,%r26)
 
$lclu_done:
   bv          %r0(%r2)
   copy        %r25,%r28
 
2:    b           $lclu_done
   ldo         1(%r25),%r25
 
   ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
ENDPROC_CFI(lclear_user)
 
 
   /*
    * long lstrnlen_user(char *s, long n)
    *
    * Returns 0 if exception before zero byte or reaching N,
    *         N+1 if N would be exceeded,
    *         else strlen + 1 (i.e. includes zero byte).
    */
 
ENTRY_CFI(lstrnlen_user)
   comib,=     0,%r25,$lslen_nzero
   copy        %r26,%r24
   get_sr
1:      ldbs,ma     1(%sr1,%r26),%r1
$lslen_loop:
   comib,=,n   0,%r1,$lslen_done
   addib,<>    -1,%r25,$lslen_loop
2:      ldbs,ma     1(%sr1,%r26),%r1
$lslen_done:
   bv          %r0(%r2)
   sub        %r26,%r24,%r28
 
$lslen_nzero:
   b           $lslen_done
   ldo         1(%r26),%r26 /* special case for N == 0 */
 
3:      b        $lslen_done
   copy        %r24,%r26    /* reset r26 so 0 is returned on fault */
 
   ASM_EXCEPTIONTABLE_ENTRY(1b,3b)
   ASM_EXCEPTIONTABLE_ENTRY(2b,3b)
 
ENDPROC_CFI(lstrnlen_user)
 
 
/*
 * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
 *
 * Inputs:
 * - sr1 already contains space of source region
 * - sr2 already contains space of destination region
 *
 * Returns:
 * - number of bytes that could not be copied.
 *   On success, this will be zero.
 *
 * This code is based on a C-implementation of a copy routine written by
 * Randolph Chung, which in turn was derived from the glibc.
 *
 * Several strategies are tried to try to get the best performance for various
 * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
 * at a time using general registers.  Unaligned copies are handled either by
 * aligning the destination and then using shift-and-write method, or in a few
 * cases by falling back to a byte-at-a-time copy.
 *
 * Testing with various alignments and buffer sizes shows that this code is
 * often >10x faster than a simple byte-at-a-time copy, even for strangely
 * aligned operands. It is interesting to note that the glibc version of memcpy
 * (written in C) is actually quite fast already. This routine is able to beat
 * it by 30-40% for aligned copies because of the loop unrolling, but in some
 * cases the glibc version is still slightly faster. This lends more
 * credibility that gcc can generate very good code as long as we are careful.
 *
 * Possible optimizations:
 * - add cache prefetching
 * - try not to use the post-increment address modifiers; they may create
 *   additional interlocks. Assumption is that those were only efficient on old
 *   machines (pre PA8000 processors)
 */
 
   dst = arg0
   src = arg1
   len = arg2
   end = arg3
   t1  = r19
   t2  = r20
   t3  = r21
   t4  = r22
   srcspc = sr1
   dstspc = sr2
 
   t0 = r1
   a1 = t1
   a2 = t2
   a3 = t3
   a0 = t4
 
   save_src = ret0
   save_dst = ret1
   save_len = r31
 
ENTRY_CFI(pa_memcpy)
   /* Last destination address */
   add    dst,len,end
 
   /* short copy with less than 16 bytes? */
   cmpib,COND(>>=),n 15,len,.Lbyte_loop
 
   /* same alignment? */
   xor    src,dst,t0
   extru    t0,31,2,t1
   cmpib,<>,n  0,t1,.Lunaligned_copy
 
#ifdef CONFIG_64BIT
   /* only do 64-bit copies if we can get aligned. */
   extru    t0,31,3,t1
   cmpib,<>,n  0,t1,.Lalign_loop32
 
   /* loop until we are 64-bit aligned */
.Lalign_loop64:
   extru    dst,31,3,t1
   cmpib,=,n    0,t1,.Lcopy_loop_16_start
20:    ldb,ma    1(srcspc,src),t1
21:    stb,ma    t1,1(dstspc,dst)
   b    .Lalign_loop64
   ldo    -1(len),len
 
   ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
 
.Lcopy_loop_16_start:
   ldi    31,t0
.Lcopy_loop_16:
   cmpb,COND(>>=),n t0,len,.Lword_loop
 
10:    ldd    0(srcspc,src),t1
11:    ldd    8(srcspc,src),t2
   ldo    16(src),src
12:    std,ma    t1,8(dstspc,dst)
13:    std,ma    t2,8(dstspc,dst)
14:    ldd    0(srcspc,src),t1
15:    ldd    8(srcspc,src),t2
   ldo    16(src),src
16:    std,ma    t1,8(dstspc,dst)
17:    std,ma    t2,8(dstspc,dst)
 
   ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
   ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
   ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
 
   b    .Lcopy_loop_16
   ldo    -32(len),len
 
.Lword_loop:
   cmpib,COND(>>=),n 3,len,.Lbyte_loop
20:    ldw,ma    4(srcspc,src),t1
21:    stw,ma    t1,4(dstspc,dst)
   b    .Lword_loop
   ldo    -4(len),len
 
   ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
 
#endif /* CONFIG_64BIT */
 
   /* loop until we are 32-bit aligned */
.Lalign_loop32:
   extru    dst,31,2,t1
   cmpib,=,n    0,t1,.Lcopy_loop_8
20:    ldb,ma    1(srcspc,src),t1
21:    stb,ma    t1,1(dstspc,dst)
   b    .Lalign_loop32
   ldo    -1(len),len
 
   ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
 
 
.Lcopy_loop_8:
   cmpib,COND(>>=),n 15,len,.Lbyte_loop
 
10:    ldw    0(srcspc,src),t1
11:    ldw    4(srcspc,src),t2
12:    stw,ma    t1,4(dstspc,dst)
13:    stw,ma    t2,4(dstspc,dst)
14:    ldw    8(srcspc,src),t1
15:    ldw    12(srcspc,src),t2
   ldo    16(src),src
16:    stw,ma    t1,4(dstspc,dst)
17:    stw,ma    t2,4(dstspc,dst)
 
   ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
   ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
   ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
 
   b    .Lcopy_loop_8
   ldo    -16(len),len
 
.Lbyte_loop:
   cmpclr,COND(<>) len,%r0,%r0
   b,n    .Lcopy_done
20:    ldb    0(srcspc,src),t1
   ldo    1(src),src
21:    stb,ma    t1,1(dstspc,dst)
   b    .Lbyte_loop
   ldo    -1(len),len
 
   ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
 
.Lcopy_done:
   bv    %r0(%r2)
   sub    end,dst,ret0
 
 
   /* src and dst are not aligned the same way. */
   /* need to go the hard way */
.Lunaligned_copy:
   /* align until dst is 32bit-word-aligned */
   extru    dst,31,2,t1
   cmpib,=,n    0,t1,.Lcopy_dstaligned
20:    ldb    0(srcspc,src),t1
   ldo    1(src),src
21:    stb,ma    t1,1(dstspc,dst)
   b    .Lunaligned_copy
   ldo    -1(len),len
 
   ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
   ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
 
.Lcopy_dstaligned:
 
   /* store src, dst and len in safe place */
   copy    src,save_src
   copy    dst,save_dst
   copy    len,save_len
 
   /* len now needs give number of words to copy */
   SHRREG    len,2,len
 
   /*
    * Copy from a not-aligned src to an aligned dst using shifts.
    * Handles 4 words per loop.
    */
 
   depw,z src,28,2,t0
   subi 32,t0,t0
   mtsar t0
   extru len,31,2,t0
   cmpib,= 2,t0,.Lcase2
   /* Make src aligned by rounding it down.  */
   depi 0,31,2,src
 
   cmpiclr,<> 3,t0,%r0
   b,n .Lcase3
   cmpiclr,<> 1,t0,%r0
   b,n .Lcase1
.Lcase0:
   cmpb,COND(=) %r0,len,.Lcda_finish
   nop
 
1:    ldw,ma 4(srcspc,src), a3
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
1:    ldw,ma 4(srcspc,src), a0
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   b,n .Ldo3
.Lcase1:
1:    ldw,ma 4(srcspc,src), a2
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
1:    ldw,ma 4(srcspc,src), a3
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   ldo -1(len),len
   cmpb,COND(=),n %r0,len,.Ldo0
.Ldo4:
1:    ldw,ma 4(srcspc,src), a0
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   shrpw a2, a3, %sar, t0
1:    stw,ma t0, 4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
.Ldo3:
1:    ldw,ma 4(srcspc,src), a1
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   shrpw a3, a0, %sar, t0
1:    stw,ma t0, 4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
.Ldo2:
1:    ldw,ma 4(srcspc,src), a2
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   shrpw a0, a1, %sar, t0
1:    stw,ma t0, 4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
.Ldo1:
1:    ldw,ma 4(srcspc,src), a3
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   shrpw a1, a2, %sar, t0
1:    stw,ma t0, 4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
   ldo -4(len),len
   cmpb,COND(<>) %r0,len,.Ldo4
   nop
.Ldo0:
   shrpw a2, a3, %sar, t0
1:    stw,ma t0, 4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
 
.Lcda_rdfault:
.Lcda_finish:
   /* calculate new src, dst and len and jump to byte-copy loop */
   sub    dst,save_dst,t0
   add    save_src,t0,src
   b    .Lbyte_loop
   sub    save_len,t0,len
 
.Lcase3:
1:    ldw,ma 4(srcspc,src), a0
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
1:    ldw,ma 4(srcspc,src), a1
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   b .Ldo2
   ldo 1(len),len
.Lcase2:
1:    ldw,ma 4(srcspc,src), a1
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
1:    ldw,ma 4(srcspc,src), a2
   ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
   b .Ldo1
   ldo 2(len),len
 
 
   /* fault exception fixup handlers: */
#ifdef CONFIG_64BIT
.Lcopy16_fault:
   b    .Lcopy_done
10:    std,ma    t1,8(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
#endif
 
.Lcopy8_fault:
   b    .Lcopy_done
10:    stw,ma    t1,4(dstspc,dst)
   ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
ENDPROC_CFI(pa_memcpy)
 
   .end