Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * __get_user functions. |
| 4 | * |
| 5 | * (C) Copyright 1998 Linus Torvalds |
| 6 | * (C) Copyright 2005 Andi Kleen |
Glauber Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 7 | * (C) Copyright 2008 Glauber Costa |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 18 | * Inputs: %[r|e]ax contains the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * |
Glauber Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 20 | * Outputs: %[r|e]ax is error code (0 or -EFAULT) |
| 21 | * %[r|e]dx contains zero-extended value |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 22 | * %ecx contains the high half for 32-bit __get_user_8 |
Glauber Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * |
| 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 Fitzhardinge | 0341c14 | 2009-02-13 11:14:01 -0800 | [diff] [blame] | 30 | #include <asm/page_types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/errno.h> |
Sam Ravnborg | e2d5df9 | 2005-09-09 21:28:48 +0200 | [diff] [blame] | 32 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/thread_info.h> |
Glauber Costa | 40faf46 | 2008-06-24 11:37:57 -0300 | [diff] [blame] | 34 | #include <asm/asm.h> |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 35 | #include <asm/smap.h> |
Al Viro | 784d569 | 2016-01-11 11:04:34 -0500 | [diff] [blame] | 36 | #include <asm/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | ea6f043 | 2020-04-08 12:50:01 -0700 | [diff] [blame] | 38 | #define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC |
| 39 | |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 40 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | .text |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 50 | SYM_FUNC_START(__get_user_1) |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 51 | LOAD_TASK_SIZE_MINUS_N(0) |
| 52 | cmp %_ASM_DX,%_ASM_AX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | jae bad_get_user |
Dan Williams | c7f631c | 2018-01-29 17:02:54 -0800 | [diff] [blame] | 54 | sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ |
| 55 | and %_ASM_DX, %_ASM_AX |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 56 | ASM_STAC |
H. Peter Anvin | 1664016 | 2013-02-11 23:14:48 -0800 | [diff] [blame] | 57 | 1: movzbl (%_ASM_AX),%edx |
Glauber Costa | ef8c1a2 | 2008-06-24 11:21:53 -0300 | [diff] [blame] | 58 | xor %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 59 | ASM_CLAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | ret |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 61 | SYM_FUNC_END(__get_user_1) |
Al Viro | 784d569 | 2016-01-11 11:04:34 -0500 | [diff] [blame] | 62 | EXPORT_SYMBOL(__get_user_1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 64 | SYM_FUNC_START(__get_user_2) |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 65 | LOAD_TASK_SIZE_MINUS_N(1) |
| 66 | cmp %_ASM_DX,%_ASM_AX |
Glauber Costa | 9262875 | 2008-06-24 11:13:16 -0300 | [diff] [blame] | 67 | jae bad_get_user |
Dan Williams | c7f631c | 2018-01-29 17:02:54 -0800 | [diff] [blame] | 68 | sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ |
| 69 | and %_ASM_DX, %_ASM_AX |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 70 | ASM_STAC |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 71 | 2: movzwl (%_ASM_AX),%edx |
Glauber Costa | ef8c1a2 | 2008-06-24 11:21:53 -0300 | [diff] [blame] | 72 | xor %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 73 | ASM_CLAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | ret |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 75 | SYM_FUNC_END(__get_user_2) |
Al Viro | 784d569 | 2016-01-11 11:04:34 -0500 | [diff] [blame] | 76 | EXPORT_SYMBOL(__get_user_2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 78 | SYM_FUNC_START(__get_user_4) |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 79 | LOAD_TASK_SIZE_MINUS_N(3) |
| 80 | cmp %_ASM_DX,%_ASM_AX |
Glauber Costa | 9262875 | 2008-06-24 11:13:16 -0300 | [diff] [blame] | 81 | jae bad_get_user |
Dan Williams | c7f631c | 2018-01-29 17:02:54 -0800 | [diff] [blame] | 82 | sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ |
| 83 | and %_ASM_DX, %_ASM_AX |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 84 | ASM_STAC |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 85 | 3: movl (%_ASM_AX),%edx |
Glauber Costa | ef8c1a2 | 2008-06-24 11:21:53 -0300 | [diff] [blame] | 86 | xor %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 87 | ASM_CLAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ret |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 89 | SYM_FUNC_END(__get_user_4) |
Al Viro | 784d569 | 2016-01-11 11:04:34 -0500 | [diff] [blame] | 90 | EXPORT_SYMBOL(__get_user_4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 92 | SYM_FUNC_START(__get_user_8) |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 93 | #ifdef CONFIG_X86_64 |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 94 | LOAD_TASK_SIZE_MINUS_N(7) |
| 95 | cmp %_ASM_DX,%_ASM_AX |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 96 | jae bad_get_user |
Dan Williams | c7f631c | 2018-01-29 17:02:54 -0800 | [diff] [blame] | 97 | sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ |
| 98 | and %_ASM_DX, %_ASM_AX |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 99 | ASM_STAC |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 100 | 4: movq (%_ASM_AX),%rdx |
Glauber Costa | ef8c1a2 | 2008-06-24 11:21:53 -0300 | [diff] [blame] | 101 | xor %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 102 | ASM_CLAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | ret |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 104 | #else |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 105 | LOAD_TASK_SIZE_MINUS_N(7) |
| 106 | cmp %_ASM_DX,%_ASM_AX |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 107 | jae bad_get_user_8 |
Dan Williams | c7f631c | 2018-01-29 17:02:54 -0800 | [diff] [blame] | 108 | sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */ |
| 109 | and %_ASM_DX, %_ASM_AX |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 110 | ASM_STAC |
Christoph Hellwig | 47058bb | 2020-09-03 16:22:40 +0200 | [diff] [blame] | 111 | 4: movl (%_ASM_AX),%edx |
| 112 | 5: movl 4(%_ASM_AX),%ecx |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 113 | xor %eax,%eax |
| 114 | ASM_CLAC |
| 115 | ret |
| 116 | #endif |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 117 | SYM_FUNC_END(__get_user_8) |
Al Viro | 784d569 | 2016-01-11 11:04:34 -0500 | [diff] [blame] | 118 | EXPORT_SYMBOL(__get_user_8) |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 119 | |
Linus Torvalds | ea6f043 | 2020-04-08 12:50:01 -0700 | [diff] [blame] | 120 | /* .. and the same for __get_user, just without the range checks */ |
| 121 | SYM_FUNC_START(__get_user_nocheck_1) |
| 122 | ASM_STAC |
| 123 | ASM_BARRIER_NOSPEC |
| 124 | 6: movzbl (%_ASM_AX),%edx |
| 125 | xor %eax,%eax |
| 126 | ASM_CLAC |
| 127 | ret |
| 128 | SYM_FUNC_END(__get_user_nocheck_1) |
| 129 | EXPORT_SYMBOL(__get_user_nocheck_1) |
| 130 | |
| 131 | SYM_FUNC_START(__get_user_nocheck_2) |
| 132 | ASM_STAC |
| 133 | ASM_BARRIER_NOSPEC |
| 134 | 7: movzwl (%_ASM_AX),%edx |
| 135 | xor %eax,%eax |
| 136 | ASM_CLAC |
| 137 | ret |
| 138 | SYM_FUNC_END(__get_user_nocheck_2) |
| 139 | EXPORT_SYMBOL(__get_user_nocheck_2) |
| 140 | |
| 141 | SYM_FUNC_START(__get_user_nocheck_4) |
| 142 | ASM_STAC |
| 143 | ASM_BARRIER_NOSPEC |
| 144 | 8: movl (%_ASM_AX),%edx |
| 145 | xor %eax,%eax |
| 146 | ASM_CLAC |
| 147 | ret |
| 148 | SYM_FUNC_END(__get_user_nocheck_4) |
| 149 | EXPORT_SYMBOL(__get_user_nocheck_4) |
| 150 | |
| 151 | SYM_FUNC_START(__get_user_nocheck_8) |
| 152 | ASM_STAC |
| 153 | ASM_BARRIER_NOSPEC |
| 154 | #ifdef CONFIG_X86_64 |
| 155 | 9: movq (%_ASM_AX),%rdx |
| 156 | #else |
| 157 | 9: movl (%_ASM_AX),%edx |
| 158 | 10: movl 4(%_ASM_AX),%ecx |
| 159 | #endif |
| 160 | xor %eax,%eax |
| 161 | ASM_CLAC |
| 162 | ret |
| 163 | SYM_FUNC_END(__get_user_nocheck_8) |
| 164 | EXPORT_SYMBOL(__get_user_nocheck_8) |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Jiri Slaby | ef77e68 | 2019-10-11 13:50:45 +0200 | [diff] [blame] | 167 | SYM_CODE_START_LOCAL(.Lbad_get_user_clac) |
Josh Poimboeuf | 82e844a | 2019-07-17 20:36:44 -0500 | [diff] [blame] | 168 | ASM_CLAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | bad_get_user: |
Glauber Costa | ef8c1a2 | 2008-06-24 11:21:53 -0300 | [diff] [blame] | 170 | xor %edx,%edx |
Glauber Costa | 40faf46 | 2008-06-24 11:37:57 -0300 | [diff] [blame] | 171 | mov $(-EFAULT),%_ASM_AX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | ret |
Jiri Slaby | ef77e68 | 2019-10-11 13:50:45 +0200 | [diff] [blame] | 173 | SYM_CODE_END(.Lbad_get_user_clac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 175 | #ifdef CONFIG_X86_32 |
Jiri Slaby | ef77e68 | 2019-10-11 13:50:45 +0200 | [diff] [blame] | 176 | SYM_CODE_START_LOCAL(.Lbad_get_user_8_clac) |
Josh Poimboeuf | 82e844a | 2019-07-17 20:36:44 -0500 | [diff] [blame] | 177 | ASM_CLAC |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 178 | bad_get_user_8: |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 179 | xor %edx,%edx |
| 180 | xor %ecx,%ecx |
| 181 | mov $(-EFAULT),%_ASM_AX |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 182 | ret |
Jiri Slaby | ef77e68 | 2019-10-11 13:50:45 +0200 | [diff] [blame] | 183 | SYM_CODE_END(.Lbad_get_user_8_clac) |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 184 | #endif |
| 185 | |
Linus Torvalds | ea6f043 | 2020-04-08 12:50:01 -0700 | [diff] [blame] | 186 | /* get_user */ |
Jiri Slaby | 98ededb | 2019-09-06 09:55:50 +0200 | [diff] [blame] | 187 | _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 Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 190 | #ifdef CONFIG_X86_64 |
Jiri Slaby | 98ededb | 2019-09-06 09:55:50 +0200 | [diff] [blame] | 191 | _ASM_EXTABLE_UA(4b, .Lbad_get_user_clac) |
Ville Syrjälä | 96477b4 | 2012-12-12 13:34:03 +0200 | [diff] [blame] | 192 | #else |
Jiri Slaby | 98ededb | 2019-09-06 09:55:50 +0200 | [diff] [blame] | 193 | _ASM_EXTABLE_UA(4b, .Lbad_get_user_8_clac) |
| 194 | _ASM_EXTABLE_UA(5b, .Lbad_get_user_8_clac) |
Glauber Costa | 6c2d458 | 2008-06-24 12:05:11 -0300 | [diff] [blame] | 195 | #endif |
Linus Torvalds | ea6f043 | 2020-04-08 12:50:01 -0700 | [diff] [blame] | 196 | |
| 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 |