fpu_entry.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:22k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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