i387_64.c 3.6 KB

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