Blame view

arch/x86/math-emu/fpu_entry.c 22.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  /*---------------------------------------------------------------------------+
   |  fpu_entry.c                                                              |
   |                                                                           |
   | The entry functions for wm-FPU-emu                                        |
   |                                                                           |
   | Copyright (C) 1992,1993,1994,1996,1997                                    |
   |                  W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
   |                  E-mail   billm@suburbia.net                              |
   |                                                                           |
   | See the files "README" and "COPYING" for further copyright and warranty   |
   | information.                                                              |
   |                                                                           |
   +---------------------------------------------------------------------------*/
  
  /*---------------------------------------------------------------------------+
   | Note:                                                                     |
   |    The file contains code which accesses user memory.                     |
   |    Emulator static data may change when user memory is accessed, due to   |
   |    other processes using the emulator while swapping is in progress.      |
   +---------------------------------------------------------------------------*/
  
  /*---------------------------------------------------------------------------+
   | math_emulate(), restore_i387_soft() and save_i387_soft() are the only     |
   | entry points for wm-FPU-emu.                                              |
   +---------------------------------------------------------------------------*/
  
  #include <linux/signal.h>
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
28
  #include <linux/regset.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
  
  #include <asm/uaccess.h>
  #include <asm/desc.h>
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
32
  #include <asm/user.h>
e8a496ac8   Suresh Siddha   x86: fix broken m...
33
  #include <asm/i387.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
37
38
39
  
  #include "fpu_system.h"
  #include "fpu_emu.h"
  #include "exception.h"
  #include "control_w.h"
  #include "status_w.h"
3d0d14f98   Ingo Molnar   x86: lindent arch...
40
  #define __BAD__ FPU_illegal	/* Illegal on an 80486, causes SIGILL */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41

3d0d14f98   Ingo Molnar   x86: lindent arch...
42
  #ifndef NO_UNDOC_CODE		/* Un-documented FPU op-codes supported by default. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
  
  /* WARNING: These codes are not documented by Intel in their 80486 manual
     and may not work on FPU clones or later Intel FPUs. */
  
  /* Changes to support the un-doc codes provided by Linus Torvalds. */
3d0d14f98   Ingo Molnar   x86: lindent arch...
48
49
50
51
52
53
54
55
56
  #define _d9_d8_ fstp_i		/* unofficial code (19) */
  #define _dc_d0_ fcom_st		/* unofficial code (14) */
  #define _dc_d8_ fcompst		/* unofficial code (1c) */
  #define _dd_c8_ fxch_i		/* unofficial code (0d) */
  #define _de_d0_ fcompst		/* unofficial code (16) */
  #define _df_c0_ ffreep		/* unofficial code (07) ffree + pop */
  #define _df_c8_ fxch_i		/* unofficial code (0f) */
  #define _df_d0_ fstp_i		/* unofficial code (17) */
  #define _df_d8_ fstp_i		/* unofficial code (1f) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
  
  static FUNC const st_instr_table[64] = {
3d0d14f98   Ingo Molnar   x86: lindent arch...
59
60
61
62
63
64
65
66
  	fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, _df_c0_,
  	fmul__, fxch_i, __BAD__, __BAD__, fmul_i, _dd_c8_, fmulp_, _df_c8_,
  	fcom_st, fp_nop, __BAD__, __BAD__, _dc_d0_, fst_i_, _de_d0_, _df_d0_,
  	fcompst, _d9_d8_, __BAD__, __BAD__, _dc_d8_, fstp_i, fcompp, _df_d8_,
  	fsub__, FPU_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
  	fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
  	fdiv__, FPU_triga, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
  	fdivr_, FPU_trigb, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  };
3d0d14f98   Ingo Molnar   x86: lindent arch...
68
  #else /* Support only documented FPU op-codes */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  
  static FUNC const st_instr_table[64] = {
3d0d14f98   Ingo Molnar   x86: lindent arch...
71
72
73
74
75
76
77
78
  	fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, __BAD__,
  	fmul__, fxch_i, __BAD__, __BAD__, fmul_i, __BAD__, fmulp_, __BAD__,
  	fcom_st, fp_nop, __BAD__, __BAD__, __BAD__, fst_i_, __BAD__, __BAD__,
  	fcompst, __BAD__, __BAD__, __BAD__, __BAD__, fstp_i, fcompp, __BAD__,
  	fsub__, FPU_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
  	fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
  	fdiv__, FPU_triga, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
  	fdivr_, FPU_trigb, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
  };
  
  #endif /* NO_UNDOC_CODE */
