diff options
Diffstat (limited to 'arch/x86/include/asm/i387.h')
-rw-r--r-- | arch/x86/include/asm/i387.h | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h index a850b4d8d14..247904945d3 100644 --- a/arch/x86/include/asm/i387.h +++ b/arch/x86/include/asm/i387.h | |||
@@ -29,10 +29,11 @@ extern unsigned int sig_xstate_size; | |||
29 | extern void fpu_init(void); | 29 | extern void fpu_init(void); |
30 | extern void mxcsr_feature_mask_init(void); | 30 | extern void mxcsr_feature_mask_init(void); |
31 | extern int init_fpu(struct task_struct *child); | 31 | extern int init_fpu(struct task_struct *child); |
32 | extern void __math_state_restore(struct task_struct *); | ||
33 | extern void math_state_restore(void); | 32 | extern void math_state_restore(void); |
34 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); | 33 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); |
35 | 34 | ||
35 | DECLARE_PER_CPU(struct task_struct *, fpu_owner_task); | ||
36 | |||
36 | extern user_regset_active_fn fpregs_active, xfpregs_active; | 37 | extern user_regset_active_fn fpregs_active, xfpregs_active; |
37 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, | 38 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, |
38 | xstateregs_get; | 39 | xstateregs_get; |
@@ -269,6 +270,16 @@ static inline int fpu_restore_checking(struct fpu *fpu) | |||
269 | 270 | ||
270 | static inline int restore_fpu_checking(struct task_struct *tsk) | 271 | static inline int restore_fpu_checking(struct task_struct *tsk) |
271 | { | 272 | { |
273 | /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception | ||
274 | is pending. Clear the x87 state here by setting it to fixed | ||
275 | values. "m" is a random variable that should be in L1 */ | ||
276 | alternative_input( | ||
277 | ASM_NOP8 ASM_NOP2, | ||
278 | "emms\n\t" /* clear stack tags */ | ||
279 | "fildl %P[addr]", /* set F?P to defined value */ | ||
280 | X86_FEATURE_FXSAVE_LEAK, | ||
281 | [addr] "m" (tsk->thread.fpu.has_fpu)); | ||
282 | |||
272 | return fpu_restore_checking(&tsk->thread.fpu); | 283 | return fpu_restore_checking(&tsk->thread.fpu); |
273 | } | 284 | } |
274 | 285 | ||
@@ -279,19 +290,21 @@ static inline int restore_fpu_checking(struct task_struct *tsk) | |||
279 | */ | 290 | */ |
280 | static inline int __thread_has_fpu(struct task_struct *tsk) | 291 | static inline int __thread_has_fpu(struct task_struct *tsk) |
281 | { | 292 | { |
282 | return tsk->thread.has_fpu; | 293 | return tsk->thread.fpu.has_fpu; |
283 | } | 294 | } |
284 | 295 | ||
285 | /* Must be paired with an 'stts' after! */ | 296 | /* Must be paired with an 'stts' after! */ |
286 | static inline void __thread_clear_has_fpu(struct task_struct *tsk) | 297 | static inline void __thread_clear_has_fpu(struct task_struct *tsk) |
287 | { | 298 | { |
288 | tsk->thread.has_fpu = 0; | 299 | tsk->thread.fpu.has_fpu = 0; |
300 | percpu_write(fpu_owner_task, NULL); | ||
289 | } | 301 | } |
290 | 302 | ||
291 | /* Must be paired with a 'clts' before! */ | 303 | /* Must be paired with a 'clts' before! */ |
292 | static inline void __thread_set_has_fpu(struct task_struct *tsk) | 304 | static inline void __thread_set_has_fpu(struct task_struct *tsk) |
293 | { | 305 | { |
294 | tsk->thread.has_fpu = 1; | 306 | tsk->thread.fpu.has_fpu = 1; |
307 | percpu_write(fpu_owner_task, tsk); | ||
295 | } | 308 | } |
296 | 309 | ||
297 | /* | 310 | /* |
@@ -336,30 +349,36 @@ typedef struct { int preload; } fpu_switch_t; | |||
336 | * We don't do that yet, so "fpu_lazy_restore()" always returns | 349 | * We don't do that yet, so "fpu_lazy_restore()" always returns |
337 | * false, but some day.. | 350 | * false, but some day.. |
338 | */ | 351 | */ |
339 | #define fpu_lazy_restore(tsk) (0) | 352 | static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu) |
340 | #define fpu_lazy_state_intact(tsk) do { } while (0) | 353 | { |
354 | return new == percpu_read_stable(fpu_owner_task) && | ||
355 | cpu == new->thread.fpu.last_cpu; | ||
356 | } | ||
341 | 357 | ||
342 | static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new) | 358 | static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu) |
343 | { | 359 | { |
344 | fpu_switch_t fpu; | 360 | fpu_switch_t fpu; |
345 | 361 | ||
346 | fpu.preload = tsk_used_math(new) && new->fpu_counter > 5; | 362 | fpu.preload = tsk_used_math(new) && new->fpu_counter > 5; |
347 | if (__thread_has_fpu(old)) { | 363 | if (__thread_has_fpu(old)) { |
348 | if (__save_init_fpu(old)) | 364 | if (!__save_init_fpu(old)) |
349 | fpu_lazy_state_intact(old); | 365 | cpu = ~0; |
350 | __thread_clear_has_fpu(old); | 366 | old->thread.fpu.last_cpu = cpu; |
351 | old->fpu_counter++; | 367 | old->thread.fpu.has_fpu = 0; /* But leave fpu_owner_task! */ |
352 | 368 | ||
353 | /* Don't change CR0.TS if we just switch! */ | 369 | /* Don't change CR0.TS if we just switch! */ |
354 | if (fpu.preload) { | 370 | if (fpu.preload) { |
371 | new->fpu_counter++; | ||
355 | __thread_set_has_fpu(new); | 372 | __thread_set_has_fpu(new); |
356 | prefetch(new->thread.fpu.state); | 373 | prefetch(new->thread.fpu.state); |
357 | } else | 374 | } else |
358 | stts(); | 375 | stts(); |
359 | } else { | 376 | } else { |
360 | old->fpu_counter = 0; | 377 | old->fpu_counter = 0; |
378 | old->thread.fpu.last_cpu = ~0; | ||
361 | if (fpu.preload) { | 379 | if (fpu.preload) { |
362 | if (fpu_lazy_restore(new)) | 380 | new->fpu_counter++; |
381 | if (fpu_lazy_restore(new, cpu)) | ||
363 | fpu.preload = 0; | 382 | fpu.preload = 0; |
364 | else | 383 | else |
365 | prefetch(new->thread.fpu.state); | 384 | prefetch(new->thread.fpu.state); |
@@ -377,8 +396,10 @@ static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct ta | |||
377 | */ | 396 | */ |
378 | static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) | 397 | static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) |
379 | { | 398 | { |
380 | if (fpu.preload) | 399 | if (fpu.preload) { |
381 | __math_state_restore(new); | 400 | if (unlikely(restore_fpu_checking(new))) |
401 | __thread_fpu_end(new); | ||
402 | } | ||
382 | } | 403 | } |
383 | 404 | ||
384 | /* | 405 | /* |
@@ -451,8 +472,10 @@ static inline void kernel_fpu_begin(void) | |||
451 | __save_init_fpu(me); | 472 | __save_init_fpu(me); |
452 | __thread_clear_has_fpu(me); | 473 | __thread_clear_has_fpu(me); |
453 | /* We do 'stts()' in kernel_fpu_end() */ | 474 | /* We do 'stts()' in kernel_fpu_end() */ |
454 | } else | 475 | } else { |
476 | percpu_write(fpu_owner_task, NULL); | ||
455 | clts(); | 477 | clts(); |
478 | } | ||
456 | } | 479 | } |
457 | 480 | ||
458 | static inline void kernel_fpu_end(void) | 481 | static inline void kernel_fpu_end(void) |