i387.c 13 KB

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