3d0d14f98   Ingo Molnar   x86: lindent arch...
82
83
84
85
86
87
88
89
90
91
  #define _NONE_ 0		/* Take no special action */
  #define _REG0_ 1		/* Need to check for not empty st(0) */
  #define _REGI_ 2		/* Need to check for not empty st(0) and st(rm) */
  #define _REGi_ 0		/* Uses st(rm) */
  #define _PUSH_ 3		/* Need to check for space to push onto stack */
  #define _null_ 4		/* Function illegal or not implemented */
  #define _REGIi 5		/* Uses st(0) and st(rm), result to st(rm) */
  #define _REGIp 6		/* Uses st(0) and st(rm), result to st(rm) then pop */
  #define _REGIc 0		/* Compare st(0) and st(rm) */
  #define _REGIn 0		/* Uses st(0) and st(rm), but handle checks later */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
  
  #ifndef NO_UNDOC_CODE
  
  /* Un-documented FPU op-codes supported by default. (see above) */
  
  static u_char const type_table[64] = {
3d0d14f98   Ingo Molnar   x86: lindent arch...
98
99
100
101
102
103
104
105
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _REGi_,
  	_REGI_, _REGIn, _null_, _null_, _REGIi, _REGI_, _REGIp, _REGI_,
  	_REGIc, _NONE_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
  	_REGIc, _REG0_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
  	_REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
  	_REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  };
3d0d14f98   Ingo Molnar   x86: lindent arch...
107
  #else /* Support only documented FPU op-codes */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
  
  static u_char const type_table[64] = {
3d0d14f98   Ingo Molnar   x86: lindent arch...
110
111
112
113
114
115
116
117
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _null_,
  	_REGI_, _REGIn, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  	_REGIc, _NONE_, _null_, _null_, _null_, _REG0_, _null_, _null_,
  	_REGIc, _null_, _null_, _null_, _null_, _REG0_, _REGIc, _null_,
  	_REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
  	_REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
  	_REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
  };
  
  #endif /* NO_UNDOC_CODE */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  #ifdef RE_ENTRANT_CHECKING
3d0d14f98   Ingo Molnar   x86: lindent arch...
122
  u_char emulating = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  #endif /* RE_ENTRANT_CHECKING */
e8d591dc7   Ingo Molnar   x86: lindent arch...
124
  static int valid_prefix(u_char *Byte, u_char __user ** fpu_eip,
3d0d14f98   Ingo Molnar   x86: lindent arch...
125
  			overrides * override);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126

