fpu32.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * FXSAVE<->i387 conversion support. Based on code by Gareth Hughes.
  4. * This is used for ptrace, signals and coredumps in 32bit emulation.
  5. */
  6. #include <linux/sched.h>
  7. #include <asm/sigcontext32.h>
  8. #include <asm/processor.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/i387.h>
  11. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  12. {
  13. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  14. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  15. tmp = ~twd;
  16. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  17. /* and move the valid bits to the lower byte. */
  18. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  19. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  20. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  21. return tmp;
  22. }
  23. static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  24. {
  25. struct _fpxreg *st = NULL;
  26. unsigned long tos = (fxsave->swd >> 11) & 7;
  27. unsigned long twd = (unsigned long) fxsave->twd;
  28. unsigned long tag;
  29. unsigned long ret = 0xffff0000;
  30. int i;
  31. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
  32. for (i = 0 ; i < 8 ; i++) {
  33. if (twd & 0x1) {
  34. st = FPREG_ADDR( fxsave, (i - tos) & 7 );
  35. switch (st->exponent & 0x7fff) {
  36. case 0x7fff:
  37. tag = 2; /* Special */
  38. break;
  39. case 0x0000:
  40. if ( !st->significand[0] &&
  41. !st->significand[1] &&
  42. !st->significand[2] &&
  43. !st->significand[3] ) {
  44. tag = 1; /* Zero */
  45. } else {
  46. tag = 2; /* Special */
  47. }
  48. break;
  49. default:
  50. if (st->significand[3] & 0x8000) {
  51. tag = 0; /* Valid */
  52. } else {
  53. tag = 2; /* Special */
  54. }
  55. break;
  56. }
  57. } else {
  58. tag = 3; /* Empty */
  59. }
  60. ret |= (tag << (2 * i));
  61. twd = twd >> 1;
  62. }
  63. return ret;
  64. }
  65. static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
  66. struct _fpstate_ia32 __user *buf)
  67. {
  68. struct _fpxreg *to;
  69. struct _fpreg __user *from;
  70. int i;
  71. u32 v;
  72. int err = 0;
  73. #define G(num,val) err |= __get_user(val, num + (u32 __user *)buf)
  74. G(0, fxsave->cwd);
  75. G(1, fxsave->swd);
  76. G(2, fxsave->twd);
  77. fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
  78. G(3, fxsave->rip);
  79. G(4, v);
  80. fxsave->fop = v>>16; /* cs ignored */
  81. G(5, fxsave->rdp);
  82. /* 6: ds ignored */
  83. #undef G
  84. if (err)
  85. return -1;
  86. to = (struct _fpxreg *)&fxsave->st_space[0];
  87. from = &buf->_st[0];
  88. for (i = 0 ; i < 8 ; i++, to++, from++) {
  89. if (__copy_from_user(to, from, sizeof(*from)))
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user *buf,
  95. struct i387_fxsave_struct *fxsave,
  96. struct pt_regs *regs,
  97. struct task_struct *tsk)
  98. {
  99. struct _fpreg __user *to;
  100. struct _fpxreg *from;
  101. int i;
  102. u16 cs,ds;
  103. int err = 0;
  104. if (tsk == current) {
  105. /* should be actually ds/cs at fpu exception time,
  106. but that information is not available in 64bit mode. */
  107. asm("movw %%ds,%0 " : "=r" (ds));
  108. asm("movw %%cs,%0 " : "=r" (cs));
  109. } else { /* ptrace. task has stopped. */
  110. ds = tsk->thread.ds;
  111. cs = regs->cs;
  112. }
  113. #define P(num,val) err |= __put_user(val, num + (u32 __user *)buf)
  114. P(0, (u32)fxsave->cwd | 0xffff0000);
  115. P(1, (u32)fxsave->swd | 0xffff0000);
  116. P(2, twd_fxsr_to_i387(fxsave));
  117. P(3, (u32)fxsave->rip);
  118. P(4, cs | ((u32)fxsave->fop) << 16);
  119. P(5, fxsave->rdp);
  120. P(6, 0xffff0000 | ds);
  121. #undef P
  122. if (err)
  123. return -1;
  124. to = &buf->_st[0];
  125. from = (struct _fpxreg *) &fxsave->st_space[0];
  126. for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
  127. if (__copy_to_user(to, from, sizeof(*to)))
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. int restore_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf, int fsave)
  133. {
  134. clear_fpu(tsk);
  135. if (!fsave) {
  136. if (__copy_from_user(&tsk->thread.i387.fxsave,
  137. &buf->_fxsr_env[0],
  138. sizeof(struct i387_fxsave_struct)))
  139. return -1;
  140. tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
  141. set_stopped_child_used_math(tsk);
  142. }
  143. return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
  144. }
  145. int save_i387_ia32(struct task_struct *tsk,
  146. struct _fpstate_ia32 __user *buf,
  147. struct pt_regs *regs,
  148. int fsave)
  149. {
  150. int err = 0;
  151. init_fpu(tsk);
  152. if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
  153. return -1;
  154. if (fsave)
  155. return 0;
  156. err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
  157. if (fsave)
  158. return err ? -1 : 1;
  159. err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
  160. err |= __copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
  161. sizeof(struct i387_fxsave_struct));
  162. return err ? -1 : 1;
  163. }