i387_32.c 13 KB

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