d315760ff   Tejun Heo   x86: fix math_emu...
127
  void math_emulate(struct math_emu_info *info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  {
3d0d14f98   Ingo Molnar   x86: lindent arch...
129
130
131
132
133
134
135
136
137
138
139
140
141
  	u_char FPU_modrm, byte1;
  	unsigned short code;
  	fpu_addr_modes addr_modes;
  	int unmasked;
  	FPU_REG loaded_data;
  	FPU_REG *st0_ptr;
  	u_char loaded_tag, st0_tag;
  	void __user *data_address;
  	struct address data_sel_off;
  	struct address entry_sel_off;
  	unsigned long code_base = 0;
  	unsigned long code_limit = 0;	/* Initialized to stop compiler warnings */
  	struct desc_struct code_descriptor;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142

e8a496ac8   Suresh Siddha   x86: fix broken m...
143
144
145
146
147
148
  	if (!used_math()) {
  		if (init_fpu(current)) {
  			do_group_exit(SIGKILL);
  			return;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  #ifdef RE_ENTRANT_CHECKING
3d0d14f98   Ingo Molnar   x86: lindent arch...
150
151
152
153
154
  	if (emulating) {
  		printk("ERROR: wm-FPU-emu is not RE-ENTRANT!
  ");
  	}
  	RE_ENTRANT_CHECK_ON;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  #endif /* RE_ENTRANT_CHECKING */
d315760ff   Tejun Heo   x86: fix math_emu...
156
  	FPU_info = info;
3d0d14f98   Ingo Molnar   x86: lindent arch...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  
  	FPU_ORIG_EIP = FPU_EIP;
  
  	if ((FPU_EFLAGS & 0x00020000) != 0) {
  		/* Virtual 8086 mode */
  		addr_modes.default_mode = VM86;
  		FPU_EIP += code_base = FPU_CS << 4;
  		code_limit = code_base + 0xffff;	/* Assumes code_base <= 0xffff0000 */
  	} else if (FPU_CS == __USER_CS && FPU_DS == __USER_DS) {
  		addr_modes.default_mode = 0;
  	} else if (FPU_CS == __KERNEL_CS) {
  		printk("math_emulate: %04x:%08lx
  ", FPU_CS, FPU_EIP);
  		panic("Math emulation needed in kernel");
  	} else {
  
  		if ((FPU_CS & 4) != 4) {	/* Must be in the LDT */
  			/* Can only handle segmented addressing via the LDT
  			   for now, and it must be 16 bit */
  			printk("FPU emulator: Unsupported addressing mode
  ");
  			math_abort(FPU_info, SIGILL);
  		}
  
  		code_descriptor = LDT_DESCRIPTOR(FPU_CS);
  		if (SEG_D_SIZE(code_descriptor)) {
  			/* The above test may be wrong, the book is not clear */
  			/* Segmented 32 bit protected mode */
  			addr_modes.default_mode = SEG32;
  		} else {
  			/* 16 bit protected mode */
  			addr_modes.default_mode = PM16;
  		}
  		FPU_EIP += code_base = SEG_BASE_ADDR(code_descriptor);
  		code_limit = code_base
  		    + (SEG_LIMIT(code_descriptor) +
  		       1) * SEG_GRANULARITY(code_descriptor)
  		    - 1;
  		if (code_limit < code_base)
  			code_limit = 0xffffffff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
  	}
3d0d14f98   Ingo Molnar   x86: lindent arch...
198

ff0ebb23c   Roland McGrath   x86: x86 user_reg...
199
  	FPU_lookahead = !(FPU_EFLAGS & X86_EFLAGS_TF);
3d0d14f98   Ingo Molnar   x86: lindent arch...
200
201
202
203
204
205
206
207
208
209
210
211
212
  
  	if (!valid_prefix(&byte1, (u_char __user **) & FPU_EIP,
  			  &addr_modes.override)) {
  		RE_ENTRANT_CHECK_OFF;
  		printk
  		    ("FPU emulator: Unknown prefix byte 0x%02x, probably due to
  "
  		     "FPU emulator: self-modifying code! (emulation impossible)
  ",
  		     byte1);
  		RE_ENTRANT_CHECK_ON;
  		EXCEPTION(EX_INTERNAL | 0x126);
  		math_abort(FPU_info, SIGILL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  	}
3d0d14f98   Ingo Molnar   x86: lindent arch...
214
215
216
217
218
219
220
221
222
223
224
225
  
        do_another_FPU_instruction:
  
  	no_ip_update = 0;
  
  	FPU_EIP++;		/* We have fetched the prefix and first code bytes. */
  
  	if (addr_modes.default_mode) {
  		/* This checks for the minimum instruction bytes.
  		   We also need to check any extra (address mode) code access. */
  		if (FPU_EIP > code_limit)
  			math_abort(FPU_info, SIGSEGV);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
  	}
3d0d14f98   Ingo Molnar   x86: lindent arch...
227
228
229
230
231
232
233
234
  
  	if ((byte1 & 0xf8) != 0xd8) {
  		if (byte1 == FWAIT_OPCODE) {
  			if (partial_status & SW_Summary)
  				goto do_the_FPU_interrupt;
  			else
  				goto FPU_fwait_done;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  #ifdef PARANOID
3d0d14f98   Ingo Molnar   x86: lindent arch...
236
237
  		EXCEPTION(EX_INTERNAL | 0x128);
  		math_abort(FPU_info, SIGILL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  #endif /* PARANOID */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
  	}
3d0d14f98   Ingo Molnar   x86: lindent arch...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  	RE_ENTRANT_CHECK_OFF;
  	FPU_code_access_ok(1);
  	FPU_get_user(FPU_modrm, (u_char __user *) FPU_EIP);
  	RE_ENTRANT_CHECK_ON;
  	FPU_EIP++;
  
  	if (partial_status & SW_Summary) {
  		/* Ignore the error for now if the current instruction is a no-wait
  		   control instruction */
  		/* The 80486 manual contradicts itself on this topic,
  		   but a real 80486 uses the following instructions:
  		   fninit, fnstenv, fnsave, fnstsw, fnstenv, fnclex.
  		 */
  		code = (FPU_modrm << 8) | byte1;
  		if (!((((code & 0xf803) == 0xe003) ||	/* fnclex, fninit, fnstsw */
  		       (((code & 0x3003) == 0x3001) &&	/* fnsave, fnstcw, fnstenv,
  							   fnstsw */
  			((code & 0xc000) != 0xc000))))) {
  			/*
  			 *  We need to simulate the action of the kernel to FPU
  			 *  interrupts here.
  			 */
  		      do_the_FPU_interrupt:
  
  			FPU_EIP = FPU_ORIG_EIP;	/* Point to current FPU instruction. */
  
  			RE_ENTRANT_CHECK_OFF;
  			current->thread.trap_no = 16;
  			current->thread.error_code = 0;
  			send_sig(SIGFPE, current, 1);
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
  		}
3d0d14f98   Ingo Molnar   x86: lindent arch...
272
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273

3d0d14f98   Ingo Molnar   x86: lindent arch...
274
275
276
  	entry_sel_off.offset = FPU_ORIG_EIP;
  	entry_sel_off.selector = FPU_CS;
  	entry_sel_off.opcode = (byte1 << 8) | FPU_modrm;
cc7594e42   Andrew Morton   i386: arch/x86/ma...
277
  	entry_sel_off.empty = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278

3d0d14f98   Ingo Molnar   x86: lindent arch...
279
  	FPU_rm = FPU_modrm & 7;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280

3d0d14f98   Ingo Molnar   x86: lindent arch...
281
282
  	if (FPU_modrm < 0300) {
  		/* All of these instructions use the mod/rm byte to get a data address */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283

3d0d14f98   Ingo Molnar   x86: lindent arch...
284
285
286
287
288
289
290
291
292
293
294
295
296
  		if ((addr_modes.default_mode & SIXTEEN)
  		    ^ (addr_modes.override.address_size == ADDR_SIZE_PREFIX))
  			data_address =
  			    FPU_get_address_16(FPU_modrm, &FPU_EIP,
  					       &data_sel_off, addr_modes);
  		else
  			data_address =
  			    FPU_get_address(FPU_modrm, &FPU_EIP, &data_sel_off,
  					    addr_modes);
  
  		if (addr_modes.default_mode) {
  			if (FPU_EIP - 1 > code_limit)
  				math_abort(FPU_info, SIGSEGV);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
  		}
3d0d14f98   Ingo Molnar   x86: lindent arch...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  
  		if (!(byte1 & 1)) {
  			unsigned short status1 = partial_status;
  
  			st0_ptr = &st(0);
  			st0_tag = FPU_gettag0();
  
  			/* Stack underflow has priority */
  			if (NOT_EMPTY_ST0) {
  				if (addr_modes.default_mode & PROTECTED) {
  					/* This table works for 16 and 32 bit protected mode */
  					if (access_limit <
  					    data_sizes_16[(byte1 >> 1) & 3])
  						math_abort(FPU_info, SIGSEGV);
  				}
  
  				unmasked = 0;	/* Do this here to stop compiler warnings. */
  				switch ((byte1 >> 1) & 3) {
  				case 0:
  					unmasked =
  					    FPU_load_single((float __user *)
  							    data_address,
  							    &loaded_data);
  					loaded_tag = unmasked & 0xff;
  					unmasked &= ~0xff;
  					break;
  				case 1:
  					loaded_tag =
  					    FPU_load_int32((long __user *)
  							   data_address,
  							   &loaded_data);
  					break;
  				case 2:
  					unmasked =
  					    FPU_load_double((double __user *)
  							    data_address,
  							    &loaded_data);
  					loaded_tag = unmasked & 0xff;
  					unmasked &= ~0xff;
  					break;
  				case 3:
  				default:	/* Used here to suppress gcc warnings. */
  					loaded_tag =
  					    FPU_load_int16((short __user *)
  							   data_address,
  							   &loaded_data);
  					break;
  				}
  
  				/* No more access to user memory, it is safe
  				   to use static data now */
  
  				/* NaN operands have the next priority. */
  				/* We have to delay looking at st(0) until after
  				   loading the data, because that data might contain an SNaN */
  				if (((st0_tag == TAG_Special) && isNaN(st0_ptr))
  				    || ((loaded_tag == TAG_Special)
  					&& isNaN(&loaded_data))) {
  					/* Restore the status word; we might have loaded a
  					   denormal. */
  					partial_status = status1;
  					if ((FPU_modrm & 0x30) == 0x10) {
  						/* fcom or fcomp */
  						EXCEPTION(EX_Invalid);
  						setcc(SW_C3 | SW_C2 | SW_C0);
  						if ((FPU_modrm & 0x08)
  						    && (control_word &
  							CW_Invalid))
  							FPU_pop();	/* fcomp, masked, so we pop. */
  					} else {
  						if (loaded_tag == TAG_Special)
  							loaded_tag =
  							    FPU_Special
  							    (&loaded_data);
  #ifdef PECULIAR_486
  						/* This is not really needed, but gives behaviour
  						   identical to an 80486 */
  						if ((FPU_modrm & 0x28) == 0x20)
  							/* fdiv or fsub */
  							real_2op_NaN
  							    (&loaded_data,
  							     loaded_tag, 0,
  							     &loaded_data);
  						else
  #endif /* PECULIAR_486 */
  							/* fadd, fdivr, fmul, or fsubr */
  							real_2op_NaN
  							    (&loaded_data,
  							     loaded_tag, 0,
  							     st0_ptr);
  					}
  					goto reg_mem_instr_done;
  				}
  
  				if (unmasked && !((FPU_modrm & 0x30) == 0x10)) {
  					/* Is not a comparison instruction. */
  					if ((FPU_modrm & 0x38) == 0x38) {
  						/* fdivr */
  						if ((st0_tag == TAG_Zero) &&
  						    ((loaded_tag == TAG_Valid)
  						     || (loaded_tag ==
  							 TAG_Special
  							 &&
  							 isdenormal
  							 (&loaded_data)))) {
  							if (FPU_divide_by_zero
  							    (0,
  							     getsign
  							     (&loaded_data))
  							    < 0) {
  								/* We use the fact here that the unmasked
  								   exception in the loaded data was for a
  								   denormal operand */
  								/* Restore the state of the denormal op bit */
  								partial_status
  								    &=
  								    ~SW_Denorm_Op;
  								partial_status
  								    |=
  								    status1 &
  								    SW_Denorm_Op;
  							} else
  								setsign(st0_ptr,
  									getsign
  									(&loaded_data));
  						}
  					}
  					goto reg_mem_instr_done;
  				}
  
  				switch ((FPU_modrm >> 3) & 7) {
  				case 0:	/* fadd */
  					clear_C1();
  					FPU_add(&loaded_data, loaded_tag, 0,
  						control_word);
  					break;
  				case 1:	/* fmul */
  					clear_C1();
  					FPU_mul(&loaded_data, loaded_tag, 0,
  						control_word);
  					break;
  				case 2:	/* fcom */
  					FPU_compare_st_data(&loaded_data,
  							    loaded_tag);
  					break;
  				case 3:	/* fcomp */
  					if (!FPU_compare_st_data
  					    (&loaded_data, loaded_tag)
  					    && !unmasked)
  						FPU_pop();
  					break;
  				case 4:	/* fsub */
  					clear_C1();
  					FPU_sub(LOADED | loaded_tag,
  						(int)&loaded_data,
  						control_word);
  					break;
  				case 5:	/* fsubr */
  					clear_C1();
  					FPU_sub(REV | LOADED | loaded_tag,
  						(int)&loaded_data,
  						control_word);
  					break;
  				case 6:	/* fdiv */
  					clear_C1();
  					FPU_div(LOADED | loaded_tag,
  						(int)&loaded_data,
  						control_word);
  					break;
  				case 7:	/* fdivr */
  					clear_C1();
  					if (st0_tag == TAG_Zero)
  						partial_status = status1;	/* Undo any denorm tag,
  										   zero-divide has priority. */
  					FPU_div(REV | LOADED | loaded_tag,
  						(int)&loaded_data,
  						control_word);
  					break;
  				}
  			} else {
  				if ((FPU_modrm & 0x30) == 0x10) {
  					/* The instruction is fcom or fcomp */
  					EXCEPTION(EX_StackUnder);
  					setcc(SW_C3 | SW_C2 | SW_C0);
  					if ((FPU_modrm & 0x08)
  					    && (control_word & CW_Invalid))
  						FPU_pop();	/* fcomp */
  				} else
  					FPU_stack_underflow();
  			}
  		      reg_mem_instr_done:
  			operand_address = data_sel_off;
  		} else {
  			if (!(no_ip_update =
  			      FPU_load_store(((FPU_modrm & 0x38) | (byte1 & 6))
  					     >> 1, addr_modes, data_address))) {
  				operand_address = data_sel_off;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497

3d0d14f98   Ingo Molnar   x86: lindent arch...
498
499
500
  	} else {
  		/* None of these instructions access user memory */
  		u_char instr_index = (FPU_modrm & 0x38) | (byte1 & 7);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
  
  #ifdef PECULIAR_486
3d0d14f98   Ingo Molnar   x86: lindent arch...
503
504
505
506
  		/* This is supposed to be undefined, but a real 80486 seems
  		   to do this: */
  		operand_address.offset = 0;
  		operand_address.selector = FPU_DS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
  #endif /* PECULIAR_486 */
3d0d14f98   Ingo Molnar   x86: lindent arch...
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  		st0_ptr = &st(0);
  		st0_tag = FPU_gettag0();
  		switch (type_table[(int)instr_index]) {
  		case _NONE_:	/* also _REGIc: _REGIn */
  			break;
  		case _REG0_:
  			if (!NOT_EMPTY_ST0) {
  				FPU_stack_underflow();
  				goto FPU_instruction_done;
  			}
  			break;
  		case _REGIi:
  			if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  				FPU_stack_underflow_i(FPU_rm);
  				goto FPU_instruction_done;
  			}
  			break;
  		case _REGIp:
  			if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  				FPU_stack_underflow_pop(FPU_rm);
  				goto FPU_instruction_done;
  			}
  			break;
  		case _REGI_:
  			if (!NOT_EMPTY_ST0 || !NOT_EMPTY(FPU_rm)) {
  				FPU_stack_underflow();
  				goto FPU_instruction_done;
  			}
  			break;
  		case _PUSH_:	/* Only used by the fld st(i) instruction */
  			break;
  		case _null_:
  			FPU_illegal();
  			goto FPU_instruction_done;
  		default:
  			EXCEPTION(EX_INTERNAL | 0x111);
  			goto FPU_instruction_done;
  		}
  		(*st_instr_table[(int)instr_index]) ();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547

3d0d14f98   Ingo Molnar   x86: lindent arch...
548
549
550
  	      FPU_instruction_done:
  		;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551

3d0d14f98   Ingo Molnar   x86: lindent arch...
552
553
  	if (!no_ip_update)
  		instruction_address = entry_sel_off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554

3d0d14f98   Ingo Molnar   x86: lindent arch...
555
        FPU_fwait_done:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
  
  #ifdef DEBUG
3d0d14f98   Ingo Molnar   x86: lindent arch...
558
559
560
  	RE_ENTRANT_CHECK_OFF;
  	FPU_printall();
  	RE_ENTRANT_CHECK_ON;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
  #endif /* DEBUG */
3d0d14f98   Ingo Molnar   x86: lindent arch...
562
563
564
565
566
567
  	if (FPU_lookahead && !need_resched()) {
  		FPU_ORIG_EIP = FPU_EIP - code_base;
  		if (valid_prefix(&byte1, (u_char __user **) & FPU_EIP,
  				 &addr_modes.override))
  			goto do_another_FPU_instruction;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568

3d0d14f98   Ingo Molnar   x86: lindent arch...
569
570
  	if (addr_modes.default_mode)
  		FPU_EIP -= code_base;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571

3d0d14f98   Ingo Molnar   x86: lindent arch...
572
  	RE_ENTRANT_CHECK_OFF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
576
577
  /* Support for prefix bytes is not yet complete. To properly handle
     all prefix bytes, further changes are needed in the emulator code
     which accesses user address space. Access to separate segments is
     important for msdos emulation. */
e8d591dc7   Ingo Molnar   x86: lindent arch...
578
  static int valid_prefix(u_char *Byte, u_char __user **fpu_eip,
3d0d14f98   Ingo Molnar   x86: lindent arch...
579
  			overrides * override)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
  {
3d0d14f98   Ingo Molnar   x86: lindent arch...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
  	u_char byte;
  	u_char __user *ip = *fpu_eip;
  
  	*override = (overrides) {
  	0, 0, PREFIX_DEFAULT};	/* defaults */
  
  	RE_ENTRANT_CHECK_OFF;
  	FPU_code_access_ok(1);
  	FPU_get_user(byte, ip);
  	RE_ENTRANT_CHECK_ON;
  
  	while (1) {
  		switch (byte) {
  		case ADDR_SIZE_PREFIX:
  			override->address_size = ADDR_SIZE_PREFIX;
  			goto do_next_byte;
  
  		case OP_SIZE_PREFIX:
  			override->operand_size = OP_SIZE_PREFIX;
  			goto do_next_byte;
  
  		case PREFIX_CS:
  			override->segment = PREFIX_CS_;
  			goto do_next_byte;
  		case PREFIX_ES:
  			override->segment = PREFIX_ES_;
  			goto do_next_byte;
  		case PREFIX_SS:
  			override->segment = PREFIX_SS_;
  			goto do_next_byte;
  		case PREFIX_FS:
  			override->segment = PREFIX_FS_;
  			goto do_next_byte;
  		case PREFIX_GS:
  			override->segment = PREFIX_GS_;
  			goto do_next_byte;
  		case PREFIX_DS:
  			override->segment = PREFIX_DS_;
  			goto do_next_byte;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
621
622
623
  
  /* lock is not a valid prefix for FPU instructions,
     let the cpu handle it to generate a SIGILL. */
  /*	case PREFIX_LOCK: */
3d0d14f98   Ingo Molnar   x86: lindent arch...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  			/* rep.. prefixes have no meaning for FPU instructions */
  		case PREFIX_REPE:
  		case PREFIX_REPNE:
  
  		      do_next_byte:
  			ip++;
  			RE_ENTRANT_CHECK_OFF;
  			FPU_code_access_ok(1);
  			FPU_get_user(byte, ip);
  			RE_ENTRANT_CHECK_ON;
  			break;
  		case FWAIT_OPCODE:
  			*Byte = byte;
  			return 1;
  		default:
  			if ((byte & 0xf8) == 0xd8) {
  				*Byte = byte;
  				*fpu_eip = ip;
  				return 1;
  			} else {
  				/* Not a valid sequence of prefix bytes followed by
  				   an FPU instruction. */
  				*Byte = byte;	/* Needed for error message. */
  				return 0;
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
  }
ae6af41f5   Tejun Heo   x86: math_emu inf...
652
  void math_abort(struct math_emu_info *info, unsigned int signal)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
654
655
656
  {
  	FPU_EIP = FPU_ORIG_EIP;
  	current->thread.trap_no = 16;
  	current->thread.error_code = 0;
3d0d14f98   Ingo Molnar   x86: lindent arch...
657
  	send_sig(signal, current, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  	RE_ENTRANT_CHECK_OFF;
3d0d14f98   Ingo Molnar   x86: lindent arch...
659
        __asm__("movl %0,%%esp ; ret": :"g"(((long)info) - 4));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  #ifdef PARANOID
3d0d14f98   Ingo Molnar   x86: lindent arch...
661
662
  	printk("ERROR: wm-FPU-emu math_abort failed!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
  #endif /* PARANOID */
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
  #define S387 ((struct i387_soft_struct *)s387)
  #define sstatus_word() \
    ((S387->swd & ~SW_Top & 0xffff) | ((S387->ftop << SW_Top_Shift) & SW_Top))
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
668
669
670
671
  int fpregs_soft_set(struct task_struct *target,
  		    const struct user_regset *regset,
  		    unsigned int pos, unsigned int count,
  		    const void *kbuf, const void __user *ubuf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  {
c3f8978ea   H. Peter Anvin   x86, fpu: Unbreak...
673
  	struct i387_soft_struct *s387 = &target->thread.fpu.state->soft;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
674
675
  	void *space = s387->st_space;
  	int ret;
3d0d14f98   Ingo Molnar   x86: lindent arch...
676
677
678
  	int offset, other, i, tags, regnr, tag, newtop;
  
  	RE_ENTRANT_CHECK_OFF;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
679
680
  	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, s387, 0,
  				 offsetof(struct i387_soft_struct, st_space));
3d0d14f98   Ingo Molnar   x86: lindent arch...
681
  	RE_ENTRANT_CHECK_ON;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
682
683
  	if (ret)
  		return ret;
3d0d14f98   Ingo Molnar   x86: lindent arch...
684
685
686
687
688
689
  
  	S387->ftop = (S387->swd >> SW_Top_Shift) & 7;
  	offset = (S387->ftop & 7) * 10;
  	other = 80 - offset;
  
  	RE_ENTRANT_CHECK_OFF;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
690

3d0d14f98   Ingo Molnar   x86: lindent arch...
691
  	/* Copy all registers in stack order. */
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
692
693
694
695
696
  	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  				 space + offset, 0, other);
  	if (!ret && offset)
  		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  					 space, 0, offset);
3d0d14f98   Ingo Molnar   x86: lindent arch...
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  	RE_ENTRANT_CHECK_ON;
  
  	/* The tags may need to be corrected now. */
  	tags = S387->twd;
  	newtop = S387->ftop;
  	for (i = 0; i < 8; i++) {
  		regnr = (i + newtop) & 7;
  		if (((tags >> ((regnr & 7) * 2)) & 3) != TAG_Empty) {
  			/* The loaded data over-rides all other cases. */
  			tag =
  			    FPU_tagof((FPU_REG *) ((u_char *) S387->st_space +
  						   10 * regnr));
  			tags &= ~(3 << (regnr * 2));
  			tags |= (tag & 3) << (regnr * 2);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
  	}
3d0d14f98   Ingo Molnar   x86: lindent arch...
713
  	S387->twd = tags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714

ff0ebb23c   Roland McGrath   x86: x86 user_reg...
715
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
  }
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
717
718
719
720
  int fpregs_soft_get(struct task_struct *target,
  		    const struct user_regset *regset,
  		    unsigned int pos, unsigned int count,
  		    void *kbuf, void __user *ubuf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
  {
c3f8978ea   H. Peter Anvin   x86, fpu: Unbreak...
722
  	struct i387_soft_struct *s387 = &target->thread.fpu.state->soft;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
723
724
  	const void *space = s387->st_space;
  	int ret;
3d0d14f98   Ingo Molnar   x86: lindent arch...
725
  	int offset = (S387->ftop & 7) * 10, other = 80 - offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726

3d0d14f98   Ingo Molnar   x86: lindent arch...
727
  	RE_ENTRANT_CHECK_OFF;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
728

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  #ifdef PECULIAR_486
3d0d14f98   Ingo Molnar   x86: lindent arch...
730
731
732
733
734
735
736
  	S387->cwd &= ~0xe080;
  	/* An 80486 sets nearly all of the reserved bits to 1. */
  	S387->cwd |= 0xffff0040;
  	S387->swd = sstatus_word() | 0xffff0000;
  	S387->twd |= 0xffff0000;
  	S387->fcs &= ~0xf8000000;
  	S387->fos |= 0xffff0000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
  #endif /* PECULIAR_486 */
3d0d14f98   Ingo Molnar   x86: lindent arch...
738

ff0ebb23c   Roland McGrath   x86: x86 user_reg...
739
740
  	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, s387, 0,
  				  offsetof(struct i387_soft_struct, st_space));
3d0d14f98   Ingo Molnar   x86: lindent arch...
741

3d0d14f98   Ingo Molnar   x86: lindent arch...
742
  	/* Copy all registers in stack order. */
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
743
744
745
746
747
748
  	if (!ret)
  		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  					  space + offset, 0, other);
  	if (!ret)
  		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  					  space, 0, offset);
3d0d14f98   Ingo Molnar   x86: lindent arch...
749
  	RE_ENTRANT_CHECK_ON;
ff0ebb23c   Roland McGrath   x86: x86 user_reg...
750
751
  	return ret;
  }