xref: /openbmc/linux/arch/x86/lib/copy_mc.c (revision ec6347bb)
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 
48ec6347bbSDan Williams /**
49ec6347bbSDan Williams  * copy_mc_to_kernel - memory copy that handles source exceptions
50ec6347bbSDan Williams  *
51ec6347bbSDan Williams  * @dst:	destination address
52ec6347bbSDan Williams  * @src:	source address
53ec6347bbSDan Williams  * @len:	number of bytes to copy
54ec6347bbSDan Williams  *
55ec6347bbSDan Williams  * Call into the 'fragile' version on systems that have trouble
56ec6347bbSDan Williams  * actually do machine check recovery. Everyone else can just
57ec6347bbSDan Williams  * use memcpy().
58ec6347bbSDan Williams  *
59ec6347bbSDan Williams  * Return 0 for success, or number of bytes not copied if there was an
60ec6347bbSDan Williams  * exception.
61ec6347bbSDan Williams  */
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);
66ec6347bbSDan Williams 	memcpy(dst, src, len);
67ec6347bbSDan Williams 	return 0;
68ec6347bbSDan Williams }
69ec6347bbSDan Williams EXPORT_SYMBOL_GPL(copy_mc_to_kernel);
70ec6347bbSDan Williams 
71ec6347bbSDan Williams unsigned long __must_check copy_mc_to_user(void *dst, const void *src, unsigned len)
72ec6347bbSDan Williams {
73ec6347bbSDan Williams 	unsigned long ret;
74ec6347bbSDan Williams 
75ec6347bbSDan Williams 	if (!copy_mc_fragile_enabled)
76ec6347bbSDan Williams 		return copy_user_generic(dst, src, len);
77ec6347bbSDan Williams 
78ec6347bbSDan Williams 	__uaccess_begin();
79ec6347bbSDan Williams 	ret = copy_mc_fragile(dst, src, len);
80ec6347bbSDan Williams 	__uaccess_end();
81ec6347bbSDan Williams 	return ret;
82ec6347bbSDan Williams }
83