i387.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/config.h>
  17. #include <linux/sched.h>
  18. #include <linux/init.h>
  19. #include <asm/processor.h>
  20. #include <asm/i387.h>
  21. #include <asm/sigcontext.h>
  22. #include <asm/user.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/uaccess.h>
  25. unsigned int mxcsr_feature_mask = 0xffffffff;
  26. void mxcsr_feature_mask_init(void)
  27. {
  28. unsigned int mask;
  29. clts();
  30. memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  31. asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
  32. mask = current->thread.i387.fxsave.mxcsr_mask;
  33. if (mask == 0) mask = 0x0000ffbf;
  34. mxcsr_feature_mask &= mask;
  35. stts();
  36. }
  37. /*
  38. * Called at bootup to set up the initial FPU state that is later cloned
  39. * into all processes.
  40. */
  41. void __cpuinit fpu_init(void)
  42. {
  43. unsigned long oldcr0 = read_cr0();
  44. extern void __bad_fxsave_alignment(void);
  45. if (offsetof(struct task_struct, thread.i387.fxsave) & 15)
  46. __bad_fxsave_alignment();
  47. set_in_cr4(X86_CR4_OSFXSR);
  48. set_in_cr4(X86_CR4_OSXMMEXCPT);
  49. write_cr0(oldcr0 & ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */
  50. mxcsr_feature_mask_init();
  51. /* clean state in init */
  52. current_thread_info()->status = 0;
  53. clear_used_math();
  54. }
  55. void init_fpu(struct task_struct *child)
  56. {
  57. if (tsk_used_math(child)) {
  58. if (child == current)
  59. unlazy_fpu(child);
  60. return;
  61. }
  62. memset(&child->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  63. child->thread.i387.fxsave.cwd = 0x37f;
  64. child->thread.i387.fxsave.mxcsr = 0x1f80;
  65. /* only the device not available exception or ptrace can call init_fpu */
  66. set_stopped_child_used_math(child);
  67. }
  68. /*
  69. * Signal frame handlers.
  70. */
  71. int save_i387(struct _fpstate __user *buf)
  72. {
  73. struct task_struct *tsk = current;
  74. int err = 0;
  75. {
  76. extern void bad_user_i387_struct(void);
  77. if (sizeof(struct user_i387_struct) != sizeof(tsk->thread.i387.fxsave))
  78. bad_user_i387_struct();
  79. }
  80. if ((unsigned long)buf % 16)
  81. printk("save_i387: bad fpstate %p\n",buf);
  82. if (!used_math())
  83. return 0;
  84. clear_used_math(); /* trigger finit */
  85. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  86. err = save_i387_checking((struct i387_fxsave_struct __user *)buf);
  87. if (err) return err;
  88. stts();
  89. } else {
  90. if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
  91. sizeof(struct i387_fxsave_struct)))
  92. return -1;
  93. }
  94. return 1;
  95. }
  96. /*
  97. * ptrace request handlers.
  98. */
  99. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
  100. {
  101. init_fpu(tsk);
  102. return __copy_to_user(buf, &tsk->thread.i387.fxsave,
  103. sizeof(struct user_i387_struct)) ? -EFAULT : 0;
  104. }
  105. int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
  106. {
  107. if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
  108. sizeof(struct user_i387_struct)))
  109. return -EFAULT;
  110. return 0;
  111. }
  112. /*
  113. * FPU state for core dumps.
  114. */
  115. int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
  116. {
  117. struct task_struct *tsk = current;
  118. if (!used_math())
  119. return 0;
  120. unlazy_fpu(tsk);
  121. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  122. return 1;
  123. }
  124. int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
  125. {
  126. int fpvalid = !!tsk_used_math(tsk);
  127. if (fpvalid) {
  128. if (tsk == current)
  129. unlazy_fpu(tsk);
  130. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  131. }
  132. return fpvalid;
  133. }