regset.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. /* xcomp_bv must be 0 when using uncompacted format */
  111. if (!ret && xsave->header.xcomp_bv)
  112. ret = -EINVAL;
  113. }
  114. /*
  115. * In case of failure, mark all states as init:
  116. */
  117. if (ret)
  118. fpstate_init(&fpu->state);
  119. /*
  120. * mxcsr reserved bits must be masked to zero for security reasons.
  121. */
  122. xsave->i387.mxcsr &= mxcsr_feature_mask;
  123. xsave->header.xfeatures &= xfeatures_mask;
  124. /*
  125. * These bits must be zero.
  126. */
  127. memset(&xsave->header.reserved, 0, 48);
  128. return ret;
  129. }
  130. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  131. /*
  132. * FPU tag word conversions.
  133. */
  134. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  135. {
  136. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  137. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  138. tmp = ~twd;
  139. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  140. /* and move the valid bits to the lower byte. */
  141. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  142. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  143. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  144. return tmp;
  145. }
  146. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  147. #define FP_EXP_TAG_VALID 0
  148. #define FP_EXP_TAG_ZERO 1
  149. #define FP_EXP_TAG_SPECIAL 2
  150. #define FP_EXP_TAG_EMPTY 3
  151. static inline u32 twd_fxsr_to_i387(struct fxregs_state *fxsave)
  152. {
  153. struct _fpxreg *st;
  154. u32 tos = (fxsave->swd >> 11) & 7;
  155. u32 twd = (unsigned long) fxsave->twd;
  156. u32 tag;
  157. u32 ret = 0xffff0000u;
  158. int i;
  159. for (i = 0; i < 8; i++, twd >>= 1) {
  160. if (twd & 0x1) {
  161. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  162. switch (st->exponent & 0x7fff) {
  163. case 0x7fff:
  164. tag = FP_EXP_TAG_SPECIAL;
  165. break;
  166. case 0x0000:
  167. if (!st->significand[0] &&
  168. !st->significand[1] &&
  169. !st->significand[2] &&
  170. !st->significand[3])
  171. tag = FP_EXP_TAG_ZERO;
  172. else
  173. tag = FP_EXP_TAG_SPECIAL;
  174. break;
  175. default:
  176. if (st->significand[3] & 0x8000)
  177. tag = FP_EXP_TAG_VALID;
  178. else
  179. tag = FP_EXP_TAG_SPECIAL;
  180. break;
  181. }
  182. } else {
  183. tag = FP_EXP_TAG_EMPTY;
  184. }
  185. ret |= tag << (2 * i);
  186. }
  187. return ret;
  188. }
  189. /*
  190. * FXSR floating point environment conversions.
  191. */
  192. void
  193. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  194. {
  195. struct fxregs_state *fxsave = &tsk->thread.fpu.state.fxsave;
  196. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  197. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  198. int i;
  199. env->cwd = fxsave->cwd | 0xffff0000u;
  200. env->swd = fxsave->swd | 0xffff0000u;
  201. env->twd = twd_fxsr_to_i387(fxsave);
  202. #ifdef CONFIG_X86_64
  203. env->fip = fxsave->rip;
  204. env->foo = fxsave->rdp;
  205. /*
  206. * should be actually ds/cs at fpu exception time, but
  207. * that information is not available in 64bit mode.
  208. */
  209. env->fcs = task_pt_regs(tsk)->cs;
  210. if (tsk == current) {
  211. savesegment(ds, env->fos);
  212. } else {
  213. env->fos = tsk->thread.ds;
  214. }
  215. env->fos |= 0xffff0000;
  216. #else
  217. env->fip = fxsave->fip;
  218. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  219. env->foo = fxsave->foo;
  220. env->fos = fxsave->fos;
  221. #endif
  222. for (i = 0; i < 8; ++i)
  223. memcpy(&to[i], &from[i], sizeof(to[0]));
  224. }
  225. void convert_to_fxsr(struct task_struct *tsk,
  226. const struct user_i387_ia32_struct *env)
  227. {
  228. struct fxregs_state *fxsave = &tsk->thread.fpu.state.fxsave;
  229. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  230. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  231. int i;
  232. fxsave->cwd = env->cwd;
  233. fxsave->swd = env->swd;
  234. fxsave->twd = twd_i387_to_fxsr(env->twd);
  235. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  236. #ifdef CONFIG_X86_64
  237. fxsave->rip = env->fip;
  238. fxsave->rdp = env->foo;
  239. /* cs and ds ignored */
  240. #else
  241. fxsave->fip = env->fip;
  242. fxsave->fcs = (env->fcs & 0xffff);
  243. fxsave->foo = env->foo;
  244. fxsave->fos = env->fos;
  245. #endif
  246. for (i = 0; i < 8; ++i)
  247. memcpy(&to[i], &from[i], sizeof(from[0]));
  248. }
  249. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  250. unsigned int pos, unsigned int count,
  251. void *kbuf, void __user *ubuf)
  252. {
  253. struct fpu *fpu = &target->thread.fpu;
  254. struct user_i387_ia32_struct env;
  255. fpu__activate_fpstate_read(fpu);
  256. if (!boot_cpu_has(X86_FEATURE_FPU))
  257. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  258. if (!boot_cpu_has(X86_FEATURE_FXSR))
  259. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  260. &fpu->state.fsave, 0,
  261. -1);
  262. fpstate_sanitize_xstate(fpu);
  263. if (kbuf && pos == 0 && count == sizeof(env)) {
  264. convert_from_fxsr(kbuf, target);
  265. return 0;
  266. }
  267. convert_from_fxsr(&env, target);
  268. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  269. }
  270. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  271. unsigned int pos, unsigned int count,
  272. const void *kbuf, const void __user *ubuf)
  273. {
  274. struct fpu *fpu = &target->thread.fpu;
  275. struct user_i387_ia32_struct env;
  276. int ret;
  277. fpu__activate_fpstate_write(fpu);
  278. fpstate_sanitize_xstate(fpu);
  279. if (!boot_cpu_has(X86_FEATURE_FPU))
  280. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  281. if (!boot_cpu_has(X86_FEATURE_FXSR))
  282. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  283. &fpu->state.fsave, 0,
  284. -1);
  285. if (pos > 0 || count < sizeof(env))
  286. convert_from_fxsr(&env, target);
  287. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  288. if (!ret)
  289. convert_to_fxsr(target, &env);
  290. /*
  291. * update the header bit in the xsave header, indicating the
  292. * presence of FP.
  293. */
  294. if (boot_cpu_has(X86_FEATURE_XSAVE))
  295. fpu->state.xsave.header.xfeatures |= XFEATURE_MASK_FP;
  296. return ret;
  297. }
  298. /*
  299. * FPU state for core dumps.
  300. * This is only used for a.out dumps now.
  301. * It is declared generically using elf_fpregset_t (which is
  302. * struct user_i387_struct) but is in fact only used for 32-bit
  303. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  304. */
  305. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
  306. {
  307. struct task_struct *tsk = current;
  308. struct fpu *fpu = &tsk->thread.fpu;
  309. int fpvalid;
  310. fpvalid = fpu->fpstate_active;
  311. if (fpvalid)
  312. fpvalid = !fpregs_get(tsk, NULL,
  313. 0, sizeof(struct user_i387_ia32_struct),
  314. ufpu, NULL);
  315. return fpvalid;
  316. }
  317. EXPORT_SYMBOL(dump_fpu);
  318. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */