i387.c 13 KB

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