i387.c 13 KB

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