PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/x86/math-emu/fpu_entry.c

http://github.com/CyanogenMod/cm-kernel
C | 767 lines | 588 code | 92 blank | 87 comment | 106 complexity | c2c4e3997d9220dd504817f42921b639 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*---------------------------------------------------------------------------+
  2. | fpu_entry.c |
  3. | |
  4. | The entry functions for wm-FPU-emu |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1996,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | See the files "README" and "COPYING" for further copyright and warranty |
  11. | information. |
  12. | |
  13. +---------------------------------------------------------------------------*/
  14. /*---------------------------------------------------------------------------+
  15. | Note: |
  16. | The file contains code which accesses user memory. |
  17. | Emulator static data may change when user memory is accessed, due to |
  18. | other processes using the emulator while swapping is in progress. |
  19. +---------------------------------------------------------------------------*/
  20. /*---------------------------------------------------------------------------+
  21. | math_emulate(), restore_i387_soft() and save_i387_soft() are the only |
  22. | entry points for wm-FPU-emu. |
  23. +---------------------------------------------------------------------------*/
  24. #include <linux/signal.h>
  25. #include <linux/regset.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/desc.h>
  28. #include <asm/user.h>
  29. #include <asm/i387.h>
  30. #include "fpu_system.h"
  31. #include "fpu_emu.h"
  32. #include "exception.h"
  33. #include "control_w.h"
  34. #include "status_w.h"
  35. #define __BAD__ FPU_illegal /* Illegal on an 80486, causes SIGILL */
  36. #ifndef NO_UNDOC_CODE /* Un-documented FPU op-codes supported by default. */
  37. /* WARNING: These codes are not documented by Intel in their 80486 manual
  38. and may not work on FPU clones or later Intel FPUs. */
  39. /* Changes to support the un-doc codes provided by Linus Torvalds. */
  40. #define _d9_d8_ fstp_i /* unofficial code (19) */
  41. #define _dc_d0_ fcom_st /* unofficial code (14) */
  42. #define _dc_d8_ fcompst /* unofficial code (1c) */
  43. #define _dd_c8_ fxch_i /* unofficial code (0d) */
  44. #define _de_d0_ fcompst /* unofficial code (16) */
  45. #define _df_c0_ ffreep /* unofficial code (07) ffree + pop */
  46. #define _df_c8_ fxch_i /* unofficial code (0f) */
  47. #define _df_d0_ fstp_i /* unofficial code (17) */
  48. #define _df_d8_ fstp_i /* unofficial code (1f) */
  49. static FUNC const st_instr_table[64] = {
  50. fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, _df_c0_,
  51. fmul__, fxch_i, __BAD__, __BAD__, fmul_i, _dd_c8_, fmulp_, _df_c8_,
  52. fcom_st, fp_nop, __BAD__, __BAD__, _dc_d0_, fst_i_, _de_d0_, _df_d0_,
  53. fcompst, _d9_d8_, __BAD__, __BAD__, _dc_d8_, fstp_i, fcompp, _df_d8_,
  54. fsub__, FPU_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
  55. fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
  56. fdiv__, FPU_triga, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
  57. fdivr_, FPU_trigb, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
  58. };
  59. #else /* Support only documented FPU op-codes */
  60. static FUNC const st_instr_table[64] = {
  61. fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, __BAD__,
  62. fmul__, fxch_i, __BAD__, __BAD__, fmul_i, __BAD__, fmulp_, __BAD__,
  63. fcom_st, fp_nop, __BAD__, __BAD__, __BAD__, fst_i_, __BAD__, __BAD__,
  64. fcompst, __BAD__, __BAD__, __BAD__, __BAD__, fstp_i, fcompp, __BAD__,
  65. fsub__, FPU_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
  66. fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
  67. fdiv__, FPU_triga, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
  68. fdivr_, FPU_trigb, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
  69. };
  70. #endif /* NO_UNDOC_CODE */
  71. #define _NONE_ 0 /* Take no special action */
  72. #define _REG0_ 1 /* Need to check for not empty st(0) */
  73. #define _REGI_ 2 /* Need to check for not empty st(0) and st(rm) */
  74. #define _REGi_ 0 /* Uses st(rm) */
  75. #define _PUSH_ 3 /* Need to check for space to push onto stack */
  76. #define _null_ 4 /* Function illegal or not implemented */
  77. #define _REGIi 5 /* Uses st(0) and st(rm), result to st(rm) */
  78. #define _REGIp 6 /* Uses st(0) and st(rm), result to st(rm) then pop */
  79. #define _REGIc 0 /* Compare st(0) and st(rm) */
  80. #define _REGIn 0 /* Uses st(0) and st(rm), but handle checks later */
  81. #ifndef NO_UNDOC_CODE
  82. /* Un-documented FPU op-codes supported by default. (see above) */
  83. static u_char const type_table[64] = {
  84. _REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _REGi_,
  85. _REGI_, _REGIn, _null_, _null_, _REGIi, _REGI_, _REGIp, _REGI_,
  86. _REGIc, _NONE_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
  87. _REGIc, _REG0_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
  88. _REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
  89. _REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
  90. _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  91. _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
  92. };
  93. #else /* Support only documented FPU op-codes */
  94. static u_char const type_table[64] = {
  95. _REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _null_,
  96. _REGI_, _REGIn, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  97. _REGIc, _NONE_, _null_, _null_, _null_, _REG0_, _null_, _null_,
  98. _REGIc, _null_, _null_, _null_, _null_, _REG0_, _REGIc, _null_,
  99. _REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
  100. _REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
  101. _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  102. _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
  103. };
  104. #endif /* NO_UNDOC_CODE */
  105. #ifdef RE_ENTRANT_CHECKING
  106. u_char emulating = 0;
  107. #endif /* RE_ENTRANT_CHECKING */
  108. static int valid_prefix(u_char *Byte, u_char __user ** fpu_eip,
  109. overrides * override);
  110. void math_emulate(struct math_emu_info *info)
  111. {
  112. u_char FPU_modrm, byte1;
  113. unsigned short code;
  114. fpu_addr_modes addr_modes;
  115. int unmasked;
  116. FPU_REG loaded_data;
  117. FPU_REG *st0_ptr;
  118. u_char loaded_tag, st0_tag;
  119. void __user *data_address;
  120. struct address data_sel_off;
  121. struct address entry_sel_off;
  122. unsigned long code_base = 0;
  123. unsigned long code_limit = 0; /* Initialized to stop compiler warnings */
  124. struct desc_struct code_descriptor;
  125. if (!used_math()) {
  126. if (init_fpu(current)) {
  127. do_group_exit(SIGKILL);
  128. return;
  129. }
  130. }
  131. #ifdef RE_ENTRANT_CHECKING
  132. if (emulating) {
  133. printk("ERROR: wm-FPU-emu is not RE-ENTRANT!\n");
  134. }
  135. RE_ENTRANT_CHECK_ON;
  136. #endif /* RE_ENTRANT_CHECKING */
  137. FPU_info = info;
  138. FPU_ORIG_EIP = FPU_EIP;
  139. if ((FPU_EFLAGS & 0x00020000) != 0) {
  140. /* Virtual 8086 mode */
  141. addr_modes.default_mode = VM86;
  142. FPU_EIP += code_base = FPU_CS << 4;
  143. code_limit = code_base + 0xffff; /* Assumes code_base <= 0xffff0000 */
  144. } else if (FPU_CS == __USER_CS && FPU_DS == __USER_DS) {
  145. addr_modes.default_mode = 0;
  146. } else if (FPU_CS == __KERNEL_CS) {
  147. printk("math_emulate: %04x:%08lx\n", FPU_CS, FPU_EIP);
  148. panic("Math emulation needed in kernel");
  149. } else {
  150. if ((FPU_CS & 4) != 4) { /* Must be in the LDT */
  151. /* Can only handle segmented addressing via the LDT
  152. for now, and it must be 16 bit */
  153. printk("FPU emulator: Unsupported addressing mode\n");
  154. math_abort(FPU_info, SIGILL);
  155. }
  156. code_descriptor = LDT_DESCRIPTOR(FPU_CS);
  157. if (SEG_D_SIZE(code_descriptor)) {
  158. /* The above test may be wrong, the book is not clear */
  159. /* Segmented 32 bit protected mode */
  160. addr_modes.default_mode = SEG32;
  161. } else {
  162. /* 16 bit protected mode */
  163. addr_modes.default_mode = PM16;
  164. }
  165. FPU_EIP += code_base = SEG_BASE_ADDR(code_descriptor);
  166. code_limit = code_base
  167. + (SEG_LIMIT(code_descriptor) +
  168. 1) * SEG_GRANULARITY(code_descriptor)
  169. - 1;
  170. if (code_limit < code_base)
  171. code_limit = 0xffffffff;
  172. }
  173. FPU_lookahead = !(FPU_EFLAGS & X86_EFLAGS_TF);
  174. if (!valid_prefix(&byte1, (u_char __user **) & FPU_EIP,
  175. &addr_modes.override)) {
  176. RE_ENTRANT_CHECK_OFF;
  177. printk
  178. ("FPU emulator: Unknown prefix byte 0x%02x, probably due to\n"
  179. "FPU emulator: self-modifying code! (emulation impossible)\n",
  180. byte1);
  181. RE_ENTRANT_CHECK_ON;
  182. EXCEPTION(EX_INTERNAL | 0x126);
  183. math_abort(FPU_info, SIGILL);
  184. }
  185. do_another_FPU_instruction:
  186. no_ip_update = 0;
  187. FPU_EIP++; /* We have fetched the prefix and first code bytes. */
  188. if (addr_modes.default_mode) {
  189. /* This checks for the minimum instruction bytes.
  190. We also need to check any extra (address mode) code access. */
  191. if (FPU_EIP > code_limit)
  192. math_abort(FPU_info, SIGSEGV);
  193. }
  194. if ((byte1 & 0xf8) != 0xd8) {
  195. if (byte1 == FWAIT_OPCODE) {
  196. if (partial_status & SW_Summary)
  197. goto do_the_FPU_interrupt;
  198. else
  199. goto FPU_fwait_done;
  200. }
  201. #ifdef PARANOID
  202. EXCEPTION(EX_INTERNAL | 0x128);
  203. math_abort(FPU_info, SIGILL);
  204. #endif /* PARANOID */
  205. }
  206. RE_ENTRANT_CHECK_OFF;
  207. FPU_code_access_ok(1);
  208. FPU_get_user(FPU_modrm, (u_char __user *) FPU_EIP);
  209. RE_ENTRANT_CHECK_ON;
  210. FPU_EIP++;
  211. if (partial_status & SW_Summary) {
  212. /* Ignore the error for now if the current instruction is a no-wait
  213. control instruction */
  214. /* The 80486 manual contradicts itself on this topic,
  215. but a real 80486 uses the following instructions:
  216. fninit, fnstenv, fnsave, fnstsw, fnstenv, fnclex.
  217. */
  218. code = (FPU_modrm << 8) | byte1;
  219. if (!((((code & 0xf803) == 0xe003) || /* fnclex, fninit, fnstsw */
  220. (((code & 0x3003) == 0x3001) && /* fnsave, fnstcw, fnstenv,
  221. fnstsw */
  222. ((code & 0xc000) != 0xc000))))) {
  223. /*
  224. * We need to simulate the action of the kernel to FPU
  225. * interrupts here.
  226. */
  227. do_the_FPU_interrupt:
  228. FPU_EIP = FPU_ORIG_EIP; /* Point to current FPU instruction. */
  229. RE_ENTRANT_CHECK_OFF;
  230. current->thread.trap_no = 16;
  231. current->thread.error_code = 0;
  232. send_sig(SIGFPE, current, 1);
  233. return;
  234. }
  235. }
  236. entry_sel_off.offset = FPU_ORIG_EIP;
  237. entry_sel_off.selector = FPU_CS;
  238. entry_sel_off.opcode = (byte1 << 8) | FPU_modrm;
  239. entry_sel_off.empty = 0;
  240. FPU_rm = FPU_modrm & 7;
  241. if (FPU_modrm < 0300) {
  242. /* All of these instructions use the mod/rm byte to get a data address */
  243. if ((addr_modes.default_mode & SIXTEEN)
  244. ^ (addr_modes.override.address_size == ADDR_SIZE_PREFIX))
  245. data_address =
  246. FPU_get_address_16(FPU_modrm, &FPU_EIP,
  247. &data_sel_off, addr_modes);
  248. else
  249. data_address =
  250. FPU_get_address(FPU_modrm, &FPU_EIP, &data_sel_off,
  251. addr_modes);
  252. if (addr_modes.default_mode) {
  253. if (FPU_EIP - 1 > code_limit)
  254. math_abort(FPU_info, SIGSEGV);
  255. }
  256. if (!(byte1 & 1)) {
  257. unsigned short status1 = partial_status;
  258. st0_ptr = &st(0);
  259. st0_tag = FPU_gettag0();
  260. /* Stack underflow has priority */
  261. if (NOT_EMPTY_ST0) {
  262. if (addr_modes.default_mode & PROTECTED) {
  263. /* This table works for 16 and 32 bit protected mode */
  264. if (access_limit <
  265. data_sizes_16[(byte1 >> 1) & 3])
  266. math_abort(FPU_info, SIGSEGV);
  267. }
  268. unmasked = 0; /* Do this here to stop compiler warnings. */
  269. switch ((byte1 >> 1) & 3) {
  270. case 0:
  271. unmasked =
  272. FPU_load_single((float __user *)
  273. data_address,
  274. &loaded_data);
  275. loaded_tag = unmasked & 0xff;
  276. unmasked &= ~0xff;
  277. break;
  278. case 1:
  279. loaded_tag =
  280. FPU_load_int32((long __user *)
  281. data_address,
  282. &loaded_data);
  283. break;
  284. case 2:
  285. unmasked =
  286. FPU_load_double((double __user *)
  287. data_address,
  288. &loaded_data);
  289. loaded_tag = unmasked & 0xff;
  290. unmasked &= ~0xff;
  291. break;
  292. case 3:
  293. default: /* Used here to suppress gcc warnings. */
  294. loaded_tag =
  295. FPU_load_int16((short __user *)
  296. data_address,
  297. &loaded_data);
  298. break;
  299. }
  300. /* No more access to user memory, it is safe
  301. to use static data now */
  302. /* NaN operands have the next priority. */
  303. /* We have to delay looking at st(0) until after
  304. loading the data, because that data might contain an SNaN */
  305. if (((st0_tag == TAG_Special) && isNaN(st0_ptr))
  306. || ((loaded_tag == TAG_Special)
  307. && isNaN(&loaded_data))) {
  308. /* Restore the status word; we might have loaded a
  309. denormal. */
  310. partial_status = status1;
  311. if ((FPU_modrm & 0x30) == 0x10) {
  312. /* fcom or fcomp */
  313. EXCEPTION(EX_Invalid);
  314. setcc(SW_C3 | SW_C2 | SW_C0);
  315. if ((FPU_modrm & 0x08)
  316. && (control_word &
  317. CW_Invalid))
  318. FPU_pop(); /* fcomp, masked, so we pop. */
  319. } else {
  320. if (loaded_tag == TAG_Special)
  321. loaded_tag =
  322. FPU_Special
  323. (&loaded_data);
  324. #ifdef PECULIAR_486
  325. /* This is not really needed, but gives behaviour
  326. identical to an 80486 */
  327. if ((FPU_modrm & 0x28) == 0x20)
  328. /* fdiv or fsub */
  329. real_2op_NaN
  330. (&loaded_data,
  331. loaded_tag, 0,
  332. &loaded_data);
  333. else
  334. #endif /* PECULIAR_486 */
  335. /* fadd, fdivr, fmul, or fsubr */
  336. real_2op_NaN
  337. (&loaded_data,
  338. loaded_tag, 0,
  339. st0_ptr);
  340. }
  341. goto reg_mem_instr_done;
  342. }
  343. if (unmasked && !((FPU_modrm & 0x30) == 0x10)) {
  344. /* Is not a comparison instruction. */
  345. if ((FPU_modrm & 0x38) == 0x38) {
  346. /* fdivr */
  347. if ((st0_tag == TAG_Zero) &&
  348. ((loaded_tag == TAG_Valid)
  349. || (loaded_tag ==
  350. TAG_Special
  351. &&
  352. isdenormal
  353. (&loaded_data)))) {
  354. if (FPU_divide_by_zero
  355. (0,
  356. getsign
  357. (&loaded_data))
  358. < 0) {
  359. /* We use the fact here that the unmasked
  360. exception in the loaded data was for a
  361. denormal operand */
  362. /* Restore the state of the denormal op bit */
  363. partial_status
  364. &=
  365. ~SW_Denorm_Op;
  366. partial_status
  367. |=
  368. status1 &
  369. SW_Denorm_Op;
  370. } else
  371. setsign(st0_ptr,
  372. getsign
  373. (&loaded_data));
  374. }
  375. }
  376. goto reg_mem_instr_done;
  377. }
  378. switch ((FPU_modrm >> 3) & 7) {
  379. case 0: /* fadd */
  380. clear_C1();
  381. FPU_add(&loaded_data, loaded_tag, 0,
  382. control_word);
  383. break;
  384. case 1: /* fmul */
  385. clear_C1();
  386. FPU_mul(&loaded_data, loaded_tag, 0,
  387. control_word);
  388. break;
  389. case 2: /* fcom */
  390. FPU_compare_st_data(&loaded_data,
  391. loaded_tag);
  392. break;
  393. case 3: /* fcomp */
  394. if (!FPU_compare_st_data
  395. (&loaded_data, loaded_tag)
  396. && !unmasked)
  397. FPU_pop();
  398. break;
  399. case 4: /* fsub */
  400. clear_C1();
  401. FPU_sub(LOADED | loaded_tag,
  402. (int)&loaded_data,
  403. control_word);
  404. break;
  405. case 5: /* fsubr */
  406. clear_C1();
  407. FPU_sub(REV | LOADED | loaded_tag,
  408. (int)&loaded_data,
  409. control_word);
  410. break;
  411. case 6: /* fdiv */
  412. clear_C1();
  413. FPU_div(LOADED | loaded_tag,
  414. (int)&loaded_data,
  415. control_word);
  416. break;
  417. case 7: /* fdivr */
  418. clear_C1();
  419. if (st0_tag == TAG_Zero)
  420. partial_status = status1; /* Undo any denorm tag,
  421. zero-divide has priority. */
  422. FPU_div(REV | LOADED | loaded_tag,
  423. (int)&loaded_data,
  424. control_word);
  425. break;
  426. }
  427. } else {
  428. if ((FPU_modrm & 0x30) == 0x10) {
  429. /* The instruction is fcom or fcomp */
  430. EXCEPTION(EX_StackUnder);
  431. setcc(SW_C3 | SW_C2 | SW_C0);
  432. if ((FPU_modrm & 0x08)
  433. && (control_word & CW_Invalid))
  434. FPU_pop(); /* fcomp */
  435. } else
  436. FPU_stack_underflow();
  437. }
  438. reg_mem_instr_done:
  439. operand_address = data_sel_off;
  440. } else {
  441. if (!(no_ip_update =
  442. FPU_load_store(((FPU_modrm & 0x38) | (byte1 & 6))
  443. >> 1, addr_modes, data_address))) {
  444. operand_address = data_sel_off;
  445. }
  446. }
  447. } else {
  448. /* None of these instructions access user memory */
  449. u_char instr_index = (FPU_modrm & 0x38) | (byte1 & 7);
  450. #ifdef PECULIAR_486
  451. /* This is supposed to be undefined, but a real 80486 seems
  452. to do this: */
  453. operand_address.offset = 0;
  454. operand_address.selector = FPU_DS;
  455. #endif /* PECULIAR_486 */
  456. st0_ptr = &st(0);
  457. st0_tag = FPU_gettag0();
  458. switch (type_table[(int)instr_index]) {
  459. case _NONE_: /* also _REGIc: _REGIn */
  460. break;
  461. case _REG0_:
  462. if (!NOT_EMPTY_ST0) {
  463. FPU_stack_underflow();
  464. goto FPU_instruction_done;
  465. }
  466. break;
  467. case _REGIi:
  468. if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  469. FPU_stack_underflow_i(FPU_rm);
  470. goto FPU_instruction_done;
  471. }
  472. break;
  473. case _REGIp:
  474. if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  475. FPU_stack_underflow_pop(FPU_rm);
  476. goto FPU_instruction_done;
  477. }
  478. break;
  479. case _REGI_:
  480. if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  481. FPU_stack_underflow();
  482. goto FPU_instruction_done;
  483. }
  484. break;
  485. case _PUSH_: /* Only used by the fld st(i) instruction */
  486. break;
  487. case _null_:
  488. FPU_illegal();
  489. goto FPU_instruction_done;
  490. default:
  491. EXCEPTION(EX_INTERNAL | 0x111);
  492. goto FPU_instruction_done;
  493. }
  494. (*st_instr_table[(int)instr_index]) ();
  495. FPU_instruction_done:
  496. ;
  497. }
  498. if (!no_ip_update)
  499. instruction_address = entry_sel_off;
  500. FPU_fwait_done:
  501. #ifdef DEBUG
  502. RE_ENTRANT_CHECK_OFF;
  503. FPU_printall();
  504. RE_ENTRANT_CHECK_ON;
  505. #endif /* DEBUG */
  506. if (FPU_lookahead && !need_resched()) {
  507. FPU_ORIG_EIP = FPU_EIP - code_base;
  508. if (valid_prefix(&byte1, (u_char __user **) & FPU_EIP,
  509. &addr_modes.override))
  510. goto do_another_FPU_instruction;
  511. }
  512. if (addr_modes.default_mode)
  513. FPU_EIP -= code_base;
  514. RE_ENTRANT_CHECK_OFF;
  515. }
  516. /* Support for prefix bytes is not yet complete. To properly handle
  517. all prefix bytes, further changes are needed in the emulator code
  518. which accesses user address space. Access to separate segments is
  519. important for msdos emulation. */
  520. static int valid_prefix(u_char *Byte, u_char __user **fpu_eip,
  521. overrides * override)
  522. {
  523. u_char byte;
  524. u_char __user *ip = *fpu_eip;
  525. *override = (overrides) {
  526. 0, 0, PREFIX_DEFAULT}; /* defaults */
  527. RE_ENTRANT_CHECK_OFF;
  528. FPU_code_access_ok(1);
  529. FPU_get_user(byte, ip);
  530. RE_ENTRANT_CHECK_ON;
  531. while (1) {
  532. switch (byte) {
  533. case ADDR_SIZE_PREFIX:
  534. override->address_size = ADDR_SIZE_PREFIX;
  535. goto do_next_byte;
  536. case OP_SIZE_PREFIX:
  537. override->operand_size = OP_SIZE_PREFIX;
  538. goto do_next_byte;
  539. case PREFIX_CS:
  540. override->segment = PREFIX_CS_;
  541. goto do_next_byte;
  542. case PREFIX_ES:
  543. override->segment = PREFIX_ES_;
  544. goto do_next_byte;
  545. case PREFIX_SS:
  546. override->segment = PREFIX_SS_;
  547. goto do_next_byte;
  548. case PREFIX_FS:
  549. override->segment = PREFIX_FS_;
  550. goto do_next_byte;
  551. case PREFIX_GS:
  552. override->segment = PREFIX_GS_;
  553. goto do_next_byte;
  554. case PREFIX_DS:
  555. override->segment = PREFIX_DS_;
  556. goto do_next_byte;
  557. /* lock is not a valid prefix for FPU instructions,
  558. let the cpu handle it to generate a SIGILL. */
  559. /* case PREFIX_LOCK: */
  560. /* rep.. prefixes have no meaning for FPU instructions */
  561. case PREFIX_REPE:
  562. case PREFIX_REPNE:
  563. do_next_byte:
  564. ip++;
  565. RE_ENTRANT_CHECK_OFF;
  566. FPU_code_access_ok(1);
  567. FPU_get_user(byte, ip);
  568. RE_ENTRANT_CHECK_ON;
  569. break;
  570. case FWAIT_OPCODE:
  571. *Byte = byte;
  572. return 1;
  573. default:
  574. if ((byte & 0xf8) == 0xd8) {
  575. *Byte = byte;
  576. *fpu_eip = ip;
  577. return 1;
  578. } else {
  579. /* Not a valid sequence of prefix bytes followed by
  580. an FPU instruction. */
  581. *Byte = byte; /* Needed for error message. */
  582. return 0;
  583. }
  584. }
  585. }
  586. }
  587. void math_abort(struct math_emu_info *info, unsigned int signal)
  588. {
  589. FPU_EIP = FPU_ORIG_EIP;
  590. current->thread.trap_no = 16;
  591. current->thread.error_code = 0;
  592. send_sig(signal, current, 1);
  593. RE_ENTRANT_CHECK_OFF;
  594. __asm__("movl %0,%%esp ; ret": :"g"(((long)info) - 4));
  595. #ifdef PARANOID
  596. printk("ERROR: wm-FPU-emu math_abort failed!\n");
  597. #endif /* PARANOID */
  598. }
  599. #define S387 ((struct i387_soft_struct *)s387)
  600. #define sstatus_word() \
  601. ((S387->swd & ~SW_Top & 0xffff) | ((S387->ftop << SW_Top_Shift) & SW_Top))
  602. int fpregs_soft_set(struct task_struct *target,
  603. const struct user_regset *regset,
  604. unsigned int pos, unsigned int count,
  605. const void *kbuf, const void __user *ubuf)
  606. {
  607. struct i387_soft_struct *s387 = &target->thread.xstate->soft;
  608. void *space = s387->st_space;
  609. int ret;
  610. int offset, other, i, tags, regnr, tag, newtop;
  611. RE_ENTRANT_CHECK_OFF;
  612. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, s387, 0,
  613. offsetof(struct i387_soft_struct, st_space));
  614. RE_ENTRANT_CHECK_ON;
  615. if (ret)
  616. return ret;
  617. S387->ftop = (S387->swd >> SW_Top_Shift) & 7;
  618. offset = (S387->ftop & 7) * 10;
  619. other = 80 - offset;
  620. RE_ENTRANT_CHECK_OFF;
  621. /* Copy all registers in stack order. */
  622. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  623. space + offset, 0, other);
  624. if (!ret && offset)
  625. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  626. space, 0, offset);
  627. RE_ENTRANT_CHECK_ON;
  628. /* The tags may need to be corrected now. */
  629. tags = S387->twd;
  630. newtop = S387->ftop;
  631. for (i = 0; i < 8; i++) {
  632. regnr = (i + newtop) & 7;
  633. if (((tags >> ((regnr & 7) * 2)) & 3) != TAG_Empty) {
  634. /* The loaded data over-rides all other cases. */
  635. tag =
  636. FPU_tagof((FPU_REG *) ((u_char *) S387->st_space +
  637. 10 * regnr));
  638. tags &= ~(3 << (regnr * 2));
  639. tags |= (tag & 3) << (regnr * 2);
  640. }
  641. }
  642. S387->twd = tags;
  643. return ret;
  644. }
  645. int fpregs_soft_get(struct task_struct *target,
  646. const struct user_regset *regset,
  647. unsigned int pos, unsigned int count,
  648. void *kbuf, void __user *ubuf)
  649. {
  650. struct i387_soft_struct *s387 = &target->thread.xstate->soft;
  651. const void *space = s387->st_space;
  652. int ret;
  653. int offset = (S387->ftop & 7) * 10, other = 80 - offset;
  654. RE_ENTRANT_CHECK_OFF;
  655. #ifdef PECULIAR_486
  656. S387->cwd &= ~0xe080;
  657. /* An 80486 sets nearly all of the reserved bits to 1. */
  658. S387->cwd |= 0xffff0040;
  659. S387->swd = sstatus_word() | 0xffff0000;
  660. S387->twd |= 0xffff0000;
  661. S387->fcs &= ~0xf8000000;
  662. S387->fos |= 0xffff0000;
  663. #endif /* PECULIAR_486 */
  664. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, s387, 0,
  665. offsetof(struct i387_soft_struct, st_space));
  666. /* Copy all registers in stack order. */
  667. if (!ret)
  668. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  669. space + offset, 0, other);
  670. if (!ret)
  671. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  672. space, 0, offset);
  673. RE_ENTRANT_CHECK_ON;
  674. return ret;
  675. }