xref: /openbmc/linux/arch/x86/lib/copy_mc.c (revision 5da8e4a6)
1ec6347bbSDan Williams // SPDX-License-Identifier: GPL-2.0
2ec6347bbSDan Williams /* Copyright(c) 2016-2020 Intel Corporation. All rights reserved. */
3ec6347bbSDan Williams 
4ec6347bbSDan Williams #include <linux/jump_label.h>
5ec6347bbSDan Williams #include <linux/uaccess.h>
6ec6347bbSDan Williams #include <linux/export.h>
7ec6347bbSDan Williams #include <linux/string.h>
8ec6347bbSDan Williams #include <linux/types.h>
9ec6347bbSDan Williams 
10ec6347bbSDan Williams #include <asm/mce.h>
11ec6347bbSDan Williams 
12ec6347bbSDan Williams #ifdef CONFIG_X86_MCE
13ec6347bbSDan Williams /*
14ec6347bbSDan Williams  * See COPY_MC_TEST for self-test of the copy_mc_fragile()
15ec6347bbSDan Williams  * implementation.
16ec6347bbSDan Williams  */
17ec6347bbSDan Williams static DEFINE_STATIC_KEY_FALSE(copy_mc_fragile_key);
18ec6347bbSDan Williams 
19ec6347bbSDan Williams void enable_copy_mc_fragile(void)
20ec6347bbSDan Williams {
21ec6347bbSDan Williams 	static_branch_inc(&copy_mc_fragile_key);
22ec6347bbSDan Williams }
23ec6347bbSDan Williams #define copy_mc_fragile_enabled (static_branch_unlikely(&copy_mc_fragile_key))
24ec6347bbSDan Williams 
25ec6347bbSDan Williams /*
26ec6347bbSDan Williams  * Similar to copy_user_handle_tail, probe for the write fault point, or
27ec6347bbSDan Williams  * source exception point.
28ec6347bbSDan Williams  */
29ec6347bbSDan Williams __visible notrace unsigned long
30ec6347bbSDan Williams copy_mc_fragile_handle_tail(char *to, char *from, unsigned len)
31ec6347bbSDan Williams {
32ec6347bbSDan Williams 	for (; len; --len, to++, from++)
33ec6347bbSDan Williams 		if (copy_mc_fragile(to, from, 1))
34ec6347bbSDan Williams 			break;
35ec6347bbSDan Williams 	return len;
36ec6347bbSDan Williams }
37ec6347bbSDan Williams #else
38ec6347bbSDan Williams /*
39ec6347bbSDan Williams  * No point in doing careful copying, or consulting a static key when
40ec6347bbSDan Williams  * there is no #MC handler in the CONFIG_X86_MCE=n case.
41ec6347bbSDan Williams  */
42ec6347bbSDan Williams void enable_copy_mc_fragile(void)
43ec6347bbSDan Williams {
44ec6347bbSDan Williams }
45ec6347bbSDan Williams #define copy_mc_fragile_enabled (0)
46ec6347bbSDan Williams #endif
47ec6347bbSDan Williams 
485da8e4a6SDan Williams unsigned long copy_mc_enhanced_fast_string(void *dst, const void *src, unsigned len);
495da8e4a6SDan Williams 
50ec6347bbSDan Williams /**
51ec6347bbSDan Williams  * copy_mc_to_kernel - memory copy that handles source exceptions
52ec6347bbSDan Williams  *
53ec6347bbSDan Williams  * @dst:	destination address
54ec6347bbSDan Williams  * @src:	source address
55ec6347bbSDan Williams  * @len:	number of bytes to copy
56ec6347bbSDan Williams  *
575da8e4a6SDan Williams  * Call into the 'fragile' version on systems that benefit from avoiding
585da8e4a6SDan Williams  * corner case poison consumption scenarios, For example, accessing
595da8e4a6SDan Williams  * poison across 2 cachelines with a single instruction. Almost all
605da8e4a6SDan Williams  * other uses case can use copy_mc_enhanced_fast_string() for a fast
615da8e4a6SDan Williams  * recoverable copy, or fallback to plain memcpy.
62ec6347bbSDan Williams  *
63ec6347bbSDan Williams  * Return 0 for success, or number of bytes not copied if there was an
64ec6347bbSDan Williams  * exception.
65ec6347bbSDan Williams  */
66ec6347bbSDan Williams unsigned long __must_check copy_mc_to_kernel(void *dst, const void *src, unsigned len)
67ec6347bbSDan Williams {
68ec6347bbSDan Williams 	if (copy_mc_fragile_enabled)
69ec6347bbSDan Williams 		return copy_mc_fragile(dst, src, len);
705da8e4a6SDan Williams 	if (static_cpu_has(X86_FEATURE_ERMS))
715da8e4a6SDan Williams 		return copy_mc_enhanced_fast_string(dst, src, len);
72ec6347bbSDan Williams 	memcpy(dst, src, len);
73ec6347bbSDan Williams 	return 0;
74ec6347bbSDan Williams }
75ec6347bbSDan Williams EXPORT_SYMBOL_GPL(copy_mc_to_kernel);
76ec6347bbSDan Williams 
77ec6347bbSDan Williams unsigned long __must_check copy_mc_to_user(void *dst, const void *src, unsigned len)
78ec6347bbSDan Williams {
79ec6347bbSDan Williams 	unsigned long ret;
80ec6347bbSDan Williams 
815da8e4a6SDan Williams 	if (copy_mc_fragile_enabled) {
82ec6347bbSDan Williams 		__uaccess_begin();
83ec6347bbSDan Williams 		ret = copy_mc_fragile(dst, src, len);
84ec6347bbSDan Williams 		__uaccess_end();
85ec6347bbSDan Williams 		return ret;
86ec6347bbSDan Williams 	}
875da8e4a6SDan Williams 
885da8e4a6SDan Williams 	if (static_cpu_has(X86_FEATURE_ERMS)) {
895da8e4a6SDan Williams 		__uaccess_begin();
905da8e4a6SDan Williams 		ret = copy_mc_enhanced_fast_string(dst, src, len);
915da8e4a6SDan Williams 		__uaccess_end();
925da8e4a6SDan Williams 		return ret;
935da8e4a6SDan Williams 	}
945da8e4a6SDan Williams 
955da8e4a6SDan Williams 	return copy_user_generic(dst, src, len);
965da8e4a6SDan Williams }
97