Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xsave/xrstor support. |
| 3 | * |
| 4 | * Author: Suresh Siddha <suresh.b.siddha@intel.com> |
| 5 | */ |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 6 | #include <linux/compat.h> |
Fenghua Yu | 7e7ce87 | 2014-05-29 11:12:43 -0700 | [diff] [blame] | 7 | #include <linux/cpu.h> |
Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 8 | #include <asm/fpu/api.h> |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 9 | #include <asm/fpu/internal.h> |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 10 | #include <asm/sigframe.h> |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 11 | #include <asm/tlbflush.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 12 | |
Ingo Molnar | 5b07343 | 2015-04-28 08:51:17 +0200 | [diff] [blame] | 13 | static const char *xfeature_names[] = |
| 14 | { |
| 15 | "x87 floating point registers" , |
| 16 | "SSE registers" , |
| 17 | "AVX registers" , |
| 18 | "MPX bounds registers" , |
| 19 | "MPX CSR" , |
| 20 | "AVX-512 opmask" , |
| 21 | "AVX-512 Hi256" , |
| 22 | "AVX-512 ZMM_Hi256" , |
| 23 | "unknown xstate feature" , |
| 24 | }; |
| 25 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 26 | /* |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 27 | * Mask of xstate features supported by the CPU and the kernel: |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 28 | */ |
Ingo Molnar | 5b07343 | 2015-04-28 08:51:17 +0200 | [diff] [blame] | 29 | u64 xfeatures_mask __read_mostly; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 30 | |
Robert Richter | 45c2d7f | 2010-07-21 19:03:55 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Represents init state for the supported extended state. |
| 33 | */ |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 34 | struct xsave_struct init_xstate_ctx; |
Robert Richter | 45c2d7f | 2010-07-21 19:03:55 +0200 | [diff] [blame] | 35 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 36 | static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32; |
Ingo Molnar | 966ece6 | 2015-04-25 05:04:41 +0200 | [diff] [blame] | 37 | static unsigned int xstate_offsets[XFEATURES_NR_MAX], xstate_sizes[XFEATURES_NR_MAX]; |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 38 | static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8]; |
Ingo Molnar | 84246fe4 | 2015-04-24 09:23:59 +0200 | [diff] [blame] | 39 | |
| 40 | /* The number of supported xfeatures in xfeatures_mask: */ |
| 41 | static unsigned int xfeatures_nr; |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 42 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 43 | /* |
Ingo Molnar | 5b07343 | 2015-04-28 08:51:17 +0200 | [diff] [blame] | 44 | * Return whether the system supports a given xfeature. |
| 45 | * |
| 46 | * Also return the name of the (most advanced) feature that the caller requested: |
| 47 | */ |
| 48 | int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name) |
| 49 | { |
| 50 | u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask; |
| 51 | |
| 52 | if (unlikely(feature_name)) { |
| 53 | long xfeature_idx, max_idx; |
| 54 | u64 xfeatures_print; |
| 55 | /* |
| 56 | * So we use FLS here to be able to print the most advanced |
| 57 | * feature that was requested but is missing. So if a driver |
| 58 | * asks about "XSTATE_SSE | XSTATE_YMM" we'll print the |
| 59 | * missing AVX feature - this is the most informative message |
| 60 | * to users: |
| 61 | */ |
| 62 | if (xfeatures_missing) |
| 63 | xfeatures_print = xfeatures_missing; |
| 64 | else |
| 65 | xfeatures_print = xfeatures_needed; |
| 66 | |
| 67 | xfeature_idx = fls64(xfeatures_print)-1; |
| 68 | max_idx = ARRAY_SIZE(xfeature_names)-1; |
| 69 | xfeature_idx = min(xfeature_idx, max_idx); |
| 70 | |
| 71 | *feature_name = xfeature_names[xfeature_idx]; |
| 72 | } |
| 73 | |
| 74 | if (xfeatures_missing) |
| 75 | return 0; |
| 76 | |
| 77 | return 1; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(cpu_has_xfeatures); |
| 80 | |
| 81 | /* |
Ingo Molnar | 73a3aeb | 2015-04-24 11:32:59 +0200 | [diff] [blame] | 82 | * When executing XSAVEOPT (optimized XSAVE), if a processor implementation |
| 83 | * detects that an FPU state component is still (or is again) in its |
| 84 | * initialized state, it may clear the corresponding bit in the header.xfeatures |
| 85 | * field, and can skip the writeout of registers to the corresponding memory layout. |
| 86 | * |
| 87 | * This means that when the bit is zero, the state component might still contain |
| 88 | * some previous - non-initialized register state. |
| 89 | * |
| 90 | * Before writing xstate information to user-space we sanitize those components, |
| 91 | * to always ensure that the memory layout of a feature will be in the init state |
| 92 | * if the corresponding header bit is zero. This is to ensure that user-space doesn't |
| 93 | * see some stale state in the memory layout during signal handling, debugging etc. |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 94 | */ |
Ingo Molnar | 36e49e7f | 2015-04-28 11:25:02 +0200 | [diff] [blame^] | 95 | void fpstate_sanitize_xstate(struct fpu *fpu) |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 96 | { |
Ingo Molnar | 36e49e7f | 2015-04-28 11:25:02 +0200 | [diff] [blame^] | 97 | struct i387_fxsave_struct *fx = &fpu->state.fxsave; |
Ingo Molnar | 73a3aeb | 2015-04-24 11:32:59 +0200 | [diff] [blame] | 98 | int feature_bit; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 99 | u64 xfeatures; |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 100 | |
Ingo Molnar | 1ac91a7 | 2015-04-28 11:17:55 +0200 | [diff] [blame] | 101 | if (!use_xsaveopt()) |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 102 | return; |
| 103 | |
Ingo Molnar | 36e49e7f | 2015-04-28 11:25:02 +0200 | [diff] [blame^] | 104 | xfeatures = fpu->state.xsave.header.xfeatures; |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * None of the feature bits are in init state. So nothing else |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 108 | * to do for us, as the memory layout is up to date. |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 109 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 110 | if ((xfeatures & xfeatures_mask) == xfeatures_mask) |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 111 | return; |
| 112 | |
| 113 | /* |
| 114 | * FP is in init state |
| 115 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 116 | if (!(xfeatures & XSTATE_FP)) { |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 117 | fx->cwd = 0x37f; |
| 118 | fx->swd = 0; |
| 119 | fx->twd = 0; |
| 120 | fx->fop = 0; |
| 121 | fx->rip = 0; |
| 122 | fx->rdp = 0; |
| 123 | memset(&fx->st_space[0], 0, 128); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * SSE is in init state |
| 128 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 129 | if (!(xfeatures & XSTATE_SSE)) |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 130 | memset(&fx->xmm_space[0], 0, 256); |
| 131 | |
Ingo Molnar | 73a3aeb | 2015-04-24 11:32:59 +0200 | [diff] [blame] | 132 | /* |
| 133 | * First two features are FPU and SSE, which above we handled |
| 134 | * in a special way already: |
| 135 | */ |
| 136 | feature_bit = 0x2; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 137 | xfeatures = (xfeatures_mask & ~xfeatures) >> 2; |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
Ingo Molnar | 73a3aeb | 2015-04-24 11:32:59 +0200 | [diff] [blame] | 140 | * Update all the remaining memory layouts according to their |
| 141 | * standard xstate layout, if their header bit is in the init |
| 142 | * state: |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 143 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 144 | while (xfeatures) { |
| 145 | if (xfeatures & 0x1) { |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 146 | int offset = xstate_offsets[feature_bit]; |
| 147 | int size = xstate_sizes[feature_bit]; |
| 148 | |
Ingo Molnar | 73a3aeb | 2015-04-24 11:32:59 +0200 | [diff] [blame] | 149 | memcpy((void *)fx + offset, |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 150 | (void *)&init_xstate_ctx + offset, |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 151 | size); |
| 152 | } |
| 153 | |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 154 | xfeatures >>= 1; |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 155 | feature_bit++; |
| 156 | } |
| 157 | } |
| 158 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Check for the presence of extended state information in the |
| 161 | * user fpstate pointer in the sigcontext. |
| 162 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 163 | static inline int check_for_xstate(struct i387_fxsave_struct __user *buf, |
| 164 | void __user *fpstate, |
| 165 | struct _fpx_sw_bytes *fx_sw) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 166 | { |
| 167 | int min_xstate_size = sizeof(struct i387_fxsave_struct) + |
Ingo Molnar | 3a54450 | 2015-04-24 10:14:36 +0200 | [diff] [blame] | 168 | sizeof(struct xstate_header); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 169 | unsigned int magic2; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 170 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 171 | if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw))) |
| 172 | return -1; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 173 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 174 | /* Check for the first magic field and other error scenarios. */ |
| 175 | if (fx_sw->magic1 != FP_XSTATE_MAGIC1 || |
| 176 | fx_sw->xstate_size < min_xstate_size || |
| 177 | fx_sw->xstate_size > xstate_size || |
| 178 | fx_sw->xstate_size > fx_sw->extended_size) |
| 179 | return -1; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 182 | * Check for the presence of second magic word at the end of memory |
| 183 | * layout. This detects the case where the user just copied the legacy |
| 184 | * fpstate layout with out copying the extended state information |
| 185 | * in the memory layout. |
| 186 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 187 | if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size)) |
| 188 | || magic2 != FP_XSTATE_MAGIC2) |
| 189 | return -1; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Signal frame handlers. |
| 196 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 197 | static inline int save_fsave_header(struct task_struct *tsk, void __user *buf) |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 198 | { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 199 | if (use_fxsr()) { |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 200 | struct xsave_struct *xsave = &tsk->thread.fpu.state.xsave; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 201 | struct user_i387_ia32_struct env; |
| 202 | struct _fpstate_ia32 __user *fp = buf; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 203 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 204 | convert_from_fxsr(&env, tsk); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 205 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 206 | if (__copy_to_user(buf, &env, sizeof(env)) || |
| 207 | __put_user(xsave->i387.swd, &fp->status) || |
| 208 | __put_user(X86_FXSR_MAGIC, &fp->magic)) |
| 209 | return -1; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 210 | } else { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 211 | struct i387_fsave_struct __user *fp = buf; |
| 212 | u32 swd; |
| 213 | if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status)) |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 214 | return -1; |
| 215 | } |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 216 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 217 | return 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 220 | static inline int save_xstate_epilog(void __user *buf, int ia32_frame) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 221 | { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 222 | struct xsave_struct __user *x = buf; |
| 223 | struct _fpx_sw_bytes *sw_bytes; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 224 | u32 xfeatures; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 225 | int err; |
| 226 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 227 | /* Setup the bytes not touched by the [f]xsave and reserved for SW. */ |
| 228 | sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved; |
| 229 | err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes)); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 230 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 231 | if (!use_xsave()) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 232 | return err; |
| 233 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 234 | err |= __put_user(FP_XSTATE_MAGIC2, (__u32 *)(buf + xstate_size)); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 235 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 236 | /* |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 237 | * Read the xfeatures which we copied (directly from the cpu or |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 238 | * from the state in task struct) to the user buffers. |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 239 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 240 | err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * For legacy compatible, we always set FP/SSE bits in the bit |
| 244 | * vector while saving the state to the user context. This will |
| 245 | * enable us capturing any changes(during sigreturn) to |
| 246 | * the FP/SSE bits by the legacy applications which don't touch |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 247 | * xfeatures in the xsave header. |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 248 | * |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 249 | * xsave aware apps can change the xfeatures in the xsave |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 250 | * header as well as change any contents in the memory layout. |
| 251 | * xrestore as part of sigreturn will capture all the changes. |
| 252 | */ |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 253 | xfeatures |= XSTATE_FPSSE; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 254 | |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 255 | err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 256 | |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | static inline int save_user_xstate(struct xsave_struct __user *buf) |
| 261 | { |
| 262 | int err; |
| 263 | |
| 264 | if (use_xsave()) |
| 265 | err = xsave_user(buf); |
| 266 | else if (use_fxsr()) |
| 267 | err = fxsave_user((struct i387_fxsave_struct __user *) buf); |
| 268 | else |
| 269 | err = fsave_user((struct i387_fsave_struct __user *) buf); |
| 270 | |
| 271 | if (unlikely(err) && __clear_user(buf, xstate_size)) |
| 272 | err = -EFAULT; |
| 273 | return err; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 277 | * Save the fpu, extended register state to the user signal frame. |
| 278 | * |
| 279 | * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save |
| 280 | * state is copied. |
| 281 | * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'. |
| 282 | * |
| 283 | * buf == buf_fx for 64-bit frames and 32-bit fsave frame. |
| 284 | * buf != buf_fx for 32-bit frames with fxstate. |
| 285 | * |
| 286 | * If the fpu, extended register state is live, save the state directly |
| 287 | * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise, |
| 288 | * copy the thread's fpu state to the user frame starting at 'buf_fx'. |
| 289 | * |
| 290 | * If this is a 32-bit frame with fxstate, put a fsave header before |
| 291 | * the aligned state at 'buf_fx'. |
| 292 | * |
| 293 | * For [f]xsave state, update the SW reserved fields in the [f]xsave frame |
| 294 | * indicating the absence/presence of the extended state to the user. |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 295 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 296 | int save_xstate_sig(void __user *buf, void __user *buf_fx, int size) |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 297 | { |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 298 | struct xsave_struct *xsave = ¤t->thread.fpu.state.xsave; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 299 | struct task_struct *tsk = current; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 300 | int ia32_fxstate = (buf != buf_fx); |
| 301 | |
| 302 | ia32_fxstate &= (config_enabled(CONFIG_X86_32) || |
| 303 | config_enabled(CONFIG_IA32_EMULATION)); |
| 304 | |
| 305 | if (!access_ok(VERIFY_WRITE, buf, size)) |
| 306 | return -EACCES; |
| 307 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 308 | if (!static_cpu_has(X86_FEATURE_FPU)) |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 309 | return fpregs_soft_get(current, NULL, 0, |
| 310 | sizeof(struct user_i387_ia32_struct), NULL, |
| 311 | (struct _fpstate_ia32 __user *) buf) ? -1 : 1; |
| 312 | |
| 313 | if (user_has_fpu()) { |
| 314 | /* Save the live register state to the user directly. */ |
| 315 | if (save_user_xstate(buf_fx)) |
| 316 | return -1; |
| 317 | /* Update the thread's fxstate to save the fsave header. */ |
| 318 | if (ia32_fxstate) |
| 319 | fpu_fxsave(&tsk->thread.fpu); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 320 | } else { |
Ingo Molnar | 36e49e7f | 2015-04-28 11:25:02 +0200 | [diff] [blame^] | 321 | fpstate_sanitize_xstate(&tsk->thread.fpu); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 322 | if (__copy_to_user(buf_fx, xsave, xstate_size)) |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | /* Save the fsave header for the 32-bit frames. */ |
| 327 | if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf)) |
| 328 | return -1; |
| 329 | |
| 330 | if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate)) |
| 331 | return -1; |
| 332 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static inline void |
| 337 | sanitize_restored_xstate(struct task_struct *tsk, |
| 338 | struct user_i387_ia32_struct *ia32_env, |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 339 | u64 xfeatures, int fx_only) |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 340 | { |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 341 | struct xsave_struct *xsave = &tsk->thread.fpu.state.xsave; |
Ingo Molnar | 3a54450 | 2015-04-24 10:14:36 +0200 | [diff] [blame] | 342 | struct xstate_header *header = &xsave->header; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 343 | |
| 344 | if (use_xsave()) { |
| 345 | /* These bits must be zero. */ |
Ingo Molnar | 3a54450 | 2015-04-24 10:14:36 +0200 | [diff] [blame] | 346 | memset(header->reserved, 0, 48); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * Init the state that is not present in the memory |
| 350 | * layout and not enabled by the OS. |
| 351 | */ |
| 352 | if (fx_only) |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 353 | header->xfeatures = XSTATE_FPSSE; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 354 | else |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 355 | header->xfeatures &= (xfeatures_mask & xfeatures); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | if (use_fxsr()) { |
| 359 | /* |
| 360 | * mscsr reserved bits must be masked to zero for security |
| 361 | * reasons. |
| 362 | */ |
| 363 | xsave->i387.mxcsr &= mxcsr_feature_mask; |
| 364 | |
| 365 | convert_to_fxsr(tsk, ia32_env); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Restore the extended state if present. Otherwise, restore the FP/SSE state. |
| 371 | */ |
| 372 | static inline int restore_user_xstate(void __user *buf, u64 xbv, int fx_only) |
| 373 | { |
| 374 | if (use_xsave()) { |
| 375 | if ((unsigned long)buf % 64 || fx_only) { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 376 | u64 init_bv = xfeatures_mask & ~XSTATE_FPSSE; |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 377 | xrstor_state(&init_xstate_ctx, init_bv); |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 378 | return fxrstor_user(buf); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 379 | } else { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 380 | u64 init_bv = xfeatures_mask & ~xbv; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 381 | if (unlikely(init_bv)) |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 382 | xrstor_state(&init_xstate_ctx, init_bv); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 383 | return xrestore_user(buf, xbv); |
| 384 | } |
| 385 | } else if (use_fxsr()) { |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 386 | return fxrstor_user(buf); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 387 | } else |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 388 | return frstor_user(buf); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size) |
| 392 | { |
| 393 | int ia32_fxstate = (buf != buf_fx); |
| 394 | struct task_struct *tsk = current; |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 395 | struct fpu *fpu = &tsk->thread.fpu; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 396 | int state_size = xstate_size; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 397 | u64 xfeatures = 0; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 398 | int fx_only = 0; |
| 399 | |
| 400 | ia32_fxstate &= (config_enabled(CONFIG_X86_32) || |
| 401 | config_enabled(CONFIG_IA32_EMULATION)); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 402 | |
| 403 | if (!buf) { |
Ingo Molnar | af2d94f | 2015-04-23 17:34:20 +0200 | [diff] [blame] | 404 | fpu_reset_state(fpu); |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 405 | return 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 408 | if (!access_ok(VERIFY_READ, buf, size)) |
| 409 | return -EACCES; |
| 410 | |
Ingo Molnar | c4d72e2 | 2015-04-27 07:18:17 +0200 | [diff] [blame] | 411 | fpu__activate_curr(fpu); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 412 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 413 | if (!static_cpu_has(X86_FEATURE_FPU)) |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 414 | return fpregs_soft_set(current, NULL, |
| 415 | 0, sizeof(struct user_i387_ia32_struct), |
| 416 | NULL, buf) != 0; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 417 | |
| 418 | if (use_xsave()) { |
| 419 | struct _fpx_sw_bytes fx_sw_user; |
| 420 | if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) { |
| 421 | /* |
| 422 | * Couldn't find the extended state information in the |
| 423 | * memory layout. Restore just the FP/SSE and init all |
| 424 | * the other extended state. |
| 425 | */ |
| 426 | state_size = sizeof(struct i387_fxsave_struct); |
| 427 | fx_only = 1; |
| 428 | } else { |
| 429 | state_size = fx_sw_user.xstate_size; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 430 | xfeatures = fx_sw_user.xfeatures; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | if (ia32_fxstate) { |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 435 | /* |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 436 | * For 32-bit frames with fxstate, copy the user state to the |
| 437 | * thread's fpu state, reconstruct fxstate from the fsave |
| 438 | * header. Sanitize the copied state etc. |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 439 | */ |
Oleg Nesterov | a7c80eb | 2015-03-13 09:53:09 +0100 | [diff] [blame] | 440 | struct fpu *fpu = &tsk->thread.fpu; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 441 | struct user_i387_ia32_struct env; |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 442 | int err = 0; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 443 | |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 444 | /* |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 445 | * Drop the current fpu which clears fpu->fpstate_active. This ensures |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 446 | * that any context-switch during the copy of the new state, |
| 447 | * avoids the intermediate state from getting restored/saved. |
| 448 | * Thus avoiding the new restored state from getting corrupted. |
| 449 | * We will be ready to restore/save the state only after |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 450 | * fpu->fpstate_active is again set. |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 451 | */ |
Ingo Molnar | ca6787b | 2015-04-23 12:33:50 +0200 | [diff] [blame] | 452 | drop_fpu(fpu); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 453 | |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 454 | if (__copy_from_user(&fpu->state.xsave, buf_fx, state_size) || |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 455 | __copy_from_user(&env, buf, sizeof(env))) { |
Ingo Molnar | c0ee2cf | 2015-04-03 13:01:52 +0200 | [diff] [blame] | 456 | fpstate_init(fpu); |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 457 | err = -1; |
| 458 | } else { |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 459 | sanitize_restored_xstate(tsk, &env, xfeatures, fx_only); |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 460 | } |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 461 | |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 462 | fpu->fpstate_active = 1; |
Oleg Nesterov | df24fb8 | 2014-09-02 19:57:17 +0200 | [diff] [blame] | 463 | if (use_eager_fpu()) { |
| 464 | preempt_disable(); |
Ingo Molnar | 3a0aee4 | 2015-04-22 13:16:47 +0200 | [diff] [blame] | 465 | fpu__restore(); |
Oleg Nesterov | df24fb8 | 2014-09-02 19:57:17 +0200 | [diff] [blame] | 466 | preempt_enable(); |
| 467 | } |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 468 | |
| 469 | return err; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 470 | } else { |
| 471 | /* |
| 472 | * For 64-bit frames and 32-bit fsave frames, restore the user |
| 473 | * state to the registers directly (with exceptions handled). |
| 474 | */ |
| 475 | user_fpu_begin(); |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 476 | if (restore_user_xstate(buf_fx, xfeatures, fx_only)) { |
Ingo Molnar | af2d94f | 2015-04-23 17:34:20 +0200 | [diff] [blame] | 477 | fpu_reset_state(fpu); |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 478 | return -1; |
| 479 | } |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 480 | } |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 481 | |
| 482 | return 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 483 | } |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 484 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 485 | /* |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 486 | * Prepare the SW reserved portion of the fxsave memory layout, indicating |
| 487 | * the presence of the extended state information in the memory layout |
| 488 | * pointed by the fpstate pointer in the sigcontext. |
| 489 | * This will be saved when ever the FP and extended state context is |
| 490 | * saved on the user stack during the signal handler delivery to the user. |
| 491 | */ |
roel kluin | 8bcad30 | 2008-10-21 19:49:09 -0400 | [diff] [blame] | 492 | static void prepare_fx_sw_frame(void) |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 493 | { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 494 | int fsave_header_size = sizeof(struct i387_fsave_struct); |
| 495 | int size = xstate_size + FP_XSTATE_MAGIC2_SIZE; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 496 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 497 | if (config_enabled(CONFIG_X86_32)) |
| 498 | size += fsave_header_size; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 499 | |
| 500 | fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1; |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 501 | fx_sw_reserved.extended_size = size; |
Ingo Molnar | 400e4b2 | 2015-04-24 10:19:47 +0200 | [diff] [blame] | 502 | fx_sw_reserved.xfeatures = xfeatures_mask; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 503 | fx_sw_reserved.xstate_size = xstate_size; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 504 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 505 | if (config_enabled(CONFIG_IA32_EMULATION)) { |
| 506 | fx_sw_reserved_ia32 = fx_sw_reserved; |
| 507 | fx_sw_reserved_ia32.extended_size += fsave_header_size; |
| 508 | } |
| 509 | } |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 510 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 511 | /* |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 512 | * Enable the extended processor state save/restore feature. |
| 513 | * Called once per CPU onlining. |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 514 | */ |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 515 | void fpu__init_cpu_xstate(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 516 | { |
Ingo Molnar | e84611f | 2015-04-25 06:41:07 +0200 | [diff] [blame] | 517 | if (!cpu_has_xsave || !xfeatures_mask) |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 518 | return; |
| 519 | |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 520 | cr4_set_bits(X86_CR4_OSXSAVE); |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 521 | xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 525 | * Record the offsets and sizes of different state managed by the xsave |
| 526 | * memory layout. |
| 527 | */ |
Robert Richter | 4995b9d | 2010-07-21 19:03:56 +0200 | [diff] [blame] | 528 | static void __init setup_xstate_features(void) |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 529 | { |
| 530 | int eax, ebx, ecx, edx, leaf = 0x2; |
| 531 | |
Ingo Molnar | 84246fe4 | 2015-04-24 09:23:59 +0200 | [diff] [blame] | 532 | xfeatures_nr = fls64(xfeatures_mask); |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 533 | |
| 534 | do { |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 535 | cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx); |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 536 | |
| 537 | if (eax == 0) |
| 538 | break; |
| 539 | |
| 540 | xstate_offsets[leaf] = ebx; |
| 541 | xstate_sizes[leaf] = eax; |
| 542 | |
| 543 | leaf++; |
| 544 | } while (1); |
| 545 | } |
| 546 | |
Ingo Molnar | 33588b5 | 2015-04-28 09:17:26 +0200 | [diff] [blame] | 547 | static void print_xstate_feature(u64 xstate_mask) |
Ingo Molnar | 69496e1 | 2015-04-24 08:48:01 +0200 | [diff] [blame] | 548 | { |
Ingo Molnar | 33588b5 | 2015-04-28 09:17:26 +0200 | [diff] [blame] | 549 | const char *feature_name; |
Ingo Molnar | 69496e1 | 2015-04-24 08:48:01 +0200 | [diff] [blame] | 550 | |
Ingo Molnar | 33588b5 | 2015-04-28 09:17:26 +0200 | [diff] [blame] | 551 | if (cpu_has_xfeatures(xstate_mask, &feature_name)) |
| 552 | pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name); |
Ingo Molnar | 69496e1 | 2015-04-24 08:48:01 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Print out all the supported xstate features: |
| 557 | */ |
| 558 | static void print_xstate_features(void) |
| 559 | { |
Ingo Molnar | 33588b5 | 2015-04-28 09:17:26 +0200 | [diff] [blame] | 560 | print_xstate_feature(XSTATE_FP); |
| 561 | print_xstate_feature(XSTATE_SSE); |
| 562 | print_xstate_feature(XSTATE_YMM); |
| 563 | print_xstate_feature(XSTATE_BNDREGS); |
| 564 | print_xstate_feature(XSTATE_BNDCSR); |
| 565 | print_xstate_feature(XSTATE_OPMASK); |
| 566 | print_xstate_feature(XSTATE_ZMM_Hi256); |
| 567 | print_xstate_feature(XSTATE_Hi16_ZMM); |
Ingo Molnar | 69496e1 | 2015-04-24 08:48:01 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 570 | /* |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 571 | * This function sets up offsets and sizes of all extended states in |
| 572 | * xsave area. This supports both standard format and compacted format |
| 573 | * of the xsave aread. |
| 574 | * |
| 575 | * Input: void |
| 576 | * Output: void |
| 577 | */ |
| 578 | void setup_xstate_comp(void) |
| 579 | { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 580 | unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8]; |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 581 | int i; |
| 582 | |
Fenghua Yu | 8ff925e | 2014-05-30 14:59:24 -0700 | [diff] [blame] | 583 | /* |
| 584 | * The FP xstates and SSE xstates are legacy states. They are always |
| 585 | * in the fixed offsets in the xsave area in either compacted form |
| 586 | * or standard form. |
| 587 | */ |
| 588 | xstate_comp_offsets[0] = 0; |
| 589 | xstate_comp_offsets[1] = offsetof(struct i387_fxsave_struct, xmm_space); |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 590 | |
| 591 | if (!cpu_has_xsaves) { |
Ingo Molnar | 84246fe4 | 2015-04-24 09:23:59 +0200 | [diff] [blame] | 592 | for (i = 2; i < xfeatures_nr; i++) { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 593 | if (test_bit(i, (unsigned long *)&xfeatures_mask)) { |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 594 | xstate_comp_offsets[i] = xstate_offsets[i]; |
| 595 | xstate_comp_sizes[i] = xstate_sizes[i]; |
| 596 | } |
| 597 | } |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE; |
| 602 | |
Ingo Molnar | 84246fe4 | 2015-04-24 09:23:59 +0200 | [diff] [blame] | 603 | for (i = 2; i < xfeatures_nr; i++) { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 604 | if (test_bit(i, (unsigned long *)&xfeatures_mask)) |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 605 | xstate_comp_sizes[i] = xstate_sizes[i]; |
| 606 | else |
| 607 | xstate_comp_sizes[i] = 0; |
| 608 | |
| 609 | if (i > 2) |
| 610 | xstate_comp_offsets[i] = xstate_comp_offsets[i-1] |
| 611 | + xstate_comp_sizes[i-1]; |
| 612 | |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 617 | * setup the xstate image representing the init state |
| 618 | */ |
Ingo Molnar | 26b1f5d | 2015-04-25 05:27:26 +0200 | [diff] [blame] | 619 | static void setup_init_fpu_buf(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 620 | { |
Ingo Molnar | 26b1f5d | 2015-04-25 05:27:26 +0200 | [diff] [blame] | 621 | static int on_boot_cpu = 1; |
| 622 | |
| 623 | if (!on_boot_cpu) |
| 624 | return; |
| 625 | on_boot_cpu = 0; |
| 626 | |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 627 | if (!cpu_has_xsave) |
| 628 | return; |
| 629 | |
| 630 | setup_xstate_features(); |
Ingo Molnar | 69496e1 | 2015-04-24 08:48:01 +0200 | [diff] [blame] | 631 | print_xstate_features(); |
Suresh Siddha | a1488f8 | 2010-07-19 16:05:48 -0700 | [diff] [blame] | 632 | |
Fenghua Yu | 47c2f29 | 2014-05-29 11:12:42 -0700 | [diff] [blame] | 633 | if (cpu_has_xsaves) { |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 634 | init_xstate_ctx.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask; |
| 635 | init_xstate_ctx.header.xfeatures = xfeatures_mask; |
Fenghua Yu | 47c2f29 | 2014-05-29 11:12:42 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 638 | /* |
| 639 | * Init all the features state with header_bv being 0x0 |
| 640 | */ |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 641 | xrstor_state_booting(&init_xstate_ctx, -1); |
Ingo Molnar | 3e261c1 | 2015-04-22 15:08:34 +0200 | [diff] [blame] | 642 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 643 | /* |
| 644 | * Dump the init state again. This is to identify the init state |
| 645 | * of any feature which is not represented by all zero's. |
| 646 | */ |
Ingo Molnar | 3e5e126 | 2015-04-25 05:08:17 +0200 | [diff] [blame] | 647 | xsave_state_booting(&init_xstate_ctx); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Fenghua Yu | 7e7ce87 | 2014-05-29 11:12:43 -0700 | [diff] [blame] | 650 | /* |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 651 | * Calculate total size of enabled xstates in XCR0/xfeatures_mask. |
Fenghua Yu | 7e7ce87 | 2014-05-29 11:12:43 -0700 | [diff] [blame] | 652 | */ |
| 653 | static void __init init_xstate_size(void) |
| 654 | { |
| 655 | unsigned int eax, ebx, ecx, edx; |
| 656 | int i; |
| 657 | |
| 658 | if (!cpu_has_xsaves) { |
| 659 | cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); |
| 660 | xstate_size = ebx; |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE; |
| 665 | for (i = 2; i < 64; i++) { |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 666 | if (test_bit(i, (unsigned long *)&xfeatures_mask)) { |
Fenghua Yu | 7e7ce87 | 2014-05-29 11:12:43 -0700 | [diff] [blame] | 667 | cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx); |
| 668 | xstate_size += eax; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 673 | /* |
| 674 | * Enable and initialize the xsave feature. |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 675 | * Called once per system bootup. |
Ingo Molnar | c0841e3 | 2015-04-24 03:18:28 +0200 | [diff] [blame] | 676 | * |
Ingo Molnar | c42103b | 2015-04-25 06:52:53 +0200 | [diff] [blame] | 677 | * ( Not marked __init because of false positive section warnings. ) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 678 | */ |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 679 | void fpu__init_system_xstate(void) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 680 | { |
| 681 | unsigned int eax, ebx, ecx, edx; |
Ingo Molnar | 62db687 | 2015-04-25 06:50:09 +0200 | [diff] [blame] | 682 | static bool on_boot_cpu = 1; |
| 683 | |
| 684 | if (!on_boot_cpu) |
| 685 | return; |
| 686 | on_boot_cpu = 0; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 687 | |
Ingo Molnar | e9dbfd6 | 2015-04-25 06:47:24 +0200 | [diff] [blame] | 688 | if (!cpu_has_xsave) { |
| 689 | pr_info("x86/fpu: Legacy x87 FPU detected.\n"); |
| 690 | return; |
| 691 | } |
| 692 | |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 693 | if (boot_cpu_data.cpuid_level < XSTATE_CPUID) { |
Ingo Molnar | 32d4d9c | 2015-04-24 03:25:18 +0200 | [diff] [blame] | 694 | WARN(1, "x86/fpu: XSTATE_CPUID missing!\n"); |
Robert Richter | ee813d5 | 2010-07-21 19:03:54 +0200 | [diff] [blame] | 695 | return; |
| 696 | } |
| 697 | |
| 698 | cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 699 | xfeatures_mask = eax + ((u64)edx << 32); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 700 | |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 701 | if ((xfeatures_mask & XSTATE_FPSSE) != XSTATE_FPSSE) { |
| 702 | pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 703 | BUG(); |
| 704 | } |
| 705 | |
| 706 | /* |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 707 | * Support only the state known to OS. |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 708 | */ |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 709 | xfeatures_mask = xfeatures_mask & XCNTXT_MASK; |
Robert Richter | 97e80a7 | 2010-07-21 19:03:53 +0200 | [diff] [blame] | 710 | |
Ingo Molnar | 55cc467 | 2015-04-25 06:26:36 +0200 | [diff] [blame] | 711 | /* Enable xstate instructions to be able to continue with initialization: */ |
| 712 | fpu__init_cpu_xstate(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * Recompute the context size for enabled features |
| 716 | */ |
Fenghua Yu | 7e7ce87 | 2014-05-29 11:12:43 -0700 | [diff] [blame] | 717 | init_xstate_size(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 718 | |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 719 | update_regset_xstate_info(xstate_size, xfeatures_mask); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 720 | prepare_fx_sw_frame(); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 721 | setup_init_fpu_buf(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 722 | |
Ingo Molnar | 32d4d9c | 2015-04-24 03:25:18 +0200 | [diff] [blame] | 723 | pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is 0x%x bytes, using '%s' format.\n", |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 724 | xfeatures_mask, |
Ingo Molnar | 32d4d9c | 2015-04-24 03:25:18 +0200 | [diff] [blame] | 725 | xstate_size, |
| 726 | cpu_has_xsaves ? "compacted" : "standard"); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 727 | } |
Robert Richter | 82d4150 | 2010-07-20 20:50:51 +0200 | [diff] [blame] | 728 | |
H. Peter Anvin | 1cff92d | 2010-07-21 14:23:10 -0700 | [diff] [blame] | 729 | /* |
Ingo Molnar | 9254aaa | 2015-04-24 10:02:32 +0200 | [diff] [blame] | 730 | * Restore minimal FPU state after suspend: |
| 731 | */ |
| 732 | void fpu__resume_cpu(void) |
| 733 | { |
| 734 | /* |
| 735 | * Restore XCR0 on xsave capable CPUs: |
| 736 | */ |
| 737 | if (cpu_has_xsave) |
| 738 | xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); |
| 739 | } |
| 740 | |
| 741 | /* |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 742 | * Given the xsave area and a state inside, this function returns the |
| 743 | * address of the state. |
| 744 | * |
| 745 | * This is the API that is called to get xstate address in either |
| 746 | * standard format or compacted format of xsave area. |
| 747 | * |
| 748 | * Inputs: |
| 749 | * xsave: base address of the xsave area; |
| 750 | * xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE, |
| 751 | * etc.) |
| 752 | * Output: |
| 753 | * address of the state in the xsave area. |
| 754 | */ |
| 755 | void *get_xsave_addr(struct xsave_struct *xsave, int xstate) |
| 756 | { |
| 757 | int feature = fls64(xstate) - 1; |
Ingo Molnar | 614df7f | 2015-04-24 09:20:33 +0200 | [diff] [blame] | 758 | if (!test_bit(feature, (unsigned long *)&xfeatures_mask)) |
Fenghua Yu | 7496d64 | 2014-05-29 11:12:44 -0700 | [diff] [blame] | 759 | return NULL; |
| 760 | |
| 761 | return (void *)xsave + xstate_comp_offsets[feature]; |
| 762 | } |
Paolo Bonzini | ba7b392 | 2014-11-24 10:57:42 +0100 | [diff] [blame] | 763 | EXPORT_SYMBOL_GPL(get_xsave_addr); |