Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FPU signal frame handling routines. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/compat.h> |
| 6 | #include <linux/cpu.h> |
| 7 | |
| 8 | #include <asm/fpu/internal.h> |
| 9 | #include <asm/fpu/signal.h> |
| 10 | #include <asm/fpu/regset.h> |
Yu-cheng Yu | 99aa22d | 2016-05-20 10:47:08 -0700 | [diff] [blame] | 11 | #include <asm/fpu/xstate.h> |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 12 | |
| 13 | #include <asm/sigframe.h> |
Dave Hansen | d1898b7 | 2016-06-01 10:42:20 -0700 | [diff] [blame] | 14 | #include <asm/trace/fpu.h> |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 15 | |
| 16 | static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32; |
| 17 | |
| 18 | /* |
| 19 | * Check for the presence of extended state information in the |
| 20 | * user fpstate pointer in the sigcontext. |
| 21 | */ |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 22 | static inline int check_for_xstate(struct fxregs_state __user *buf, |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 23 | void __user *fpstate, |
| 24 | struct _fpx_sw_bytes *fx_sw) |
| 25 | { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 26 | int min_xstate_size = sizeof(struct fxregs_state) + |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 27 | sizeof(struct xstate_header); |
| 28 | unsigned int magic2; |
| 29 | |
| 30 | if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw))) |
| 31 | return -1; |
| 32 | |
| 33 | /* Check for the first magic field and other error scenarios. */ |
| 34 | if (fx_sw->magic1 != FP_XSTATE_MAGIC1 || |
| 35 | fx_sw->xstate_size < min_xstate_size || |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 36 | fx_sw->xstate_size > fpu_user_xstate_size || |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 37 | fx_sw->xstate_size > fx_sw->extended_size) |
| 38 | return -1; |
| 39 | |
| 40 | /* |
| 41 | * Check for the presence of second magic word at the end of memory |
| 42 | * layout. This detects the case where the user just copied the legacy |
| 43 | * fpstate layout with out copying the extended state information |
| 44 | * in the memory layout. |
| 45 | */ |
| 46 | if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size)) |
| 47 | || magic2 != FP_XSTATE_MAGIC2) |
| 48 | return -1; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Signal frame handlers. |
| 55 | */ |
| 56 | static inline int save_fsave_header(struct task_struct *tsk, void __user *buf) |
| 57 | { |
| 58 | if (use_fxsr()) { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 59 | struct xregs_state *xsave = &tsk->thread.fpu.state.xsave; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 60 | struct user_i387_ia32_struct env; |
Ingo Molnar | 86e9fc3 | 2015-09-05 09:32:36 +0200 | [diff] [blame] | 61 | struct _fpstate_32 __user *fp = buf; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 62 | |
| 63 | convert_from_fxsr(&env, tsk); |
| 64 | |
| 65 | if (__copy_to_user(buf, &env, sizeof(env)) || |
| 66 | __put_user(xsave->i387.swd, &fp->status) || |
| 67 | __put_user(X86_FXSR_MAGIC, &fp->magic)) |
| 68 | return -1; |
| 69 | } else { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 70 | struct fregs_state __user *fp = buf; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 71 | u32 swd; |
| 72 | if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status)) |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static inline int save_xstate_epilog(void __user *buf, int ia32_frame) |
| 80 | { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 81 | struct xregs_state __user *x = buf; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 82 | struct _fpx_sw_bytes *sw_bytes; |
| 83 | u32 xfeatures; |
| 84 | int err; |
| 85 | |
| 86 | /* Setup the bytes not touched by the [f]xsave and reserved for SW. */ |
| 87 | sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved; |
| 88 | err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes)); |
| 89 | |
| 90 | if (!use_xsave()) |
| 91 | return err; |
| 92 | |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 93 | err |= __put_user(FP_XSTATE_MAGIC2, |
| 94 | (__u32 *)(buf + fpu_user_xstate_size)); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Read the xfeatures which we copied (directly from the cpu or |
| 98 | * from the state in task struct) to the user buffers. |
| 99 | */ |
| 100 | err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures); |
| 101 | |
| 102 | /* |
| 103 | * For legacy compatible, we always set FP/SSE bits in the bit |
| 104 | * vector while saving the state to the user context. This will |
| 105 | * enable us capturing any changes(during sigreturn) to |
| 106 | * the FP/SSE bits by the legacy applications which don't touch |
| 107 | * xfeatures in the xsave header. |
| 108 | * |
| 109 | * xsave aware apps can change the xfeatures in the xsave |
| 110 | * header as well as change any contents in the memory layout. |
| 111 | * xrestore as part of sigreturn will capture all the changes. |
| 112 | */ |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 113 | xfeatures |= XFEATURE_MASK_FPSSE; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 114 | |
| 115 | err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures); |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 120 | static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf) |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 121 | { |
| 122 | int err; |
| 123 | |
| 124 | if (use_xsave()) |
| 125 | err = copy_xregs_to_user(buf); |
| 126 | else if (use_fxsr()) |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 127 | err = copy_fxregs_to_user((struct fxregs_state __user *) buf); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 128 | else |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 129 | err = copy_fregs_to_user((struct fregs_state __user *) buf); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 130 | |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 131 | if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size)) |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 132 | err = -EFAULT; |
| 133 | return err; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Save the fpu, extended register state to the user signal frame. |
| 138 | * |
| 139 | * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save |
| 140 | * state is copied. |
| 141 | * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'. |
| 142 | * |
| 143 | * buf == buf_fx for 64-bit frames and 32-bit fsave frame. |
| 144 | * buf != buf_fx for 32-bit frames with fxstate. |
| 145 | * |
| 146 | * If the fpu, extended register state is live, save the state directly |
| 147 | * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise, |
| 148 | * copy the thread's fpu state to the user frame starting at 'buf_fx'. |
| 149 | * |
| 150 | * If this is a 32-bit frame with fxstate, put a fsave header before |
| 151 | * the aligned state at 'buf_fx'. |
| 152 | * |
| 153 | * For [f]xsave state, update the SW reserved fields in the [f]xsave frame |
| 154 | * indicating the absence/presence of the extended state to the user. |
| 155 | */ |
| 156 | int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) |
| 157 | { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 158 | struct xregs_state *xsave = ¤t->thread.fpu.state.xsave; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 159 | struct task_struct *tsk = current; |
| 160 | int ia32_fxstate = (buf != buf_fx); |
| 161 | |
Masahiro Yamada | 97f2645 | 2016-08-03 13:45:50 -0700 | [diff] [blame] | 162 | ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) || |
| 163 | IS_ENABLED(CONFIG_IA32_EMULATION)); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 164 | |
| 165 | if (!access_ok(VERIFY_WRITE, buf, size)) |
| 166 | return -EACCES; |
| 167 | |
| 168 | if (!static_cpu_has(X86_FEATURE_FPU)) |
| 169 | return fpregs_soft_get(current, NULL, 0, |
| 170 | sizeof(struct user_i387_ia32_struct), NULL, |
Ingo Molnar | 86e9fc3 | 2015-09-05 09:32:36 +0200 | [diff] [blame] | 171 | (struct _fpstate_32 __user *) buf) ? -1 : 1; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 172 | |
Yu-cheng Yu | 99aa22d | 2016-05-20 10:47:08 -0700 | [diff] [blame] | 173 | if (fpregs_active() || using_compacted_format()) { |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 174 | /* Save the live register state to the user directly. */ |
| 175 | if (copy_fpregs_to_sigframe(buf_fx)) |
| 176 | return -1; |
| 177 | /* Update the thread's fxstate to save the fsave header. */ |
| 178 | if (ia32_fxstate) |
| 179 | copy_fxregs_to_kernel(&tsk->thread.fpu); |
| 180 | } else { |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 181 | /* |
| 182 | * It is a *bug* if kernel uses compacted-format for xsave |
| 183 | * area and we copy it out directly to a signal frame. It |
| 184 | * should have been handled above by saving the registers |
| 185 | * directly. |
| 186 | */ |
| 187 | if (boot_cpu_has(X86_FEATURE_XSAVES)) { |
| 188 | WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 192 | fpstate_sanitize_xstate(&tsk->thread.fpu); |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 193 | if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size)) |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | /* Save the fsave header for the 32-bit frames. */ |
| 198 | if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf)) |
| 199 | return -1; |
| 200 | |
| 201 | if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate)) |
| 202 | return -1; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static inline void |
| 208 | sanitize_restored_xstate(struct task_struct *tsk, |
| 209 | struct user_i387_ia32_struct *ia32_env, |
| 210 | u64 xfeatures, int fx_only) |
| 211 | { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 212 | struct xregs_state *xsave = &tsk->thread.fpu.state.xsave; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 213 | struct xstate_header *header = &xsave->header; |
| 214 | |
| 215 | if (use_xsave()) { |
| 216 | /* These bits must be zero. */ |
| 217 | memset(header->reserved, 0, 48); |
| 218 | |
| 219 | /* |
| 220 | * Init the state that is not present in the memory |
| 221 | * layout and not enabled by the OS. |
| 222 | */ |
| 223 | if (fx_only) |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 224 | header->xfeatures = XFEATURE_MASK_FPSSE; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 225 | else |
| 226 | header->xfeatures &= (xfeatures_mask & xfeatures); |
| 227 | } |
| 228 | |
| 229 | if (use_fxsr()) { |
| 230 | /* |
| 231 | * mscsr reserved bits must be masked to zero for security |
| 232 | * reasons. |
| 233 | */ |
| 234 | xsave->i387.mxcsr &= mxcsr_feature_mask; |
| 235 | |
| 236 | convert_to_fxsr(tsk, ia32_env); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Restore the extended state if present. Otherwise, restore the FP/SSE state. |
| 242 | */ |
| 243 | static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only) |
| 244 | { |
| 245 | if (use_xsave()) { |
| 246 | if ((unsigned long)buf % 64 || fx_only) { |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 247 | u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 248 | copy_kernel_to_xregs(&init_fpstate.xsave, init_bv); |
| 249 | return copy_user_to_fxregs(buf); |
| 250 | } else { |
| 251 | u64 init_bv = xfeatures_mask & ~xbv; |
| 252 | if (unlikely(init_bv)) |
| 253 | copy_kernel_to_xregs(&init_fpstate.xsave, init_bv); |
| 254 | return copy_user_to_xregs(buf, xbv); |
| 255 | } |
| 256 | } else if (use_fxsr()) { |
| 257 | return copy_user_to_fxregs(buf); |
| 258 | } else |
| 259 | return copy_user_to_fregs(buf); |
| 260 | } |
| 261 | |
| 262 | static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) |
| 263 | { |
| 264 | int ia32_fxstate = (buf != buf_fx); |
| 265 | struct task_struct *tsk = current; |
| 266 | struct fpu *fpu = &tsk->thread.fpu; |
Fenghua Yu | bf15a8c | 2016-05-20 10:47:06 -0700 | [diff] [blame] | 267 | int state_size = fpu_kernel_xstate_size; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 268 | u64 xfeatures = 0; |
| 269 | int fx_only = 0; |
| 270 | |
Masahiro Yamada | 97f2645 | 2016-08-03 13:45:50 -0700 | [diff] [blame] | 271 | ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) || |
| 272 | IS_ENABLED(CONFIG_IA32_EMULATION)); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 273 | |
| 274 | if (!buf) { |
| 275 | fpu__clear(fpu); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | if (!access_ok(VERIFY_READ, buf, size)) |
| 280 | return -EACCES; |
| 281 | |
| 282 | fpu__activate_curr(fpu); |
| 283 | |
| 284 | if (!static_cpu_has(X86_FEATURE_FPU)) |
| 285 | return fpregs_soft_set(current, NULL, |
| 286 | 0, sizeof(struct user_i387_ia32_struct), |
| 287 | NULL, buf) != 0; |
| 288 | |
| 289 | if (use_xsave()) { |
| 290 | struct _fpx_sw_bytes fx_sw_user; |
| 291 | if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) { |
| 292 | /* |
| 293 | * Couldn't find the extended state information in the |
| 294 | * memory layout. Restore just the FP/SSE and init all |
| 295 | * the other extended state. |
| 296 | */ |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 297 | state_size = sizeof(struct fxregs_state); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 298 | fx_only = 1; |
Dave Hansen | d1898b7 | 2016-06-01 10:42:20 -0700 | [diff] [blame] | 299 | trace_x86_fpu_xstate_check_failed(fpu); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 300 | } else { |
| 301 | state_size = fx_sw_user.xstate_size; |
| 302 | xfeatures = fx_sw_user.xfeatures; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (ia32_fxstate) { |
| 307 | /* |
| 308 | * For 32-bit frames with fxstate, copy the user state to the |
| 309 | * thread's fpu state, reconstruct fxstate from the fsave |
| 310 | * header. Sanitize the copied state etc. |
| 311 | */ |
| 312 | struct fpu *fpu = &tsk->thread.fpu; |
| 313 | struct user_i387_ia32_struct env; |
| 314 | int err = 0; |
| 315 | |
| 316 | /* |
| 317 | * Drop the current fpu which clears fpu->fpstate_active. This ensures |
| 318 | * that any context-switch during the copy of the new state, |
| 319 | * avoids the intermediate state from getting restored/saved. |
| 320 | * Thus avoiding the new restored state from getting corrupted. |
| 321 | * We will be ready to restore/save the state only after |
| 322 | * fpu->fpstate_active is again set. |
| 323 | */ |
| 324 | fpu__drop(fpu); |
| 325 | |
Yu-cheng Yu | 1fc2b67 | 2016-07-11 09:18:54 -0700 | [diff] [blame] | 326 | if (using_compacted_format()) { |
| 327 | err = copyin_to_xsaves(NULL, buf_fx, |
| 328 | &fpu->state.xsave); |
| 329 | } else { |
| 330 | err = __copy_from_user(&fpu->state.xsave, |
| 331 | buf_fx, state_size); |
Eric Biggers | 5e9b07f | 2017-10-02 11:01:40 -0700 | [diff] [blame] | 332 | |
| 333 | /* xcomp_bv must be 0 when using uncompacted format */ |
| 334 | if (!err && state_size > offsetof(struct xregs_state, header) && fpu->state.xsave.header.xcomp_bv) |
| 335 | err = -EINVAL; |
Yu-cheng Yu | 1fc2b67 | 2016-07-11 09:18:54 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (err || __copy_from_user(&env, buf, sizeof(env))) { |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 339 | fpstate_init(&fpu->state); |
Dave Hansen | d1898b7 | 2016-06-01 10:42:20 -0700 | [diff] [blame] | 340 | trace_x86_fpu_init_state(fpu); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 341 | err = -1; |
| 342 | } else { |
| 343 | sanitize_restored_xstate(tsk, &env, xfeatures, fx_only); |
| 344 | } |
| 345 | |
| 346 | fpu->fpstate_active = 1; |
Andy Lutomirski | 0fd0aa7 | 2016-10-04 20:34:33 -0400 | [diff] [blame] | 347 | preempt_disable(); |
| 348 | fpu__restore(fpu); |
| 349 | preempt_enable(); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 350 | |
| 351 | return err; |
| 352 | } else { |
| 353 | /* |
| 354 | * For 64-bit frames and 32-bit fsave frames, restore the user |
| 355 | * state to the registers directly (with exceptions handled). |
| 356 | */ |
| 357 | user_fpu_begin(); |
| 358 | if (copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only)) { |
| 359 | fpu__clear(fpu); |
| 360 | return -1; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static inline int xstate_sigframe_size(void) |
| 368 | { |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 369 | return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE : |
| 370 | fpu_user_xstate_size; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Restore FPU state from a sigframe: |
| 375 | */ |
| 376 | int fpu__restore_sig(void __user *buf, int ia32_frame) |
| 377 | { |
| 378 | void __user *buf_fx = buf; |
| 379 | int size = xstate_sigframe_size(); |
| 380 | |
| 381 | if (ia32_frame && use_fxsr()) { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 382 | buf_fx = buf + sizeof(struct fregs_state); |
| 383 | size += sizeof(struct fregs_state); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return __fpu__restore_sig(buf, buf_fx, size); |
| 387 | } |
| 388 | |
| 389 | unsigned long |
| 390 | fpu__alloc_mathframe(unsigned long sp, int ia32_frame, |
| 391 | unsigned long *buf_fx, unsigned long *size) |
| 392 | { |
| 393 | unsigned long frame_size = xstate_sigframe_size(); |
| 394 | |
| 395 | *buf_fx = sp = round_down(sp - frame_size, 64); |
| 396 | if (ia32_frame && use_fxsr()) { |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 397 | frame_size += sizeof(struct fregs_state); |
| 398 | sp -= sizeof(struct fregs_state); |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | *size = frame_size; |
| 402 | |
| 403 | return sp; |
| 404 | } |
| 405 | /* |
| 406 | * Prepare the SW reserved portion of the fxsave memory layout, indicating |
| 407 | * the presence of the extended state information in the memory layout |
| 408 | * pointed by the fpstate pointer in the sigcontext. |
| 409 | * This will be saved when ever the FP and extended state context is |
| 410 | * saved on the user stack during the signal handler delivery to the user. |
| 411 | */ |
| 412 | void fpu__init_prepare_fx_sw_frame(void) |
| 413 | { |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 414 | int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 415 | |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 416 | fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1; |
| 417 | fx_sw_reserved.extended_size = size; |
| 418 | fx_sw_reserved.xfeatures = xfeatures_mask; |
Fenghua Yu | a1141e0 | 2016-05-20 10:47:05 -0700 | [diff] [blame] | 419 | fx_sw_reserved.xstate_size = fpu_user_xstate_size; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 420 | |
Masahiro Yamada | 97f2645 | 2016-08-03 13:45:50 -0700 | [diff] [blame] | 421 | if (IS_ENABLED(CONFIG_IA32_EMULATION) || |
| 422 | IS_ENABLED(CONFIG_X86_32)) { |
Dave Hansen | ab6b529 | 2015-11-10 16:23:54 -0800 | [diff] [blame] | 423 | int fsave_header_size = sizeof(struct fregs_state); |
| 424 | |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 425 | fx_sw_reserved_ia32 = fx_sw_reserved; |
Dave Hansen | ab6b529 | 2015-11-10 16:23:54 -0800 | [diff] [blame] | 426 | fx_sw_reserved_ia32.extended_size = size + fsave_header_size; |
Ingo Molnar | b992c66 | 2015-04-30 12:45:38 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |