regset.c 9.5 KB

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