i387.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. {
  75. extern void bad_user_i387_struct(void);
  76. if (sizeof(struct user_i387_struct) != sizeof(tsk->thread.i387.fxsave))
  77. bad_user_i387_struct();
  78. }
  79. if ((unsigned long)buf % 16)
  80. printk("save_i387: bad fpstate %p\n",buf);
  81. if (!used_math())
  82. return 0;
  83. clear_used_math(); /* trigger finit */
  84. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  85. err = save_i387_checking((struct i387_fxsave_struct __user *)buf);
  86. if (err) return err;
  87. stts();
  88. } else {
  89. if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
  90. sizeof(struct i387_fxsave_struct)))
  91. return -1;
  92. }
  93. return 1;
  94. }
  95. /*
  96. * ptrace request handlers.
  97. */
  98. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
  99. {
  100. init_fpu(tsk);
  101. return __copy_to_user(buf, &tsk->thread.i387.fxsave,
  102. sizeof(struct user_i387_struct)) ? -EFAULT : 0;
  103. }
  104. int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
  105. {
  106. if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
  107. sizeof(struct user_i387_struct)))
  108. return -EFAULT;
  109. return 0;
  110. }
  111. /*
  112. * FPU state for core dumps.
  113. */
  114. int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
  115. {
  116. struct task_struct *tsk = current;
  117. if (!used_math())
  118. return 0;
  119. unlazy_fpu(tsk);
  120. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  121. return 1;
  122. }
  123. int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
  124. {
  125. int fpvalid = !!tsk_used_math(tsk);
  126. if (fpvalid) {
  127. if (tsk == current)
  128. unlazy_fpu(tsk);
  129. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  130. }
  131. return fpvalid;
  132. }