Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*---------------------------------------------------------------------------+ |
| 2 | | fpu_aux.c | |
| 3 | | | |
| 4 | | Code to implement some of the FPU auxiliary instructions. | |
| 5 | | | |
| 6 | | Copyright (C) 1992,1993,1994,1997 | |
| 7 | | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia | |
| 8 | | E-mail billm@suburbia.net | |
| 9 | | | |
| 10 | | | |
| 11 | +---------------------------------------------------------------------------*/ |
| 12 | |
| 13 | #include "fpu_system.h" |
| 14 | #include "exception.h" |
| 15 | #include "fpu_emu.h" |
| 16 | #include "status_w.h" |
| 17 | #include "control_w.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | static void fnop(void) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | static void fclex(void) |
| 24 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 25 | partial_status &= |
| 26 | ~(SW_Backward | SW_Summary | SW_Stack_Fault | SW_Precision | |
| 27 | SW_Underflow | SW_Overflow | SW_Zero_Div | SW_Denorm_Op | |
| 28 | SW_Invalid); |
| 29 | no_ip_update = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /* Needs to be externally visible */ |
| 33 | void finit(void) |
| 34 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 35 | control_word = 0x037f; |
| 36 | partial_status = 0; |
| 37 | top = 0; /* We don't keep top in the status word internally. */ |
| 38 | fpu_tag_word = 0xffff; |
| 39 | /* The behaviour is different from that detailed in |
| 40 | Section 15.1.6 of the Intel manual */ |
| 41 | operand_address.offset = 0; |
| 42 | operand_address.selector = 0; |
| 43 | instruction_address.offset = 0; |
| 44 | instruction_address.selector = 0; |
| 45 | instruction_address.opcode = 0; |
| 46 | no_ip_update = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* |
| 50 | * These are nops on the i387.. |
| 51 | */ |
| 52 | #define feni fnop |
| 53 | #define fdisi fnop |
| 54 | #define fsetpm fnop |
| 55 | |
| 56 | static FUNC const finit_table[] = { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 57 | feni, fdisi, fclex, finit, |
| 58 | fsetpm, FPU_illegal, FPU_illegal, FPU_illegal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | void finit_(void) |
| 62 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 63 | (finit_table[FPU_rm]) (); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static void fstsw_ax(void) |
| 67 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 68 | *(short *)&FPU_EAX = status_word(); |
| 69 | no_ip_update = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static FUNC const fstsw_table[] = { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 73 | fstsw_ax, FPU_illegal, FPU_illegal, FPU_illegal, |
| 74 | FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | void fstsw_(void) |
| 78 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 79 | (fstsw_table[FPU_rm]) (); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static FUNC const fp_nop_table[] = { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 83 | fnop, FPU_illegal, FPU_illegal, FPU_illegal, |
| 84 | FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | void fp_nop(void) |
| 88 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 89 | (fp_nop_table[FPU_rm]) (); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | void fld_i_(void) |
| 93 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 94 | FPU_REG *st_new_ptr; |
| 95 | int i; |
| 96 | u_char tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 98 | if (STACK_OVERFLOW) { |
| 99 | FPU_stack_overflow(); |
| 100 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 102 | |
| 103 | /* fld st(i) */ |
| 104 | i = FPU_rm; |
| 105 | if (NOT_EMPTY(i)) { |
| 106 | reg_copy(&st(i), st_new_ptr); |
| 107 | tag = FPU_gettagi(i); |
| 108 | push(); |
| 109 | FPU_settag0(tag); |
| 110 | } else { |
| 111 | if (control_word & CW_Invalid) { |
| 112 | /* The masked response */ |
| 113 | FPU_stack_underflow(); |
| 114 | } else |
| 115 | EXCEPTION(EX_StackUnder); |
| 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | void fxch_i(void) |
| 121 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 122 | /* fxch st(i) */ |
| 123 | FPU_REG t; |
| 124 | int i = FPU_rm; |
| 125 | FPU_REG *st0_ptr = &st(0), *sti_ptr = &st(i); |
| 126 | long tag_word = fpu_tag_word; |
| 127 | int regnr = top & 7, regnri = ((regnr + i) & 7); |
| 128 | u_char st0_tag = (tag_word >> (regnr * 2)) & 3; |
| 129 | u_char sti_tag = (tag_word >> (regnri * 2)) & 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 131 | if (st0_tag == TAG_Empty) { |
| 132 | if (sti_tag == TAG_Empty) { |
| 133 | FPU_stack_underflow(); |
| 134 | FPU_stack_underflow_i(i); |
| 135 | return; |
| 136 | } |
| 137 | if (control_word & CW_Invalid) { |
| 138 | /* Masked response */ |
| 139 | FPU_copy_to_reg0(sti_ptr, sti_tag); |
| 140 | } |
| 141 | FPU_stack_underflow_i(i); |
| 142 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 144 | if (sti_tag == TAG_Empty) { |
| 145 | if (control_word & CW_Invalid) { |
| 146 | /* Masked response */ |
| 147 | FPU_copy_to_regi(st0_ptr, st0_tag, i); |
| 148 | } |
| 149 | FPU_stack_underflow(); |
| 150 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 152 | clear_C1(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 154 | reg_copy(st0_ptr, &t); |
| 155 | reg_copy(sti_ptr, st0_ptr); |
| 156 | reg_copy(&t, sti_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 158 | tag_word &= ~(3 << (regnr * 2)) & ~(3 << (regnri * 2)); |
| 159 | tag_word |= (sti_tag << (regnr * 2)) | (st0_tag << (regnri * 2)); |
| 160 | fpu_tag_word = tag_word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | void ffree_(void) |
| 164 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 165 | /* ffree st(i) */ |
| 166 | FPU_settagi(FPU_rm, TAG_Empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | void ffreep(void) |
| 170 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 171 | /* ffree st(i) + pop - unofficial code */ |
| 172 | FPU_settagi(FPU_rm, TAG_Empty); |
| 173 | FPU_pop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | void fst_i_(void) |
| 177 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 178 | /* fst st(i) */ |
| 179 | FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | void fstp_i(void) |
| 183 | { |
Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 184 | /* fstp st(i) */ |
| 185 | FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm); |
| 186 | FPU_pop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |