i387.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * linux/arch/x86_64/kernel/i387.c
  3. *
  4. * Copyright (C) 1994 Linus Torvalds
  5. * Copyright (C) 2002 Andi Kleen, SuSE Labs
  6. *
  7. * Pentium III FXSR, SSE support
  8. * General FPU state handling cleanups
  9. * Gareth Hughes <gareth@valinux.com>, May 2000
  10. *
  11. * x86-64 rework 2002 Andi Kleen.
  12. * Does direct fxsave in and out of user space now for signal handlers.
  13. * All the FSAVE<->FXSAVE conversion code has been moved to the 32bit emulation,
  14. * the 64bit user space sees a FXSAVE frame directly.
  15. */
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <asm/processor.h>
  19. #include <asm/i387.h>
  20. #include <asm/sigcontext.h>
  21. #include <asm/user.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/uaccess.h>
  24. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffff;
  25. void mxcsr_feature_mask_init(void)
  26. {
  27. unsigned int mask;
  28. clts();
  29. memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  30. asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
  31. mask = current->thread.i387.fxsave.mxcsr_mask;
  32. if (mask == 0) mask = 0x0000ffbf;
  33. mxcsr_feature_mask &= mask;
  34. stts();
  35. }
  36. /*
  37. * Called at bootup to set up the initial FPU state that is later cloned
  38. * into all processes.
  39. */
  40. void __cpuinit fpu_init(void)
  41. {
  42. unsigned long oldcr0 = read_cr0();
  43. extern void __bad_fxsave_alignment(void);
  44. if (offsetof(struct task_struct, thread.i387.fxsave) & 15)
  45. __bad_fxsave_alignment();
  46. set_in_cr4(X86_CR4_OSFXSR);
  47. set_in_cr4(X86_CR4_OSXMMEXCPT);
  48. write_cr0(oldcr0 & ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */
  49. mxcsr_feature_mask_init();
  50. /* clean state in init */
  51. current_thread_info()->status = 0;
  52. clear_used_math();
  53. }
  54. void init_fpu(struct task_struct *child)
  55. {
  56. if (tsk_used_math(child)) {
  57. if (child == current)
  58. unlazy_fpu(child);
  59. return;
  60. }
  61. memset(&child->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  62. child->thread.i387.fxsave.cwd = 0x37f;
  63. child->thread.i387.fxsave.mxcsr = 0x1f80;
  64. /* only the device not available exception or ptrace can call init_fpu */
  65. set_stopped_child_used_math(child);
  66. }
  67. /*
  68. * Signal frame handlers.
  69. */
  70. int save_i387(struct _fpstate __user *buf)
  71. {
  72. struct task_struct *tsk = current;
  73. int err = 0;
  74. BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
  75. sizeof(tsk->thread.i387.fxsave));
  76. if ((unsigned long)buf % 16)
  77. printk("save_i387: bad fpstate %p\n",buf);
  78. if (!used_math())
  79. return 0;
  80. clear_used_math(); /* trigger finit */
  81. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  82. err = save_i387_checking((struct i387_fxsave_struct __user *)buf);
  83. if (err) return err;
  84. stts();
  85. } else {
  86. if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
  87. sizeof(struct i387_fxsave_struct)))
  88. return -1;
  89. }
  90. return 1;
  91. }
  92. /*
  93. * ptrace request handlers.
  94. */
  95. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
  96. {
  97. init_fpu(tsk);
  98. return __copy_to_user(buf, &tsk->thread.i387.fxsave,
  99. sizeof(struct user_i387_struct)) ? -EFAULT : 0;
  100. }
  101. int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
  102. {
  103. if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
  104. sizeof(struct user_i387_struct)))
  105. return -EFAULT;
  106. return 0;
  107. }
  108. /*
  109. * FPU state for core dumps.
  110. */
  111. int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
  112. {
  113. struct task_struct *tsk = current;
  114. if (!used_math())
  115. return 0;
  116. unlazy_fpu(tsk);
  117. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  118. return 1;
  119. }
  120. int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
  121. {
  122. int fpvalid = !!tsk_used_math(tsk);
  123. if (fpvalid) {
  124. if (tsk == current)
  125. unlazy_fpu(tsk);
  126. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  127. }
  128. return fpvalid;
  129. }