1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright(c) 2016-2020 Intel Corporation. All rights reserved. */
4 #include <linux/linkage.h>
5 #include <asm/copy_mc_test.h>
6 #include <asm/export.h>
15 * copy_mc_fragile - copy memory with indication if an exception / fault happened
17 * The 'fragile' version is opted into by platform quirks and takes
18 * pains to avoid unrecoverable corner cases like 'fast-string'
19 * instruction sequences, and consuming poison across a cacheline
20 * boundary. The non-fragile version is equivalent to memcpy()
21 * regardless of CPU machine-check-recovery capability.
23 SYM_FUNC_START(copy_mc_fragile)
25 /* Less than 8 bytes? Go to byte copy loop */
28 /* Check for bad alignment of source */
33 /* Copy one byte at a time until source is 8-byte aligned */
39 .L_read_leading_bytes:
41 COPY_MC_TEST_SRC %rsi 1 .E_leading_bytes
42 COPY_MC_TEST_DST %rdi 1 .E_leading_bytes
43 .L_write_leading_bytes:
48 jnz .L_read_leading_bytes
58 COPY_MC_TEST_SRC %rsi 8 .E_read_words
59 COPY_MC_TEST_DST %rdi 8 .E_write_words
67 /* Any trailing bytes? */
70 jz .L_done_memcpy_trap
72 /* Copy trailing bytes */
74 .L_read_trailing_bytes:
76 COPY_MC_TEST_SRC %rsi 1 .E_trailing_bytes
77 COPY_MC_TEST_DST %rdi 1 .E_trailing_bytes
78 .L_write_trailing_bytes:
83 jnz .L_read_trailing_bytes
85 /* Copy successful. Return zero */
90 SYM_FUNC_END(copy_mc_fragile)
91 EXPORT_SYMBOL_GPL(copy_mc_fragile)
95 * Return number of bytes not copied for any failure. Note that
96 * there is no "tail" handling since the source buffer is 8-byte
97 * aligned and poison is cacheline aligned.
108 * For write fault handling, given the destination is unaligned,
109 * we handle faults on multi-byte writes with a byte-by-byte
110 * copy up to the write-protected page.
116 jmp copy_mc_fragile_handle_tail
120 _ASM_EXTABLE_FAULT(.L_read_leading_bytes, .E_leading_bytes)
121 _ASM_EXTABLE_FAULT(.L_read_words, .E_read_words)
122 _ASM_EXTABLE_FAULT(.L_read_trailing_bytes, .E_trailing_bytes)
123 _ASM_EXTABLE(.L_write_leading_bytes, .E_leading_bytes)
124 _ASM_EXTABLE(.L_write_words, .E_write_words)
125 _ASM_EXTABLE(.L_write_trailing_bytes, .E_trailing_bytes)
126 #endif /* CONFIG_X86_MCE */
129 * copy_mc_enhanced_fast_string - memory copy with exception handling
131 * Fast string copy + fault / exception handling. If the CPU does
132 * support machine check exception recovery, but does not support
133 * recovering from fast-string exceptions then this CPU needs to be
134 * added to the copy_mc_fragile_key set of quirks. Otherwise, absent any
135 * machine check recovery support this version should be no slower than
138 SYM_FUNC_START(copy_mc_enhanced_fast_string)
143 /* Copy successful. Return zero */
146 SYM_FUNC_END(copy_mc_enhanced_fast_string)
148 .section .fixup, "ax"
151 * On fault %rcx is updated such that the copy instruction could
152 * optionally be restarted at the fault position, i.e. it
153 * contains 'bytes remaining'. A non-zero return indicates error
154 * to copy_mc_generic() users, or indicate short transfers to
155 * user-copy routines.
162 _ASM_EXTABLE_FAULT(.L_copy, .E_copy)
163 #endif /* !CONFIG_UML */