33#include <linux/linkage.h>
44#include <asm/errno.h>
55#include <asm/cpufeatures.h>
6+ #include <asm/mcsafe_test.h>
67#include <asm/alternative-asm.h>
78#include <asm/export.h>
89
@@ -183,12 +184,15 @@ ENTRY(memcpy_orig)
183184ENDPROC(memcpy_orig)
184185
185186#ifndef CONFIG_UML
187+
188+ MCSAFE_TEST_CTL
189+
186190/*
187- * memcpy_mcsafe_unrolled - memory copy with machine check exception handling
191+ * __memcpy_mcsafe - memory copy with machine check exception handling
188192 * Note that we only catch machine checks when reading the source addresses.
189193 * Writes to target are posted and don't generate machine checks.
190194 */
191- ENTRY(memcpy_mcsafe_unrolled )
195+ ENTRY(__memcpy_mcsafe )
192196 cmpl $8 , %edx
193197 /* Less than 8 bytes? Go to byte copy loop */
194198 jb .L_no_whole_words
@@ -204,58 +208,33 @@ ENTRY(memcpy_mcsafe_unrolled)
204208 subl $8 , %ecx
205209 negl %ecx
206210 subl %ecx , %edx
207- .L_copy_leading_bytes :
211+ .L_read_leading_bytes :
208212 movb (%rsi ), %al
213+ MCSAFE_TEST_SRC %rsi 1 .E_leading_bytes
214+ MCSAFE_TEST_DST %rdi 1 .E_leading_bytes
215+ .L_write_leading_bytes:
209216 movb %al , (%rdi )
210217 incq %rsi
211218 incq %rdi
212219 decl %ecx
213- jnz .L_copy_leading_bytes
220+ jnz .L_read_leading_bytes
214221
215222.L_8byte_aligned:
216- /* Figure out how many whole cache lines (64-bytes) to copy */
217- movl %edx , %ecx
218- andl $63 , %edx
219- shrl $6 , %ecx
220- jz .L_no_whole_cache_lines
221-
222- /* Loop copying whole cache lines */
223- .L_cache_w0: movq (%rsi ), %r8
224- .L_cache_w1: movq 1*8 (%rsi ), %r9
225- .L_cache_w2: movq 2*8 (%rsi ), %r10
226- .L_cache_w3: movq 3*8 (%rsi ), %r11
227- movq %r8 , (%rdi )
228- movq %r9 , 1*8 (%rdi )
229- movq %r10 , 2*8 (%rdi )
230- movq %r11 , 3*8 (%rdi )
231- .L_cache_w4: movq 4*8 (%rsi ), %r8
232- .L_cache_w5: movq 5*8 (%rsi ), %r9
233- .L_cache_w6: movq 6*8 (%rsi ), %r10
234- .L_cache_w7: movq 7*8 (%rsi ), %r11
235- movq %r8 , 4*8 (%rdi )
236- movq %r9 , 5*8 (%rdi )
237- movq %r10 , 6*8 (%rdi )
238- movq %r11 , 7*8 (%rdi )
239- leaq 64 (%rsi ), %rsi
240- leaq 64 (%rdi ), %rdi
241- decl %ecx
242- jnz .L_cache_w0
243-
244- /* Are there any trailing 8-byte words? */
245- .L_no_whole_cache_lines:
246223 movl %edx , %ecx
247224 andl $7 , %edx
248225 shrl $3 , %ecx
249226 jz .L_no_whole_words
250227
251- /* Copy trailing words */
252- .L_copy_trailing_words:
228+ .L_read_words:
253229 movq (%rsi ), %r8
254- mov %r8 , (%rdi )
255- leaq 8 (%rsi ), %rsi
256- leaq 8 (%rdi ), %rdi
230+ MCSAFE_TEST_SRC %rsi 8 .E_read_words
231+ MCSAFE_TEST_DST %rdi 8 .E_write_words
232+ .L_write_words:
233+ movq %r8 , (%rdi )
234+ addq $8 , %rsi
235+ addq $8 , %rdi
257236 decl %ecx
258- jnz .L_copy_trailing_words
237+ jnz .L_read_words
259238
260239 /* Any trailing bytes? */
261240.L_no_whole_words:
@@ -264,38 +243,55 @@ ENTRY(memcpy_mcsafe_unrolled)
264243
265244 /* Copy trailing bytes */
266245 movl %edx , %ecx
267- .L_copy_trailing_bytes :
246+ .L_read_trailing_bytes :
268247 movb (%rsi ), %al
248+ MCSAFE_TEST_SRC %rsi 1 .E_trailing_bytes
249+ MCSAFE_TEST_DST %rdi 1 .E_trailing_bytes
250+ .L_write_trailing_bytes:
269251 movb %al , (%rdi )
270252 incq %rsi
271253 incq %rdi
272254 decl %ecx
273- jnz .L_copy_trailing_bytes
255+ jnz .L_read_trailing_bytes
274256
275257 /* Copy successful. Return zero */
276258.L_done_memcpy_trap:
277259 xorq %rax , %rax
278260 ret
279- ENDPROC(memcpy_mcsafe_unrolled )
280- EXPORT_SYMBOL_GPL(memcpy_mcsafe_unrolled )
261+ ENDPROC(__memcpy_mcsafe )
262+ EXPORT_SYMBOL_GPL(__memcpy_mcsafe )
281263
282264 .section .fixup, "ax"
283- /* Return -EFAULT for any failure */
284- .L_memcpy_mcsafe_fail:
285- mov $-EFAULT, %rax
265+ /*
266+ * Return number of bytes not copied for any failure. Note that
267+ * there is no "tail" handling since the source buffer is 8-byte
268+ * aligned and poison is cacheline aligned.
269+ */
270+ .E_read_words:
271+ shll $3 , %ecx
272+ .E_leading_bytes:
273+ addl %edx , %ecx
274+ .E_trailing_bytes:
275+ mov %ecx , %eax
286276 ret
287277
278+ /*
279+ * For write fault handling, given the destination is unaligned,
280+ * we handle faults on multi-byte writes with a byte-by-byte
281+ * copy up to the write-protected page.
282+ */
283+ .E_write_words:
284+ shll $3 , %ecx
285+ addl %edx , %ecx
286+ movl %ecx , %edx
287+ jmp mcsafe_handle_tail
288+
288289 .previous
289290
290- _ASM_EXTABLE_FAULT(.L_copy_leading_bytes, .L_memcpy_mcsafe_fail)
291- _ASM_EXTABLE_FAULT(.L_cache_w0, .L_memcpy_mcsafe_fail)
292- _ASM_EXTABLE_FAULT(.L_cache_w1, .L_memcpy_mcsafe_fail)
293- _ASM_EXTABLE_FAULT(.L_cache_w2, .L_memcpy_mcsafe_fail)
294- _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
295- _ASM_EXTABLE_FAULT(.L_cache_w4, .L_memcpy_mcsafe_fail)
296- _ASM_EXTABLE_FAULT(.L_cache_w5, .L_memcpy_mcsafe_fail)
297- _ASM_EXTABLE_FAULT(.L_cache_w6, .L_memcpy_mcsafe_fail)
298- _ASM_EXTABLE_FAULT(.L_cache_w7, .L_memcpy_mcsafe_fail)
299- _ASM_EXTABLE_FAULT(.L_copy_trailing_words, .L_memcpy_mcsafe_fail)
300- _ASM_EXTABLE_FAULT(.L_copy_trailing_bytes, .L_memcpy_mcsafe_fail)
291+ _ASM_EXTABLE_FAULT(.L_read_leading_bytes, .E_leading_bytes)
292+ _ASM_EXTABLE_FAULT(.L_read_words, .E_read_words)
293+ _ASM_EXTABLE_FAULT(.L_read_trailing_bytes, .E_trailing_bytes)
294+ _ASM_EXTABLE(.L_write_leading_bytes, .E_leading_bytes)
295+ _ASM_EXTABLE(.L_write_words, .E_write_words)
296+ _ASM_EXTABLE(.L_write_trailing_bytes, .E_trailing_bytes)
301297#endif
0 commit comments