regset.c 9.6 KB

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