regset.c 9.5 KB

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