1 /*---------------------------------------------------------------------------+
4 | Get the effective address from an FPU instruction. |
6 | Copyright (C) 1992,1993,1994,1997 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
8 | Australia. E-mail billm@suburbia.net |
11 +---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------+
15 | The file contains code which accesses user memory. |
16 | Emulator static data may change when user memory is accessed, due to |
17 | other processes using the emulator while swapping is in progress. |
18 +---------------------------------------------------------------------------*/
21 #include <linux/stddef.h>
23 #include <asm/uaccess.h>
26 #include "fpu_system.h"
27 #include "exception.h"
31 #define FPU_WRITE_BIT 0x10
33 static int reg_offset
[] = {
34 offsetof(struct info
,___eax
),
35 offsetof(struct info
,___ecx
),
36 offsetof(struct info
,___edx
),
37 offsetof(struct info
,___ebx
),
38 offsetof(struct info
,___esp
),
39 offsetof(struct info
,___ebp
),
40 offsetof(struct info
,___esi
),
41 offsetof(struct info
,___edi
)
44 #define REG_(x) (*(long *)(reg_offset[(x)]+(u_char *) FPU_info))
46 static int reg_offset_vm86
[] = {
47 offsetof(struct info
,___cs
),
48 offsetof(struct info
,___vm86_ds
),
49 offsetof(struct info
,___vm86_es
),
50 offsetof(struct info
,___vm86_fs
),
51 offsetof(struct info
,___vm86_gs
),
52 offsetof(struct info
,___ss
),
53 offsetof(struct info
,___vm86_ds
)
56 #define VM86_REG_(x) (*(unsigned short *) \
57 (reg_offset_vm86[((unsigned)x)]+(u_char *) FPU_info))
59 /* These are dummy, fs and gs are not saved on the stack. */
63 static int reg_offset_pm
[] = {
64 offsetof(struct info
,___cs
),
65 offsetof(struct info
,___ds
),
66 offsetof(struct info
,___es
),
67 offsetof(struct info
,___FS
),
68 offsetof(struct info
,___GS
),
69 offsetof(struct info
,___ss
),
70 offsetof(struct info
,___ds
)
73 #define PM_REG_(x) (*(unsigned short *) \
74 (reg_offset_pm[((unsigned)x)]+(u_char *) FPU_info))
77 /* Decode the SIB byte. This function assumes mod != 0 */
78 static int sib(int mod
, unsigned long *fpu_eip
)
84 FPU_code_verify_area(1);
85 FPU_get_user(base
, (u_char
*) (*fpu_eip
)); /* The SIB byte */
89 index
= (base
>> 3) & 7;
92 if ((mod
== 0) && (base
== 5))
93 offset
= 0; /* No base register */
99 /* No index register */
100 /* A non-zero ss is illegal */
102 EXCEPTION(EX_Invalid
);
106 offset
+= (REG_(index
)) << ss
;
111 /* 8 bit signed displacement */
113 RE_ENTRANT_CHECK_OFF
;
114 FPU_code_verify_area(1);
115 FPU_get_user(displacement
, (signed char *) (*fpu_eip
));
116 offset
+= displacement
;
120 else if (mod
== 2 || base
== 5) /* The second condition also has mod==0 */
122 /* 32 bit displacement */
124 RE_ENTRANT_CHECK_OFF
;
125 FPU_code_verify_area(4);
126 FPU_get_user(displacement
, (long *) (*fpu_eip
));
127 offset
+= displacement
;
136 static unsigned long vm86_segment(u_char segment
,
137 struct address
*addr
)
141 if ( segment
> PREFIX_SS_
)
143 EXCEPTION(EX_INTERNAL
|0x130);
144 math_abort(FPU_info
,SIGSEGV
);
147 addr
->selector
= VM86_REG_(segment
);
148 return (unsigned long)VM86_REG_(segment
) << 4;
152 /* This should work for 16 and 32 bit protected mode. */
153 static long pm_address(u_char FPU_modrm
, u_char segment
,
154 struct address
*addr
, long offset
)
156 struct desc_struct descriptor
;
157 unsigned long base_address
, limit
, address
, seg_top
;
162 /* segment is unsigned, so this also detects if segment was 0: */
163 if ( segment
> PREFIX_SS_
)
165 EXCEPTION(EX_INTERNAL
|0x132);
166 math_abort(FPU_info
,SIGSEGV
);
172 /* fs and gs aren't used by the kernel, so they still have their
173 user-space values. */
175 /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
176 in the assembler statement. */
177 __asm__("mov %%fs,%0":"=r" ((unsigned short)addr
->selector
));
180 /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
181 in the assembler statement. */
182 __asm__("mov %%gs,%0":"=r" ((unsigned short)addr
->selector
));
185 addr
->selector
= PM_REG_(segment
);
188 descriptor
= LDT_DESCRIPTOR(PM_REG_(segment
));
189 base_address
= SEG_BASE_ADDR(descriptor
);
190 address
= base_address
+ offset
;
192 + (SEG_LIMIT(descriptor
)+1) * SEG_GRANULARITY(descriptor
) - 1;
193 if ( limit
< base_address
) limit
= 0xffffffff;
195 if ( SEG_EXPAND_DOWN(descriptor
) )
197 if ( SEG_G_BIT(descriptor
) )
198 seg_top
= 0xffffffff;
201 seg_top
= base_address
+ (1 << 20);
202 if ( seg_top
< base_address
) seg_top
= 0xffffffff;
205 (address
<= limit
) || (address
>= seg_top
) ? 0 :
206 ((seg_top
-address
) >= 255 ? 255 : seg_top
-address
);
211 (address
> limit
) || (address
< base_address
) ? 0 :
212 ((limit
-address
) >= 254 ? 255 : limit
-address
+1);
214 if ( SEG_EXECUTE_ONLY(descriptor
) ||
215 (!SEG_WRITE_PERM(descriptor
) && (FPU_modrm
& FPU_WRITE_BIT
)) )
224 MOD R/M byte: MOD == 3 has a special use for the FPU
225 SIB byte used iff R/M = 100b
228 ..... ......... .........
235 ..... ......... .........
240 void *FPU_get_address(u_char FPU_modrm
, unsigned long *fpu_eip
,
241 struct address
*addr
,
242 fpu_addr_modes addr_modes
)
245 unsigned rm
= FPU_modrm
& 7;
247 int address
= 0; /* Initialized just to stop compiler warnings. */
249 /* Memory accessed via the cs selector is write protected
250 in `non-segmented' 32 bit protected mode. */
251 if ( !addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
252 && (addr_modes
.override
.segment
== PREFIX_CS_
) )
254 math_abort(FPU_info
,SIGSEGV
);
257 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
259 mod
= (FPU_modrm
>> 6) & 3;
261 if (rm
== 4 && mod
!= 3)
263 address
= sib(mod
, fpu_eip
);
267 cpu_reg_ptr
= & REG_(rm
);
273 /* Special case: disp32 */
274 RE_ENTRANT_CHECK_OFF
;
275 FPU_code_verify_area(4);
276 FPU_get_user(address
, (unsigned long *) (*fpu_eip
));
279 addr
->offset
= address
;
280 return (void *) address
;
284 address
= *cpu_reg_ptr
; /* Just return the contents
285 of the cpu register */
286 addr
->offset
= address
;
287 return (void *) address
;
290 /* 8 bit signed displacement */
291 RE_ENTRANT_CHECK_OFF
;
292 FPU_code_verify_area(1);
293 FPU_get_user(address
, (signed char *) (*fpu_eip
));
298 /* 32 bit displacement */
299 RE_ENTRANT_CHECK_OFF
;
300 FPU_code_verify_area(4);
301 FPU_get_user(address
, (long *) (*fpu_eip
));
306 /* Not legal for the FPU */
307 EXCEPTION(EX_Invalid
);
309 address
+= *cpu_reg_ptr
;
312 addr
->offset
= address
;
314 switch ( addr_modes
.default_mode
)
319 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
323 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
327 EXCEPTION(EX_INTERNAL
|0x133);
330 return (void *)address
;
334 void *FPU_get_address_16(u_char FPU_modrm
, unsigned long *fpu_eip
,
335 struct address
*addr
,
336 fpu_addr_modes addr_modes
)
339 unsigned rm
= FPU_modrm
& 7;
340 int address
= 0; /* Default used for mod == 0 */
342 /* Memory accessed via the cs selector is write protected
343 in `non-segmented' 32 bit protected mode. */
344 if ( !addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
345 && (addr_modes
.override
.segment
== PREFIX_CS_
) )
347 math_abort(FPU_info
,SIGSEGV
);
350 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
352 mod
= (FPU_modrm
>> 6) & 3;
359 /* Special case: disp16 */
360 RE_ENTRANT_CHECK_OFF
;
361 FPU_code_verify_area(2);
362 FPU_get_user(address
, (unsigned short *) (*fpu_eip
));
369 /* 8 bit signed displacement */
370 RE_ENTRANT_CHECK_OFF
;
371 FPU_code_verify_area(1);
372 FPU_get_user(address
, (signed char *) (*fpu_eip
));
377 /* 16 bit displacement */
378 RE_ENTRANT_CHECK_OFF
;
379 FPU_code_verify_area(2);
380 FPU_get_user(address
, (unsigned short *) (*fpu_eip
));
385 /* Not legal for the FPU */
386 EXCEPTION(EX_Invalid
);
392 address
+= FPU_info
->___ebx
+ FPU_info
->___esi
;
395 address
+= FPU_info
->___ebx
+ FPU_info
->___edi
;
398 address
+= FPU_info
->___ebp
+ FPU_info
->___esi
;
399 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
400 addr_modes
.override
.segment
= PREFIX_SS_
;
403 address
+= FPU_info
->___ebp
+ FPU_info
->___edi
;
404 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
405 addr_modes
.override
.segment
= PREFIX_SS_
;
408 address
+= FPU_info
->___esi
;
411 address
+= FPU_info
->___edi
;
414 address
+= FPU_info
->___ebp
;
415 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
416 addr_modes
.override
.segment
= PREFIX_SS_
;
419 address
+= FPU_info
->___ebx
;
426 addr
->offset
= address
;
428 switch ( addr_modes
.default_mode
)
433 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
437 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
441 EXCEPTION(EX_INTERNAL
|0x131);
444 return (void *)address
;