i387_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * General FPU state handling cleanups
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/module.h>
  10. #include <asm/processor.h>
  11. #include <asm/i387.h>
  12. #include <asm/math_emu.h>
  13. #include <asm/sigcontext.h>
  14. #include <asm/user.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/uaccess.h>
  17. #ifdef CONFIG_MATH_EMULATION
  18. #define HAVE_HWFP (boot_cpu_data.hard_math)
  19. #else
  20. #define HAVE_HWFP 1
  21. #endif
  22. static unsigned long mxcsr_feature_mask __read_mostly = 0xffffffff;
  23. void mxcsr_feature_mask_init(void)
  24. {
  25. unsigned long mask = 0;
  26. clts();
  27. if (cpu_has_fxsr) {
  28. memset(&current->thread.i387.fxsave, 0,
  29. sizeof(struct i387_fxsave_struct));
  30. asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
  31. mask = current->thread.i387.fxsave.mxcsr_mask;
  32. if (mask == 0)
  33. mask = 0x0000ffbf;
  34. }
  35. mxcsr_feature_mask &= mask;
  36. stts();
  37. }
  38. /*
  39. * The _current_ task is using the FPU for the first time
  40. * so initialize it and set the mxcsr to its default
  41. * value at reset if we support XMM instructions and then
  42. * remeber the current task has used the FPU.
  43. */
  44. void init_fpu(struct task_struct *tsk)
  45. {
  46. if (cpu_has_fxsr) {
  47. memset(&tsk->thread.i387.fxsave, 0,
  48. sizeof(struct i387_fxsave_struct));
  49. tsk->thread.i387.fxsave.cwd = 0x37f;
  50. if (cpu_has_xmm)
  51. tsk->thread.i387.fxsave.mxcsr = 0x1f80;
  52. } else {
  53. memset(&tsk->thread.i387.fsave, 0,
  54. sizeof(struct i387_fsave_struct));
  55. tsk->thread.i387.fsave.cwd = 0xffff037fu;
  56. tsk->thread.i387.fsave.swd = 0xffff0000u;
  57. tsk->thread.i387.fsave.twd = 0xffffffffu;
  58. tsk->thread.i387.fsave.fos = 0xffff0000u;
  59. }
  60. /* only the device not available exception
  61. * or ptrace can call init_fpu */
  62. set_stopped_child_used_math(tsk);
  63. }
  64. /*
  65. * FPU lazy state save handling.
  66. */
  67. void kernel_fpu_begin(void)
  68. {
  69. struct thread_info *thread = current_thread_info();
  70. preempt_disable();
  71. if (thread->status & TS_USEDFPU) {
  72. __save_init_fpu(thread->task);
  73. return;
  74. }
  75. clts();
  76. }
  77. EXPORT_SYMBOL_GPL(kernel_fpu_begin);
  78. /*
  79. * FPU tag word conversions.
  80. */
  81. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  82. {
  83. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  84. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  85. tmp = ~twd;
  86. tmp = (tmp | (tmp >> 1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  87. /* and move the valid bits to the lower byte. */
  88. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  89. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  90. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  91. return tmp;
  92. }
  93. static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  94. {
  95. struct _fpxreg *st = NULL;
  96. unsigned long tos = (fxsave->swd >> 11) & 7;
  97. unsigned long twd = (unsigned long) fxsave->twd;
  98. unsigned long tag;
  99. unsigned long ret = 0xffff0000u;
  100. int i;
  101. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
  102. for (i = 0; i < 8; i++) {
  103. if (twd & 0x1) {
  104. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  105. switch (st->exponent & 0x7fff) {
  106. case 0x7fff:
  107. tag = 2; /* Special */
  108. break;
  109. case 0x0000:
  110. if (!st->significand[0] &&
  111. !st->significand[1] &&
  112. !st->significand[2] &&
  113. !st->significand[3]) {
  114. tag = 1; /* Zero */
  115. } else {
  116. tag = 2; /* Special */
  117. }
  118. break;
  119. default:
  120. if (st->significand[3] & 0x8000) {
  121. tag = 0; /* Valid */
  122. } else {
  123. tag = 2; /* Special */
  124. }
  125. break;
  126. }
  127. } else {
  128. tag = 3; /* Empty */
  129. }
  130. ret |= (tag << (2 * i));
  131. twd = twd >> 1;
  132. }
  133. return ret;
  134. }
  135. /*
  136. * FPU state interaction.
  137. */
  138. unsigned short get_fpu_cwd(struct task_struct *tsk)
  139. {
  140. if (cpu_has_fxsr) {
  141. return tsk->thread.i387.fxsave.cwd;
  142. } else {
  143. return (unsigned short)tsk->thread.i387.fsave.cwd;
  144. }
  145. }
  146. unsigned short get_fpu_swd(struct task_struct *tsk)
  147. {
  148. if (cpu_has_fxsr) {
  149. return tsk->thread.i387.fxsave.swd;
  150. } else {
  151. return (unsigned short)tsk->thread.i387.fsave.swd;
  152. }
  153. }
  154. #if 0
  155. unsigned short get_fpu_twd(struct task_struct *tsk)
  156. {
  157. if (cpu_has_fxsr) {
  158. return tsk->thread.i387.fxsave.twd;
  159. } else {
  160. return (unsigned short)tsk->thread.i387.fsave.twd;
  161. }
  162. }
  163. #endif /* 0 */
  164. unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  165. {
  166. if (cpu_has_xmm) {
  167. return tsk->thread.i387.fxsave.mxcsr;
  168. } else {
  169. return 0x1f80;
  170. }
  171. }
  172. #if 0
  173. void set_fpu_cwd(struct task_struct *tsk, unsigned short cwd)
  174. {
  175. if (cpu_has_fxsr) {
  176. tsk->thread.i387.fxsave.cwd = cwd;
  177. } else {
  178. tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
  179. }
  180. }
  181. void set_fpu_swd(struct task_struct *tsk, unsigned short swd)
  182. {
  183. if (cpu_has_fxsr) {
  184. tsk->thread.i387.fxsave.swd = swd;
  185. } else {
  186. tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
  187. }
  188. }
  189. void set_fpu_twd(struct task_struct *tsk, unsigned short twd)
  190. {
  191. if (cpu_has_fxsr) {
  192. tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
  193. } else {
  194. tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
  195. }
  196. }
  197. #endif /* 0 */
  198. /*
  199. * FXSR floating point environment conversions.
  200. */
  201. static int convert_fxsr_to_user(struct _fpstate __user *buf,
  202. struct i387_fxsave_struct *fxsave)
  203. {
  204. unsigned long env[7];
  205. struct _fpreg __user *to;
  206. struct _fpxreg *from;
  207. int i;
  208. env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
  209. env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
  210. env[2] = twd_fxsr_to_i387(fxsave);
  211. env[3] = fxsave->fip;
  212. env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
  213. env[5] = fxsave->foo;
  214. env[6] = fxsave->fos;
  215. if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
  216. return 1;
  217. to = &buf->_st[0];
  218. from = (struct _fpxreg *) &fxsave->st_space[0];
  219. for (i = 0; i < 8; i++, to++, from++) {
  220. unsigned long __user *t = (unsigned long __user *)to;
  221. unsigned long *f = (unsigned long *)from;
  222. if (__put_user(*f, t) ||
  223. __put_user(*(f + 1), t + 1) ||
  224. __put_user(from->exponent, &to->exponent))
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
  230. struct _fpstate __user *buf)
  231. {
  232. unsigned long env[7];
  233. struct _fpxreg *to;
  234. struct _fpreg __user *from;
  235. int i;
  236. if (__copy_from_user(env, buf, 7 * sizeof(long)))
  237. return 1;
  238. fxsave->cwd = (unsigned short)(env[0] & 0xffff);
  239. fxsave->swd = (unsigned short)(env[1] & 0xffff);
  240. fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
  241. fxsave->fip = env[3];
  242. fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
  243. fxsave->fcs = (env[4] & 0xffff);
  244. fxsave->foo = env[5];
  245. fxsave->fos = env[6];
  246. to = (struct _fpxreg *) &fxsave->st_space[0];
  247. from = &buf->_st[0];
  248. for (i = 0; i < 8; i++, to++, from++) {
  249. unsigned long *t = (unsigned long *)to;
  250. unsigned long __user *f = (unsigned long __user *)from;
  251. if (__get_user(*t, f) ||
  252. __get_user(*(t + 1), f + 1) ||
  253. __get_user(to->exponent, &from->exponent))
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Signal frame handlers.
  260. */
  261. static inline int save_i387_fsave(struct _fpstate __user *buf)
  262. {
  263. struct task_struct *tsk = current;
  264. unlazy_fpu(tsk);
  265. tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
  266. if (__copy_to_user(buf, &tsk->thread.i387.fsave,
  267. sizeof(struct i387_fsave_struct)))
  268. return -1;
  269. return 1;
  270. }
  271. static int save_i387_fxsave(struct _fpstate __user *buf)
  272. {
  273. struct task_struct *tsk = current;
  274. int err = 0;
  275. unlazy_fpu(tsk);
  276. if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave))
  277. return -1;
  278. err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
  279. err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
  280. if (err)
  281. return -1;
  282. if (__copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
  283. sizeof(struct i387_fxsave_struct)))
  284. return -1;
  285. return 1;
  286. }
  287. int save_i387(struct _fpstate __user *buf)
  288. {
  289. if (!used_math())
  290. return 0;
  291. /* This will cause a "finit" to be triggered by the next
  292. * attempted FPU operation by the 'current' process.
  293. */
  294. clear_used_math();
  295. if (HAVE_HWFP) {
  296. if (cpu_has_fxsr) {
  297. return save_i387_fxsave(buf);
  298. } else {
  299. return save_i387_fsave(buf);
  300. }
  301. } else {
  302. return save_i387_soft(&current->thread.i387.soft, buf);
  303. }
  304. }
  305. static inline int restore_i387_fsave(struct _fpstate __user *buf)
  306. {
  307. struct task_struct *tsk = current;
  308. clear_fpu(tsk);
  309. return __copy_from_user(&tsk->thread.i387.fsave, buf,
  310. sizeof(struct i387_fsave_struct));
  311. }
  312. static int restore_i387_fxsave(struct _fpstate __user *buf)
  313. {
  314. int err;
  315. struct task_struct *tsk = current;
  316. clear_fpu(tsk);
  317. err = __copy_from_user(&tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
  318. sizeof(struct i387_fxsave_struct));
  319. /* mxcsr reserved bits must be masked to zero for security reasons */
  320. tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
  321. return err ? 1 : convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
  322. }
  323. int restore_i387(struct _fpstate __user *buf)
  324. {
  325. int err;
  326. if (HAVE_HWFP) {
  327. if (cpu_has_fxsr) {
  328. err = restore_i387_fxsave(buf);
  329. } else {
  330. err = restore_i387_fsave(buf);
  331. }
  332. } else {
  333. err = restore_i387_soft(&current->thread.i387.soft, buf);
  334. }
  335. set_used_math();
  336. return err;
  337. }
  338. /*
  339. * ptrace request handlers.
  340. */
  341. static inline int get_fpregs_fsave(struct user_i387_struct __user *buf,
  342. struct task_struct *tsk)
  343. {
  344. return __copy_to_user(buf, &tsk->thread.i387.fsave,
  345. sizeof(struct user_i387_struct));
  346. }
  347. static inline int get_fpregs_fxsave(struct user_i387_struct __user *buf,
  348. struct task_struct *tsk)
  349. {
  350. return convert_fxsr_to_user((struct _fpstate __user *)buf,
  351. &tsk->thread.i387.fxsave);
  352. }
  353. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
  354. {
  355. if (HAVE_HWFP) {
  356. if (cpu_has_fxsr) {
  357. return get_fpregs_fxsave(buf, tsk);
  358. } else {
  359. return get_fpregs_fsave(buf, tsk);
  360. }
  361. } else {
  362. return save_i387_soft(&tsk->thread.i387.soft,
  363. (struct _fpstate __user *)buf);
  364. }
  365. }
  366. static inline int set_fpregs_fsave(struct task_struct *tsk,
  367. struct user_i387_struct __user *buf)
  368. {
  369. return __copy_from_user(&tsk->thread.i387.fsave, buf,
  370. sizeof(struct user_i387_struct));
  371. }
  372. static inline int set_fpregs_fxsave(struct task_struct *tsk,
  373. struct user_i387_struct __user *buf)
  374. {
  375. return convert_fxsr_from_user(&tsk->thread.i387.fxsave,
  376. (struct _fpstate __user *)buf);
  377. }
  378. int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
  379. {
  380. if (HAVE_HWFP) {
  381. if (cpu_has_fxsr) {
  382. return set_fpregs_fxsave(tsk, buf);
  383. } else {
  384. return set_fpregs_fsave(tsk, buf);
  385. }
  386. } else {
  387. return restore_i387_soft(&tsk->thread.i387.soft,
  388. (struct _fpstate __user *)buf);
  389. }
  390. }
  391. int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *tsk)
  392. {
  393. if (cpu_has_fxsr) {
  394. if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
  395. sizeof(struct user_fxsr_struct)))
  396. return -EFAULT;
  397. return 0;
  398. } else {
  399. return -EIO;
  400. }
  401. }
  402. int set_fpxregs(struct task_struct *tsk, struct user_fxsr_struct __user *buf)
  403. {
  404. int ret = 0;
  405. if (cpu_has_fxsr) {
  406. if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
  407. sizeof(struct user_fxsr_struct)))
  408. ret = -EFAULT;
  409. /* mxcsr reserved bits must be masked to zero
  410. * for security reasons */
  411. tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
  412. } else {
  413. ret = -EIO;
  414. }
  415. return ret;
  416. }
  417. /*
  418. * FPU state for core dumps.
  419. */
  420. static inline void copy_fpu_fsave(struct task_struct *tsk,
  421. struct user_i387_struct *fpu)
  422. {
  423. memcpy(fpu, &tsk->thread.i387.fsave,
  424. sizeof(struct user_i387_struct));
  425. }
  426. static inline void copy_fpu_fxsave(struct task_struct *tsk,
  427. struct user_i387_struct *fpu)
  428. {
  429. unsigned short *to;
  430. unsigned short *from;
  431. int i;
  432. memcpy(fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long));
  433. to = (unsigned short *)&fpu->st_space[0];
  434. from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
  435. for (i = 0; i < 8; i++, to += 5, from += 8)
  436. memcpy(to, from, 5 * sizeof(unsigned short));
  437. }
  438. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
  439. {
  440. int fpvalid;
  441. struct task_struct *tsk = current;
  442. fpvalid = !!used_math();
  443. if (fpvalid) {
  444. unlazy_fpu(tsk);
  445. if (cpu_has_fxsr) {
  446. copy_fpu_fxsave(tsk, fpu);
  447. } else {
  448. copy_fpu_fsave(tsk, fpu);
  449. }
  450. }
  451. return fpvalid;
  452. }
  453. EXPORT_SYMBOL(dump_fpu);
  454. int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
  455. {
  456. int fpvalid = !!tsk_used_math(tsk);
  457. if (fpvalid) {
  458. if (tsk == current)
  459. unlazy_fpu(tsk);
  460. if (cpu_has_fxsr)
  461. copy_fpu_fxsave(tsk, fpu);
  462. else
  463. copy_fpu_fsave(tsk, fpu);
  464. }
  465. return fpvalid;
  466. }
  467. int dump_task_extended_fpu(struct task_struct *tsk,
  468. struct user_fxsr_struct *fpu)
  469. {
  470. int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
  471. if (fpvalid) {
  472. if (tsk == current)
  473. unlazy_fpu(tsk);
  474. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
  475. }
  476. return fpvalid;
  477. }