blob: 6410554039fd75b673468f1a23d955c4bcff3167 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Nicolas Pitre75494232005-11-01 19:52:23 +00002/*
3 * linux/arch/arm/lib/memmove.S
4 *
5 * Author: Nicolas Pitre
6 * Created: Sep 28, 2005
7 * Copyright: (C) MontaVista Software Inc.
Nicolas Pitre75494232005-11-01 19:52:23 +00008 */
9
10#include <linux/linkage.h>
11#include <asm/assembler.h>
Lin Yongting207a6cb2014-11-26 14:36:15 +010012#include <asm/unwind.h>
Nicolas Pitre75494232005-11-01 19:52:23 +000013
Nicolas Pitre75494232005-11-01 19:52:23 +000014 .text
15
16/*
17 * Prototype: void *memmove(void *dest, const void *src, size_t n);
18 *
19 * Note:
20 *
21 * If the memory regions don't overlap, we simply branch to memcpy which is
22 * normally a bit faster. Otherwise the copy is done going downwards. This
23 * is a transposition of the code from copy_template.S but with the copy
24 * occurring in the opposite direction.
25 */
26
Linus Walleijd6d51a92020-10-25 23:52:08 +010027ENTRY(__memmove)
Fangrui Song735e8d92020-11-06 21:46:11 +010028WEAK(memmove)
Lin Yongting207a6cb2014-11-26 14:36:15 +010029 UNWIND( .fnstart )
Nicolas Pitre75494232005-11-01 19:52:23 +000030
31 subs ip, r0, r1
32 cmphi r2, ip
Linus Walleijd6d51a92020-10-25 23:52:08 +010033 bls __memcpy
Lin Yongting207a6cb2014-11-26 14:36:15 +010034 UNWIND( .fnend )
35
36 UNWIND( .fnstart )
Ard Biesheuvelccb81602021-10-04 11:29:11 +020037 UNWIND( .save {r0, r4, fpreg, lr} )
38 stmfd sp!, {r0, r4, UNWIND(fpreg,) lr}
39 UNWIND( .setfp fpreg, sp )
40 UNWIND( mov fpreg, sp )
Nicolas Pitre75494232005-11-01 19:52:23 +000041 add r1, r1, r2
42 add r0, r0, r2
43 subs r2, r2, #4
44 blt 8f
45 ands ip, r0, #3
46 PLD( pld [r1, #-4] )
47 bne 9f
48 ands ip, r1, #3
49 bne 10f
50
511: subs r2, r2, #(28)
Ard Biesheuvelccb81602021-10-04 11:29:11 +020052 stmfd sp!, {r5, r6, r8, r9}
Nicolas Pitre75494232005-11-01 19:52:23 +000053 blt 5f
54
Nicolas Pitre2239aff2008-03-31 12:38:31 -040055 CALGN( ands ip, r0, #31 )
Stefan Agnere44fc382019-02-18 00:57:38 +010056 CALGN( sbcsne r4, ip, r2 ) @ C is always set here
Nicolas Pitre75494232005-11-01 19:52:23 +000057 CALGN( bcs 2f )
58 CALGN( adr r4, 6f )
59 CALGN( subs r2, r2, ip ) @ C is set here
Nicolas Pitre4c4925c2008-06-11 12:40:13 -040060 CALGN( rsb ip, ip, #32 )
Nicolas Pitre75494232005-11-01 19:52:23 +000061 CALGN( add pc, r4, ip )
62
63 PLD( pld [r1, #-4] )
642: PLD( subs r2, r2, #96 )
65 PLD( pld [r1, #-32] )
66 PLD( blt 4f )
67 PLD( pld [r1, #-64] )
68 PLD( pld [r1, #-96] )
69
703: PLD( pld [r1, #-128] )
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200714: ldmdb r1!, {r3, r4, r5, r6, r8, r9, ip, lr}
Nicolas Pitre75494232005-11-01 19:52:23 +000072 subs r2, r2, #32
Ard Biesheuvelccb81602021-10-04 11:29:11 +020073 stmdb r0!, {r3, r4, r5, r6, r8, r9, ip, lr}
Nicolas Pitre75494232005-11-01 19:52:23 +000074 bge 3b
75 PLD( cmn r2, #96 )
76 PLD( bge 4b )
77
785: ands ip, r2, #28
79 rsb ip, ip, #32
80 addne pc, pc, ip @ C is always clear here
81 b 7f
Catalin Marinasfd522a82010-03-29 10:29:46 +0100826: W(nop)
Catalin Marinas8b592782009-07-24 12:32:57 +010083 W(ldr) r3, [r1, #-4]!
84 W(ldr) r4, [r1, #-4]!
85 W(ldr) r5, [r1, #-4]!
86 W(ldr) r6, [r1, #-4]!
Catalin Marinas8b592782009-07-24 12:32:57 +010087 W(ldr) r8, [r1, #-4]!
Ard Biesheuvelccb81602021-10-04 11:29:11 +020088 W(ldr) r9, [r1, #-4]!
Catalin Marinas8b592782009-07-24 12:32:57 +010089 W(ldr) lr, [r1, #-4]!
Nicolas Pitre75494232005-11-01 19:52:23 +000090
91 add pc, pc, ip
92 nop
Catalin Marinasfd522a82010-03-29 10:29:46 +010093 W(nop)
Catalin Marinas8b592782009-07-24 12:32:57 +010094 W(str) r3, [r0, #-4]!
95 W(str) r4, [r0, #-4]!
96 W(str) r5, [r0, #-4]!
97 W(str) r6, [r0, #-4]!
Catalin Marinas8b592782009-07-24 12:32:57 +010098 W(str) r8, [r0, #-4]!
Ard Biesheuvelccb81602021-10-04 11:29:11 +020099 W(str) r9, [r0, #-4]!
Catalin Marinas8b592782009-07-24 12:32:57 +0100100 W(str) lr, [r0, #-4]!
Nicolas Pitre75494232005-11-01 19:52:23 +0000101
102 CALGN( bcs 2b )
103
Ard Biesheuvelccb81602021-10-04 11:29:11 +02001047: ldmfd sp!, {r5, r6, r8, r9}
Nicolas Pitre75494232005-11-01 19:52:23 +0000105
1068: movs r2, r2, lsl #31
Stefan Agnere44fc382019-02-18 00:57:38 +0100107 ldrbne r3, [r1, #-1]!
108 ldrbcs r4, [r1, #-1]!
109 ldrbcs ip, [r1, #-1]
110 strbne r3, [r0, #-1]!
111 strbcs r4, [r0, #-1]!
112 strbcs ip, [r0, #-1]
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200113 ldmfd sp!, {r0, r4, UNWIND(fpreg,) pc}
Nicolas Pitre75494232005-11-01 19:52:23 +0000114
1159: cmp ip, #2
Stefan Agnere44fc382019-02-18 00:57:38 +0100116 ldrbgt r3, [r1, #-1]!
117 ldrbge r4, [r1, #-1]!
Nicolas Pitre75494232005-11-01 19:52:23 +0000118 ldrb lr, [r1, #-1]!
Stefan Agnere44fc382019-02-18 00:57:38 +0100119 strbgt r3, [r0, #-1]!
120 strbge r4, [r0, #-1]!
Nicolas Pitre75494232005-11-01 19:52:23 +0000121 subs r2, r2, ip
122 strb lr, [r0, #-1]!
123 blt 8b
124 ands ip, r1, #3
125 beq 1b
126
12710: bic r1, r1, #3
128 cmp ip, #2
129 ldr r3, [r1, #0]
130 beq 17f
131 blt 18f
132
133
134 .macro backward_copy_shift push pull
135
136 subs r2, r2, #28
137 blt 14f
138
Nicolas Pitre2239aff2008-03-31 12:38:31 -0400139 CALGN( ands ip, r0, #31 )
Stefan Agnere44fc382019-02-18 00:57:38 +0100140 CALGN( sbcsne r4, ip, r2 ) @ C is always set here
Nicolas Pitre75494232005-11-01 19:52:23 +0000141 CALGN( subcc r2, r2, ip )
142 CALGN( bcc 15f )
143
Ard Biesheuvelccb81602021-10-04 11:29:11 +020014411: stmfd sp!, {r5, r6, r8 - r10}
Nicolas Pitre75494232005-11-01 19:52:23 +0000145
146 PLD( pld [r1, #-4] )
147 PLD( subs r2, r2, #96 )
148 PLD( pld [r1, #-32] )
149 PLD( blt 13f )
150 PLD( pld [r1, #-64] )
151 PLD( pld [r1, #-96] )
152
15312: PLD( pld [r1, #-128] )
Ard Biesheuvelccb81602021-10-04 11:29:11 +020015413: ldmdb r1!, {r8, r9, r10, ip}
Victor Kamenskyd98b90e2014-02-25 08:41:09 +0100155 mov lr, r3, lspush #\push
Nicolas Pitre75494232005-11-01 19:52:23 +0000156 subs r2, r2, #32
157 ldmdb r1!, {r3, r4, r5, r6}
Victor Kamenskyd98b90e2014-02-25 08:41:09 +0100158 orr lr, lr, ip, lspull #\pull
159 mov ip, ip, lspush #\push
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200160 orr ip, ip, r10, lspull #\pull
161 mov r10, r10, lspush #\push
162 orr r10, r10, r9, lspull #\pull
Victor Kamenskyd98b90e2014-02-25 08:41:09 +0100163 mov r9, r9, lspush #\push
164 orr r9, r9, r8, lspull #\pull
165 mov r8, r8, lspush #\push
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200166 orr r8, r8, r6, lspull #\pull
Victor Kamenskyd98b90e2014-02-25 08:41:09 +0100167 mov r6, r6, lspush #\push
168 orr r6, r6, r5, lspull #\pull
169 mov r5, r5, lspush #\push
170 orr r5, r5, r4, lspull #\pull
171 mov r4, r4, lspush #\push
172 orr r4, r4, r3, lspull #\pull
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200173 stmdb r0!, {r4 - r6, r8 - r10, ip, lr}
Nicolas Pitre75494232005-11-01 19:52:23 +0000174 bge 12b
175 PLD( cmn r2, #96 )
176 PLD( bge 13b )
177
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200178 ldmfd sp!, {r5, r6, r8 - r10}
Nicolas Pitre75494232005-11-01 19:52:23 +0000179
18014: ands ip, r2, #28
181 beq 16f
182
Victor Kamenskyd98b90e2014-02-25 08:41:09 +010018315: mov lr, r3, lspush #\push
Nicolas Pitre75494232005-11-01 19:52:23 +0000184 ldr r3, [r1, #-4]!
185 subs ip, ip, #4
Victor Kamenskyd98b90e2014-02-25 08:41:09 +0100186 orr lr, lr, r3, lspull #\pull
Nicolas Pitre75494232005-11-01 19:52:23 +0000187 str lr, [r0, #-4]!
188 bgt 15b
189 CALGN( cmp r2, #0 )
190 CALGN( bge 11b )
191
19216: add r1, r1, #(\pull / 8)
193 b 8b
194
195 .endm
196
197
198 backward_copy_shift push=8 pull=24
199
20017: backward_copy_shift push=16 pull=16
201
20218: backward_copy_shift push=24 pull=8
203
Ard Biesheuvelccb81602021-10-04 11:29:11 +0200204 UNWIND( .fnend )
Catalin Marinas93ed3972008-08-28 11:22:32 +0100205ENDPROC(memmove)
Linus Walleijd6d51a92020-10-25 23:52:08 +0100206ENDPROC(__memmove)