regset.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * FPU register's regset abstraction, for ptrace, core dumps, etc.
  3. */
  4. #include <asm/fpu/internal.h>
  5. #include <asm/fpu/signal.h>
  6. #include <asm/fpu/regset.h>
  7. /*
  8. * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
  9. * as the "regset->n" for the xstate regset will be updated based on the feature
  10. * capabilites supported by the xsave.
  11. */
  12. int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
  13. {
  14. struct fpu *target_fpu = &target->thread.fpu;
  15. return target_fpu->fpstate_active ? regset->n : 0;
  16. }
  17. int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
  18. {
  19. struct fpu *target_fpu = &target->thread.fpu;
  20. return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
  21. }
  22. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  23. unsigned int pos, unsigned int count,
  24. void *kbuf, void __user *ubuf)
  25. {
  26. struct fpu *fpu = &target->thread.fpu;
  27. if (!cpu_has_fxsr)
  28. return -ENODEV;
  29. fpu__activate_fpstate_read(fpu);
  30. fpstate_sanitize_xstate(fpu);
  31. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  32. &fpu->state.fxsave, 0, -1);
  33. }
  34. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  35. unsigned int pos, unsigned int count,
  36. const void *kbuf, const void __user *ubuf)
  37. {
  38. struct fpu *fpu = &target->thread.fpu;
  39. int ret;
  40. if (!cpu_has_fxsr)
  41. return -ENODEV;
  42. fpu__activate_fpstate_write(fpu);
  43. fpstate_sanitize_xstate(fpu);
  44. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  45. &fpu->state.fxsave, 0, -1);
  46. /*
  47. * mxcsr reserved bits must be masked to zero for security reasons.
  48. */
  49. fpu->state.fxsave.mxcsr &= mxcsr_feature_mask;
  50. /*
  51. * update the header bits in the xsave header, indicating the
  52. * presence of FP and SSE state.
  53. */
  54. if (cpu_has_xsave)
  55. fpu->state.xsave.header.xfeatures |= XSTATE_FPSSE;
  56. return ret;
  57. }
  58. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  59. unsigned int pos, unsigned int count,
  60. void *kbuf, void __user *ubuf)
  61. {
  62. struct fpu *fpu = &target->thread.fpu;
  63. struct xregs_state *xsave;
  64. int ret;
  65. if (!cpu_has_xsave)
  66. return -ENODEV;
  67. fpu__activate_fpstate_read(fpu);
  68. xsave = &fpu->state.xsave;
  69. /*
  70. * Copy the 48bytes defined by the software first into the xstate
  71. * memory layout in the thread struct, so that we can copy the entire
  72. * xstateregs to the user using one user_regset_copyout().
  73. */
  74. memcpy(&xsave->i387.sw_reserved,
  75. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  76. /*
  77. * Copy the xstate memory layout.
  78. */
  79. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  80. return ret;
  81. }
  82. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  83. unsigned int pos, unsigned int count,
  84. const void *kbuf, const void __user *ubuf)
  85. {
  86. struct fpu *fpu = &target->thread.fpu;
  87. struct xregs_state *xsave;
  88. int ret;
  89. if (!cpu_has_xsave)
  90. return -ENODEV;
  91. fpu__activate_fpstate_write(fpu);
  92. xsave = &fpu->state.xsave;
  93. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  94. /*
  95. * mxcsr reserved bits must be masked to zero for security reasons.
  96. */
  97. xsave->i387.mxcsr &= mxcsr_feature_mask;
  98. xsave->header.xfeatures &= xfeatures_mask;
  99. /*
  100. * These bits must be zero.
  101. */
  102. memset(&xsave->header.reserved, 0, 48);
  103. return ret;
  104. }
  105. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  106. /*
  107. * FPU tag word conversions.
  108. */
  109. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  110. {
  111. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  112. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  113. tmp = ~twd;
  114. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  115. /* and move the valid bits to the lower byte. */
  116. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  117. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  118. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  119. return tmp;
  120. }
  121. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  122. #define FP_EXP_TAG_VALID 0
  123. #define FP_EXP_TAG_ZERO 1
  124. #define FP_EXP_TAG_SPECIAL 2
  125. #define FP_EXP_TAG_EMPTY 3
  126. static inline u32 twd_fxsr_to_i387(struct fxregs_state *fxsave)
  127. {
  128. struct _fpxreg *st;
  129. u32 tos = (fxsave->swd >> 11) & 7;
  130. u32 twd = (unsigned long) fxsave->twd;
  131. u32 tag;
  132. u32 ret = 0xffff0000u;
  133. int i;
  134. for (i = 0; i < 8; i++, twd >>= 1) {
  135. if (twd & 0x1) {
  136. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  137. switch (st->exponent & 0x7fff) {
  138. case 0x7fff:
  139. tag = FP_EXP_TAG_SPECIAL;
  140. break;
  141. case 0x0000:
  142. if (!st->significand[0] &&
  143. !st->significand[1] &&
  144. !st->significand[2] &&
  145. !st->significand[3])
  146. tag = FP_EXP_TAG_ZERO;
  147. else
  148. tag = FP_EXP_TAG_SPECIAL;
  149. break;
  150. default:
  151. if (st->significand[3] & 0x8000)
  152. tag = FP_EXP_TAG_VALID;
  153. else
  154. tag = FP_EXP_TAG_SPECIAL;
  155. break;
  156. }
  157. } else {
  158. tag = FP_EXP_TAG_EMPTY;
  159. }
  160. ret |= tag << (2 * i);
  161. }
  162. return ret;
  163. }
  164. /*
  165. * FXSR floating point environment conversions.
  166. */
  167. void
  168. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  169. {
  170. struct fxregs_state *fxsave = &tsk->thread.fpu.state.fxsave;
  171. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  172. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  173. int i;
  174. env->cwd = fxsave->cwd | 0xffff0000u;
  175. env->swd = fxsave->swd | 0xffff0000u;
  176. env->twd = twd_fxsr_to_i387(fxsave);
  177. #ifdef CONFIG_X86_64
  178. env->fip = fxsave->rip;
  179. env->foo = fxsave->rdp;
  180. /*
  181. * should be actually ds/cs at fpu exception time, but
  182. * that information is not available in 64bit mode.
  183. */
  184. env->fcs = task_pt_regs(tsk)->cs;
  185. if (tsk == current) {
  186. savesegment(ds, env->fos);
  187. } else {
  188. env->fos = tsk->thread.ds;
  189. }
  190. env->fos |= 0xffff0000;
  191. #else
  192. env->fip = fxsave->fip;
  193. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  194. env->foo = fxsave->foo;
  195. env->fos = fxsave->fos;
  196. #endif
  197. for (i = 0; i < 8; ++i)
  198. memcpy(&to[i], &from[i], sizeof(to[0]));
  199. }
  200. void convert_to_fxsr(struct task_struct *tsk,
  201. const struct user_i387_ia32_struct *env)
  202. {
  203. struct fxregs_state *fxsave = &tsk->thread.fpu.state.fxsave;
  204. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  205. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  206. int i;
  207. fxsave->cwd = env->cwd;
  208. fxsave->swd = env->swd;
  209. fxsave->twd = twd_i387_to_fxsr(env->twd);
  210. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  211. #ifdef CONFIG_X86_64
  212. fxsave->rip = env->fip;
  213. fxsave->rdp = env->foo;
  214. /* cs and ds ignored */
  215. #else
  216. fxsave->fip = env->fip;
  217. fxsave->fcs = (env->fcs & 0xffff);
  218. fxsave->foo = env->foo;
  219. fxsave->fos = env->fos;
  220. #endif
  221. for (i = 0; i < 8; ++i)
  222. memcpy(&to[i], &from[i], sizeof(from[0]));
  223. }
  224. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  225. unsigned int pos, unsigned int count,
  226. void *kbuf, void __user *ubuf)
  227. {
  228. struct fpu *fpu = &target->thread.fpu;
  229. struct user_i387_ia32_struct env;
  230. fpu__activate_fpstate_read(fpu);
  231. if (!static_cpu_has(X86_FEATURE_FPU))
  232. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  233. if (!cpu_has_fxsr)
  234. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  235. &fpu->state.fsave, 0,
  236. -1);
  237. fpstate_sanitize_xstate(fpu);
  238. if (kbuf && pos == 0 && count == sizeof(env)) {
  239. convert_from_fxsr(kbuf, target);
  240. return 0;
  241. }
  242. convert_from_fxsr(&env, target);
  243. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  244. }
  245. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  246. unsigned int pos, unsigned int count,
  247. const void *kbuf, const void __user *ubuf)
  248. {
  249. struct fpu *fpu = &target->thread.fpu;
  250. struct user_i387_ia32_struct env;
  251. int ret;
  252. fpu__activate_fpstate_write(fpu);
  253. fpstate_sanitize_xstate(fpu);
  254. if (!static_cpu_has(X86_FEATURE_FPU))
  255. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  256. if (!cpu_has_fxsr)
  257. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  258. &fpu->state.fsave, 0,
  259. -1);
  260. if (pos > 0 || count < sizeof(env))
  261. convert_from_fxsr(&env, target);
  262. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  263. if (!ret)
  264. convert_to_fxsr(target, &env);
  265. /*
  266. * update the header bit in the xsave header, indicating the
  267. * presence of FP.
  268. */
  269. if (cpu_has_xsave)
  270. fpu->state.xsave.header.xfeatures |= XSTATE_FP;
  271. return ret;
  272. }
  273. /*
  274. * FPU state for core dumps.
  275. * This is only used for a.out dumps now.
  276. * It is declared generically using elf_fpregset_t (which is
  277. * struct user_i387_struct) but is in fact only used for 32-bit
  278. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  279. */
  280. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
  281. {
  282. struct task_struct *tsk = current;
  283. struct fpu *fpu = &tsk->thread.fpu;
  284. int fpvalid;
  285. fpvalid = fpu->fpstate_active;
  286. if (fpvalid)
  287. fpvalid = !fpregs_get(tsk, NULL,
  288. 0, sizeof(struct user_i387_ia32_struct),
  289. ufpu, NULL);
  290. return fpvalid;
  291. }
  292. EXPORT_SYMBOL(dump_fpu);
  293. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */