fpu_entry.c 21 KB

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