fpu32.c 4.6 KB

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