regset.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FPU register's regset abstraction, for ptrace, core dumps, etc.
  4. */
  5. #include <linux/sched/task_stack.h>
  6. #include <linux/vmalloc.h>
  7. #include <asm/fpu/api.h>
  8. #include <asm/fpu/signal.h>
  9. #include <asm/fpu/regset.h>
  10. #include "context.h"
  11. #include "internal.h"
  12. #include "legacy.h"
  13. #include "xstate.h"
  14. /*
  15. * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
  16. * as the "regset->n" for the xstate regset will be updated based on the feature
  17. * capabilities supported by the xsave.
  18. */
  19. int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
  20. {
  21. return regset->n;
  22. }
  23. int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
  24. {
  25. if (boot_cpu_has(X86_FEATURE_FXSR))
  26. return regset->n;
  27. else
  28. return 0;
  29. }
  30. /*
  31. * The regset get() functions are invoked from:
  32. *
  33. * - coredump to dump the current task's fpstate. If the current task
  34. * owns the FPU then the memory state has to be synchronized and the
  35. * FPU register state preserved. Otherwise fpstate is already in sync.
  36. *
  37. * - ptrace to dump fpstate of a stopped task, in which case the registers
  38. * have already been saved to fpstate on context switch.
  39. */
  40. static void sync_fpstate(struct fpu *fpu)
  41. {
  42. if (fpu == &current->thread.fpu)
  43. fpu_sync_fpstate(fpu);
  44. }
  45. /*
  46. * Invalidate cached FPU registers before modifying the stopped target
  47. * task's fpstate.
  48. *
  49. * This forces the target task on resume to restore the FPU registers from
  50. * modified fpstate. Otherwise the task might skip the restore and operate
  51. * with the cached FPU registers which discards the modifications.
  52. */
  53. static void fpu_force_restore(struct fpu *fpu)
  54. {
  55. /*
  56. * Only stopped child tasks can be used to modify the FPU
  57. * state in the fpstate buffer:
  58. */
  59. WARN_ON_FPU(fpu == &current->thread.fpu);
  60. __fpu_invalidate_fpregs_state(fpu);
  61. }
  62. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  63. struct membuf to)
  64. {
  65. struct fpu *fpu = &target->thread.fpu;
  66. if (!cpu_feature_enabled(X86_FEATURE_FXSR))
  67. return -ENODEV;
  68. sync_fpstate(fpu);
  69. if (!use_xsave()) {
  70. return membuf_write(&to, &fpu->fpstate->regs.fxsave,
  71. sizeof(fpu->fpstate->regs.fxsave));
  72. }
  73. copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_FX);
  74. return 0;
  75. }
  76. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  77. unsigned int pos, unsigned int count,
  78. const void *kbuf, const void __user *ubuf)
  79. {
  80. struct fpu *fpu = &target->thread.fpu;
  81. struct fxregs_state newstate;
  82. int ret;
  83. if (!cpu_feature_enabled(X86_FEATURE_FXSR))
  84. return -ENODEV;
  85. /* No funny business with partial or oversized writes is permitted. */
  86. if (pos != 0 || count != sizeof(newstate))
  87. return -EINVAL;
  88. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
  89. if (ret)
  90. return ret;
  91. /* Do not allow an invalid MXCSR value. */
  92. if (newstate.mxcsr & ~mxcsr_feature_mask)
  93. return -EINVAL;
  94. fpu_force_restore(fpu);
  95. /* Copy the state */
  96. memcpy(&fpu->fpstate->regs.fxsave, &newstate, sizeof(newstate));
  97. /* Clear xmm8..15 for 32-bit callers */
  98. BUILD_BUG_ON(sizeof(fpu->__fpstate.regs.fxsave.xmm_space) != 16 * 16);
  99. if (in_ia32_syscall())
  100. memset(&fpu->fpstate->regs.fxsave.xmm_space[8*4], 0, 8 * 16);
  101. /* Mark FP and SSE as in use when XSAVE is enabled */
  102. if (use_xsave())
  103. fpu->fpstate->regs.xsave.header.xfeatures |= XFEATURE_MASK_FPSSE;
  104. return 0;
  105. }
  106. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  107. struct membuf to)
  108. {
  109. if (!cpu_feature_enabled(X86_FEATURE_XSAVE))
  110. return -ENODEV;
  111. sync_fpstate(&target->thread.fpu);
  112. copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_XSAVE);
  113. return 0;
  114. }
  115. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  116. unsigned int pos, unsigned int count,
  117. const void *kbuf, const void __user *ubuf)
  118. {
  119. struct fpu *fpu = &target->thread.fpu;
  120. struct xregs_state *tmpbuf = NULL;
  121. int ret;
  122. if (!cpu_feature_enabled(X86_FEATURE_XSAVE))
  123. return -ENODEV;
  124. /*
  125. * A whole standard-format XSAVE buffer is needed:
  126. */
  127. if (pos != 0 || count != fpu_user_cfg.max_size)
  128. return -EFAULT;
  129. if (!kbuf) {
  130. tmpbuf = vmalloc(count);
  131. if (!tmpbuf)
  132. return -ENOMEM;
  133. if (copy_from_user(tmpbuf, ubuf, count)) {
  134. ret = -EFAULT;
  135. goto out;
  136. }
  137. }
  138. fpu_force_restore(fpu);
  139. ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru);
  140. out:
  141. vfree(tmpbuf);
  142. return ret;
  143. }
  144. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  145. /*
  146. * FPU tag word conversions.
  147. */
  148. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  149. {
  150. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  151. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  152. tmp = ~twd;
  153. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  154. /* and move the valid bits to the lower byte. */
  155. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  156. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  157. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  158. return tmp;
  159. }
  160. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  161. #define FP_EXP_TAG_VALID 0
  162. #define FP_EXP_TAG_ZERO 1
  163. #define FP_EXP_TAG_SPECIAL 2
  164. #define FP_EXP_TAG_EMPTY 3
  165. static inline u32 twd_fxsr_to_i387(struct fxregs_state *fxsave)
  166. {
  167. struct _fpxreg *st;
  168. u32 tos = (fxsave->swd >> 11) & 7;
  169. u32 twd = (unsigned long) fxsave->twd;
  170. u32 tag;
  171. u32 ret = 0xffff0000u;
  172. int i;
  173. for (i = 0; i < 8; i++, twd >>= 1) {
  174. if (twd & 0x1) {
  175. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  176. switch (st->exponent & 0x7fff) {
  177. case 0x7fff:
  178. tag = FP_EXP_TAG_SPECIAL;
  179. break;
  180. case 0x0000:
  181. if (!st->significand[0] &&
  182. !st->significand[1] &&
  183. !st->significand[2] &&
  184. !st->significand[3])
  185. tag = FP_EXP_TAG_ZERO;
  186. else
  187. tag = FP_EXP_TAG_SPECIAL;
  188. break;
  189. default:
  190. if (st->significand[3] & 0x8000)
  191. tag = FP_EXP_TAG_VALID;
  192. else
  193. tag = FP_EXP_TAG_SPECIAL;
  194. break;
  195. }
  196. } else {
  197. tag = FP_EXP_TAG_EMPTY;
  198. }
  199. ret |= tag << (2 * i);
  200. }
  201. return ret;
  202. }
  203. /*
  204. * FXSR floating point environment conversions.
  205. */
  206. static void __convert_from_fxsr(struct user_i387_ia32_struct *env,
  207. struct task_struct *tsk,
  208. struct fxregs_state *fxsave)
  209. {
  210. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  211. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  212. int i;
  213. env->cwd = fxsave->cwd | 0xffff0000u;
  214. env->swd = fxsave->swd | 0xffff0000u;
  215. env->twd = twd_fxsr_to_i387(fxsave);
  216. #ifdef CONFIG_X86_64
  217. env->fip = fxsave->rip;
  218. env->foo = fxsave->rdp;
  219. /*
  220. * should be actually ds/cs at fpu exception time, but
  221. * that information is not available in 64bit mode.
  222. */
  223. env->fcs = task_pt_regs(tsk)->cs;
  224. if (tsk == current) {
  225. savesegment(ds, env->fos);
  226. } else {
  227. env->fos = tsk->thread.ds;
  228. }
  229. env->fos |= 0xffff0000;
  230. #else
  231. env->fip = fxsave->fip;
  232. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  233. env->foo = fxsave->foo;
  234. env->fos = fxsave->fos;
  235. #endif
  236. for (i = 0; i < 8; ++i)
  237. memcpy(&to[i], &from[i], sizeof(to[0]));
  238. }
  239. void
  240. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  241. {
  242. __convert_from_fxsr(env, tsk, &tsk->thread.fpu.fpstate->regs.fxsave);
  243. }
  244. void convert_to_fxsr(struct fxregs_state *fxsave,
  245. const struct user_i387_ia32_struct *env)
  246. {
  247. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  248. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  249. int i;
  250. fxsave->cwd = env->cwd;
  251. fxsave->swd = env->swd;
  252. fxsave->twd = twd_i387_to_fxsr(env->twd);
  253. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  254. #ifdef CONFIG_X86_64
  255. fxsave->rip = env->fip;
  256. fxsave->rdp = env->foo;
  257. /* cs and ds ignored */
  258. #else
  259. fxsave->fip = env->fip;
  260. fxsave->fcs = (env->fcs & 0xffff);
  261. fxsave->foo = env->foo;
  262. fxsave->fos = env->fos;
  263. #endif
  264. for (i = 0; i < 8; ++i)
  265. memcpy(&to[i], &from[i], sizeof(from[0]));
  266. }
  267. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  268. struct membuf to)
  269. {
  270. struct fpu *fpu = &target->thread.fpu;
  271. struct user_i387_ia32_struct env;
  272. struct fxregs_state fxsave, *fx;
  273. sync_fpstate(fpu);
  274. if (!cpu_feature_enabled(X86_FEATURE_FPU))
  275. return fpregs_soft_get(target, regset, to);
  276. if (!cpu_feature_enabled(X86_FEATURE_FXSR)) {
  277. return membuf_write(&to, &fpu->fpstate->regs.fsave,
  278. sizeof(struct fregs_state));
  279. }
  280. if (use_xsave()) {
  281. struct membuf mb = { .p = &fxsave, .left = sizeof(fxsave) };
  282. /* Handle init state optimized xstate correctly */
  283. copy_xstate_to_uabi_buf(mb, target, XSTATE_COPY_FP);
  284. fx = &fxsave;
  285. } else {
  286. fx = &fpu->fpstate->regs.fxsave;
  287. }
  288. __convert_from_fxsr(&env, target, fx);
  289. return membuf_write(&to, &env, sizeof(env));
  290. }
  291. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  292. unsigned int pos, unsigned int count,
  293. const void *kbuf, const void __user *ubuf)
  294. {
  295. struct fpu *fpu = &target->thread.fpu;
  296. struct user_i387_ia32_struct env;
  297. int ret;
  298. /* No funny business with partial or oversized writes is permitted. */
  299. if (pos != 0 || count != sizeof(struct user_i387_ia32_struct))
  300. return -EINVAL;
  301. if (!cpu_feature_enabled(X86_FEATURE_FPU))
  302. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  303. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  304. if (ret)
  305. return ret;
  306. fpu_force_restore(fpu);
  307. if (cpu_feature_enabled(X86_FEATURE_FXSR))
  308. convert_to_fxsr(&fpu->fpstate->regs.fxsave, &env);
  309. else
  310. memcpy(&fpu->fpstate->regs.fsave, &env, sizeof(env));
  311. /*
  312. * Update the header bit in the xsave header, indicating the
  313. * presence of FP.
  314. */
  315. if (cpu_feature_enabled(X86_FEATURE_XSAVE))
  316. fpu->fpstate->regs.xsave.header.xfeatures |= XFEATURE_MASK_FP;
  317. return 0;
  318. }
  319. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */