xref: /openbmc/linux/arch/x86/lib/copy_mc.c (revision d73de815)
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 static DEFINE_STATIC_KEY_FALSE(copy_mc_fragile_key);
14ec6347bbSDan Williams 
enable_copy_mc_fragile(void)15ec6347bbSDan Williams void enable_copy_mc_fragile(void)
16ec6347bbSDan Williams {
17ec6347bbSDan Williams 	static_branch_inc(&copy_mc_fragile_key);
18ec6347bbSDan Williams }
19ec6347bbSDan Williams #define copy_mc_fragile_enabled (static_branch_unlikely(&copy_mc_fragile_key))
20ec6347bbSDan Williams 
21ec6347bbSDan Williams /*
22ec6347bbSDan Williams  * Similar to copy_user_handle_tail, probe for the write fault point, or
23ec6347bbSDan Williams  * source exception point.
24ec6347bbSDan Williams  */
25ec6347bbSDan Williams __visible notrace unsigned long
copy_mc_fragile_handle_tail(char * to,char * from,unsigned len)26ec6347bbSDan Williams copy_mc_fragile_handle_tail(char *to, char *from, unsigned len)
27ec6347bbSDan Williams {
28ec6347bbSDan Williams 	for (; len; --len, to++, from++)
29ec6347bbSDan Williams 		if (copy_mc_fragile(to, from, 1))
30ec6347bbSDan Williams 			break;
31ec6347bbSDan Williams 	return len;
32ec6347bbSDan Williams }
33ec6347bbSDan Williams #else
34ec6347bbSDan Williams /*
35ec6347bbSDan Williams  * No point in doing careful copying, or consulting a static key when
36ec6347bbSDan Williams  * there is no #MC handler in the CONFIG_X86_MCE=n case.
37ec6347bbSDan Williams  */
enable_copy_mc_fragile(void)38ec6347bbSDan Williams void enable_copy_mc_fragile(void)
39ec6347bbSDan Williams {
40ec6347bbSDan Williams }
41ec6347bbSDan Williams #define copy_mc_fragile_enabled (0)
42ec6347bbSDan Williams #endif
43ec6347bbSDan Williams 
445da8e4a6SDan Williams unsigned long copy_mc_enhanced_fast_string(void *dst, const void *src, unsigned len);
455da8e4a6SDan Williams 
46ec6347bbSDan Williams /**
47ec6347bbSDan Williams  * copy_mc_to_kernel - memory copy that handles source exceptions
48ec6347bbSDan Williams  *
49ec6347bbSDan Williams  * @dst:	destination address
50ec6347bbSDan Williams  * @src:	source address
51ec6347bbSDan Williams  * @len:	number of bytes to copy
52ec6347bbSDan Williams  *
535da8e4a6SDan Williams  * Call into the 'fragile' version on systems that benefit from avoiding
545da8e4a6SDan Williams  * corner case poison consumption scenarios, For example, accessing
555da8e4a6SDan Williams  * poison across 2 cachelines with a single instruction. Almost all
565da8e4a6SDan Williams  * other uses case can use copy_mc_enhanced_fast_string() for a fast
575da8e4a6SDan Williams  * recoverable copy, or fallback to plain memcpy.
58ec6347bbSDan Williams  *
59ec6347bbSDan Williams  * Return 0 for success, or number of bytes not copied if there was an
60ec6347bbSDan Williams  * exception.
61ec6347bbSDan Williams  */
copy_mc_to_kernel(void * dst,const void * src,unsigned len)62ec6347bbSDan Williams unsigned long __must_check copy_mc_to_kernel(void *dst, const void *src, unsigned len)
63ec6347bbSDan Williams {
64ec6347bbSDan Williams 	if (copy_mc_fragile_enabled)
65ec6347bbSDan Williams 		return copy_mc_fragile(dst, src, len);
665da8e4a6SDan Williams 	if (static_cpu_has(X86_FEATURE_ERMS))
675da8e4a6SDan Williams 		return copy_mc_enhanced_fast_string(dst, src, len);
68ec6347bbSDan Williams 	memcpy(dst, src, len);
69ec6347bbSDan Williams 	return 0;
70ec6347bbSDan Williams }
71ec6347bbSDan Williams EXPORT_SYMBOL_GPL(copy_mc_to_kernel);
72ec6347bbSDan Williams 
copy_mc_to_user(void __user * dst,const void * src,unsigned len)73*d73de815SDavid Howells unsigned long __must_check copy_mc_to_user(void __user *dst, const void *src, unsigned len)
74ec6347bbSDan Williams {
75ec6347bbSDan Williams 	unsigned long ret;
76ec6347bbSDan Williams 
775da8e4a6SDan Williams 	if (copy_mc_fragile_enabled) {
78ec6347bbSDan Williams 		__uaccess_begin();
79*d73de815SDavid Howells 		ret = copy_mc_fragile((__force void *)dst, src, len);
80ec6347bbSDan Williams 		__uaccess_end();
81ec6347bbSDan Williams 		return ret;
82ec6347bbSDan Williams 	}
835da8e4a6SDan Williams 
845da8e4a6SDan Williams 	if (static_cpu_has(X86_FEATURE_ERMS)) {
855da8e4a6SDan Williams 		__uaccess_begin();
86*d73de815SDavid Howells 		ret = copy_mc_enhanced_fast_string((__force void *)dst, src, len);
875da8e4a6SDan Williams 		__uaccess_end();
885da8e4a6SDan Williams 		return ret;
895da8e4a6SDan Williams 	}
905da8e4a6SDan Williams 
91*d73de815SDavid Howells 	return copy_user_generic((__force void *)dst, src, len);
925da8e4a6SDan Williams }
93