blob: fa1bc2104b326ee36e41e9a89a74c2b8c5f511dc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * __get_user functions.
4 *
5 * (C) Copyright 1998 Linus Torvalds
6 * (C) Copyright 2005 Andi Kleen
Glauber Costa6c2d4582008-06-24 12:05:11 -03007 * (C) Copyright 2008 Glauber Costa
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * These functions have a non-standard call interface
10 * to make them more efficient, especially as they
11 * return an error value in addition to the "real"
12 * return value.
13 */
14
15/*
16 * __get_user_X
17 *
Glauber Costa6c2d4582008-06-24 12:05:11 -030018 * Inputs: %[r|e]ax contains the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
Glauber Costa6c2d4582008-06-24 12:05:11 -030020 * Outputs: %[r|e]ax is error code (0 or -EFAULT)
21 * %[r|e]dx contains zero-extended value
Ville Syrjälä96477b42012-12-12 13:34:03 +020022 * %ecx contains the high half for 32-bit __get_user_8
Glauber Costa6c2d4582008-06-24 12:05:11 -030023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 * These functions should not modify any other registers,
26 * as they get called from within inline assembly.
27 */
28
29#include <linux/linkage.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080030#include <asm/page_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/errno.h>
Sam Ravnborge2d5df92005-09-09 21:28:48 +020032#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/thread_info.h>
Glauber Costa40faf462008-06-24 11:37:57 -030034#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070035#include <asm/smap.h>
Al Viro784d5692016-01-11 11:04:34 -050036#include <asm/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvaldsea6f0432020-04-08 12:50:01 -070038#define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC
39
Christoph Hellwig47058bb2020-09-03 16:22:40 +020040#ifdef CONFIG_X86_5LEVEL
41#define LOAD_TASK_SIZE_MINUS_N(n) \
42 ALTERNATIVE __stringify(mov $((1 << 47) - 4096 - (n)),%rdx), \
43 __stringify(mov $((1 << 56) - 4096 - (n)),%rdx), X86_FEATURE_LA57
44#else
45#define LOAD_TASK_SIZE_MINUS_N(n) \
46 mov $(TASK_SIZE_MAX - (n)),%_ASM_DX
47#endif
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .text
Jiri Slaby6dcc5622019-10-11 13:51:04 +020050SYM_FUNC_START(__get_user_1)
Christoph Hellwig47058bb2020-09-03 16:22:40 +020051 LOAD_TASK_SIZE_MINUS_N(0)
52 cmp %_ASM_DX,%_ASM_AX
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 jae bad_get_user
Dan Williamsc7f631c2018-01-29 17:02:54 -080054 sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
55 and %_ASM_DX, %_ASM_AX
H. Peter Anvin63bcff22012-09-21 12:43:12 -070056 ASM_STAC
H. Peter Anvin16640162013-02-11 23:14:48 -0800571: movzbl (%_ASM_AX),%edx
Glauber Costaef8c1a22008-06-24 11:21:53 -030058 xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -070059 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 ret
Jiri Slaby6dcc5622019-10-11 13:51:04 +020061SYM_FUNC_END(__get_user_1)
Al Viro784d5692016-01-11 11:04:34 -050062EXPORT_SYMBOL(__get_user_1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jiri Slaby6dcc5622019-10-11 13:51:04 +020064SYM_FUNC_START(__get_user_2)
Christoph Hellwig47058bb2020-09-03 16:22:40 +020065 LOAD_TASK_SIZE_MINUS_N(1)
66 cmp %_ASM_DX,%_ASM_AX
Glauber Costa92628752008-06-24 11:13:16 -030067 jae bad_get_user
Dan Williamsc7f631c2018-01-29 17:02:54 -080068 sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
69 and %_ASM_DX, %_ASM_AX
H. Peter Anvin63bcff22012-09-21 12:43:12 -070070 ASM_STAC
Christoph Hellwig47058bb2020-09-03 16:22:40 +0200712: movzwl (%_ASM_AX),%edx
Glauber Costaef8c1a22008-06-24 11:21:53 -030072 xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -070073 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ret
Jiri Slaby6dcc5622019-10-11 13:51:04 +020075SYM_FUNC_END(__get_user_2)
Al Viro784d5692016-01-11 11:04:34 -050076EXPORT_SYMBOL(__get_user_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jiri Slaby6dcc5622019-10-11 13:51:04 +020078SYM_FUNC_START(__get_user_4)
Christoph Hellwig47058bb2020-09-03 16:22:40 +020079 LOAD_TASK_SIZE_MINUS_N(3)
80 cmp %_ASM_DX,%_ASM_AX
Glauber Costa92628752008-06-24 11:13:16 -030081 jae bad_get_user
Dan Williamsc7f631c2018-01-29 17:02:54 -080082 sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
83 and %_ASM_DX, %_ASM_AX
H. Peter Anvin63bcff22012-09-21 12:43:12 -070084 ASM_STAC
Christoph Hellwig47058bb2020-09-03 16:22:40 +0200853: movl (%_ASM_AX),%edx
Glauber Costaef8c1a22008-06-24 11:21:53 -030086 xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -070087 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ret
Jiri Slaby6dcc5622019-10-11 13:51:04 +020089SYM_FUNC_END(__get_user_4)
Al Viro784d5692016-01-11 11:04:34 -050090EXPORT_SYMBOL(__get_user_4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jiri Slaby6dcc5622019-10-11 13:51:04 +020092SYM_FUNC_START(__get_user_8)
Ville Syrjälä96477b42012-12-12 13:34:03 +020093#ifdef CONFIG_X86_64
Christoph Hellwig47058bb2020-09-03 16:22:40 +020094 LOAD_TASK_SIZE_MINUS_N(7)
95 cmp %_ASM_DX,%_ASM_AX
Ville Syrjälä96477b42012-12-12 13:34:03 +020096 jae bad_get_user
Dan Williamsc7f631c2018-01-29 17:02:54 -080097 sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
98 and %_ASM_DX, %_ASM_AX
H. Peter Anvin63bcff22012-09-21 12:43:12 -070099 ASM_STAC
Christoph Hellwig47058bb2020-09-03 16:22:40 +02001004: movq (%_ASM_AX),%rdx
Glauber Costaef8c1a22008-06-24 11:21:53 -0300101 xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700102 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ret
Ville Syrjälä96477b42012-12-12 13:34:03 +0200104#else
Christoph Hellwig47058bb2020-09-03 16:22:40 +0200105 LOAD_TASK_SIZE_MINUS_N(7)
106 cmp %_ASM_DX,%_ASM_AX
Ville Syrjälä96477b42012-12-12 13:34:03 +0200107 jae bad_get_user_8
Dan Williamsc7f631c2018-01-29 17:02:54 -0800108 sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
109 and %_ASM_DX, %_ASM_AX
Ville Syrjälä96477b42012-12-12 13:34:03 +0200110 ASM_STAC
Christoph Hellwig47058bb2020-09-03 16:22:40 +02001114: movl (%_ASM_AX),%edx
1125: movl 4(%_ASM_AX),%ecx
Ville Syrjälä96477b42012-12-12 13:34:03 +0200113 xor %eax,%eax
114 ASM_CLAC
115 ret
116#endif
Jiri Slaby6dcc5622019-10-11 13:51:04 +0200117SYM_FUNC_END(__get_user_8)
Al Viro784d5692016-01-11 11:04:34 -0500118EXPORT_SYMBOL(__get_user_8)
Ville Syrjälä96477b42012-12-12 13:34:03 +0200119
Linus Torvaldsea6f0432020-04-08 12:50:01 -0700120/* .. and the same for __get_user, just without the range checks */
121SYM_FUNC_START(__get_user_nocheck_1)
122 ASM_STAC
123 ASM_BARRIER_NOSPEC
1246: movzbl (%_ASM_AX),%edx
125 xor %eax,%eax
126 ASM_CLAC
127 ret
128SYM_FUNC_END(__get_user_nocheck_1)
129EXPORT_SYMBOL(__get_user_nocheck_1)
130
131SYM_FUNC_START(__get_user_nocheck_2)
132 ASM_STAC
133 ASM_BARRIER_NOSPEC
1347: movzwl (%_ASM_AX),%edx
135 xor %eax,%eax
136 ASM_CLAC
137 ret
138SYM_FUNC_END(__get_user_nocheck_2)
139EXPORT_SYMBOL(__get_user_nocheck_2)
140
141SYM_FUNC_START(__get_user_nocheck_4)
142 ASM_STAC
143 ASM_BARRIER_NOSPEC
1448: movl (%_ASM_AX),%edx
145 xor %eax,%eax
146 ASM_CLAC
147 ret
148SYM_FUNC_END(__get_user_nocheck_4)
149EXPORT_SYMBOL(__get_user_nocheck_4)
150
151SYM_FUNC_START(__get_user_nocheck_8)
152 ASM_STAC
153 ASM_BARRIER_NOSPEC
154#ifdef CONFIG_X86_64
1559: movq (%_ASM_AX),%rdx
156#else
1579: movl (%_ASM_AX),%edx
15810: movl 4(%_ASM_AX),%ecx
159#endif
160 xor %eax,%eax
161 ASM_CLAC
162 ret
163SYM_FUNC_END(__get_user_nocheck_8)
164EXPORT_SYMBOL(__get_user_nocheck_8)
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jiri Slabyef77e682019-10-11 13:50:45 +0200167SYM_CODE_START_LOCAL(.Lbad_get_user_clac)
Josh Poimboeuf82e844a2019-07-17 20:36:44 -0500168 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169bad_get_user:
Glauber Costaef8c1a22008-06-24 11:21:53 -0300170 xor %edx,%edx
Glauber Costa40faf462008-06-24 11:37:57 -0300171 mov $(-EFAULT),%_ASM_AX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ret
Jiri Slabyef77e682019-10-11 13:50:45 +0200173SYM_CODE_END(.Lbad_get_user_clac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Ville Syrjälä96477b42012-12-12 13:34:03 +0200175#ifdef CONFIG_X86_32
Jiri Slabyef77e682019-10-11 13:50:45 +0200176SYM_CODE_START_LOCAL(.Lbad_get_user_8_clac)
Josh Poimboeuf82e844a2019-07-17 20:36:44 -0500177 ASM_CLAC
Ville Syrjälä96477b42012-12-12 13:34:03 +0200178bad_get_user_8:
Ville Syrjälä96477b42012-12-12 13:34:03 +0200179 xor %edx,%edx
180 xor %ecx,%ecx
181 mov $(-EFAULT),%_ASM_AX
Ville Syrjälä96477b42012-12-12 13:34:03 +0200182 ret
Jiri Slabyef77e682019-10-11 13:50:45 +0200183SYM_CODE_END(.Lbad_get_user_8_clac)
Ville Syrjälä96477b42012-12-12 13:34:03 +0200184#endif
185
Linus Torvaldsea6f0432020-04-08 12:50:01 -0700186/* get_user */
Jiri Slaby98ededb2019-09-06 09:55:50 +0200187 _ASM_EXTABLE_UA(1b, .Lbad_get_user_clac)
188 _ASM_EXTABLE_UA(2b, .Lbad_get_user_clac)
189 _ASM_EXTABLE_UA(3b, .Lbad_get_user_clac)
Glauber Costa6c2d4582008-06-24 12:05:11 -0300190#ifdef CONFIG_X86_64
Jiri Slaby98ededb2019-09-06 09:55:50 +0200191 _ASM_EXTABLE_UA(4b, .Lbad_get_user_clac)
Ville Syrjälä96477b42012-12-12 13:34:03 +0200192#else
Jiri Slaby98ededb2019-09-06 09:55:50 +0200193 _ASM_EXTABLE_UA(4b, .Lbad_get_user_8_clac)
194 _ASM_EXTABLE_UA(5b, .Lbad_get_user_8_clac)
Glauber Costa6c2d4582008-06-24 12:05:11 -0300195#endif
Linus Torvaldsea6f0432020-04-08 12:50:01 -0700196
197/* __get_user */
198 _ASM_EXTABLE_UA(6b, .Lbad_get_user_clac)
199 _ASM_EXTABLE_UA(7b, .Lbad_get_user_clac)
200 _ASM_EXTABLE_UA(8b, .Lbad_get_user_clac)
201#ifdef CONFIG_X86_64
202 _ASM_EXTABLE_UA(9b, .Lbad_get_user_clac)
203#else
204 _ASM_EXTABLE_UA(9b, .Lbad_get_user_8_clac)
205 _ASM_EXTABLE_UA(10b, .Lbad_get_user_8_clac)
206#endif