fpu_entry.c 20 KB

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