i387.c 13 KB

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