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_access_ok(1);
85 FPU_get_user(base
, (u_char __user
*) (*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_access_ok(1);
115 FPU_get_user(displacement
, (signed char __user
*) (*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_access_ok(4);
126 FPU_get_user(displacement
, (long __user
*) (*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
);
146 #endif /* PARANOID */
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
;
158 unsigned short selector
;
163 /* segment is unsigned, so this also detects if segment was 0: */
164 if ( segment
> PREFIX_SS_
)
166 EXCEPTION(EX_INTERNAL
|0x132);
167 math_abort(FPU_info
,SIGSEGV
);
169 #endif /* PARANOID */
173 /* fs and gs aren't used by the kernel, so they still have their
174 user-space values. */
176 /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
177 in the assembler statement. */
179 __asm__("mov %%fs,%0":"=r" (selector
));
180 addr
->selector
= selector
;
183 /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
184 in the assembler statement. */
185 __asm__("mov %%gs,%0":"=r" (selector
));
186 addr
->selector
= selector
;
189 addr
->selector
= PM_REG_(segment
);
192 descriptor
= LDT_DESCRIPTOR(PM_REG_(segment
));
193 base_address
= SEG_BASE_ADDR(descriptor
);
194 address
= base_address
+ offset
;
196 + (SEG_LIMIT(descriptor
)+1) * SEG_GRANULARITY(descriptor
) - 1;
197 if ( limit
< base_address
) limit
= 0xffffffff;
199 if ( SEG_EXPAND_DOWN(descriptor
) )
201 if ( SEG_G_BIT(descriptor
) )
202 seg_top
= 0xffffffff;
205 seg_top
= base_address
+ (1 << 20);
206 if ( seg_top
< base_address
) seg_top
= 0xffffffff;
209 (address
<= limit
) || (address
>= seg_top
) ? 0 :
210 ((seg_top
-address
) >= 255 ? 255 : seg_top
-address
);
215 (address
> limit
) || (address
< base_address
) ? 0 :
216 ((limit
-address
) >= 254 ? 255 : limit
-address
+1);
218 if ( SEG_EXECUTE_ONLY(descriptor
) ||
219 (!SEG_WRITE_PERM(descriptor
) && (FPU_modrm
& FPU_WRITE_BIT
)) )
228 MOD R/M byte: MOD == 3 has a special use for the FPU
229 SIB byte used iff R/M = 100b
232 ..... ......... .........
239 ..... ......... .........
244 void __user
*FPU_get_address(u_char FPU_modrm
, unsigned long *fpu_eip
,
245 struct address
*addr
,
246 fpu_addr_modes addr_modes
)
249 unsigned rm
= FPU_modrm
& 7;
251 int address
= 0; /* Initialized just to stop compiler warnings. */
253 /* Memory accessed via the cs selector is write protected
254 in `non-segmented' 32 bit protected mode. */
255 if ( !addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
256 && (addr_modes
.override
.segment
== PREFIX_CS_
) )
258 math_abort(FPU_info
,SIGSEGV
);
261 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
263 mod
= (FPU_modrm
>> 6) & 3;
265 if (rm
== 4 && mod
!= 3)
267 address
= sib(mod
, fpu_eip
);
271 cpu_reg_ptr
= & REG_(rm
);
277 /* Special case: disp32 */
278 RE_ENTRANT_CHECK_OFF
;
279 FPU_code_access_ok(4);
280 FPU_get_user(address
, (unsigned long __user
*) (*fpu_eip
));
283 addr
->offset
= address
;
284 return (void __user
*) address
;
288 address
= *cpu_reg_ptr
; /* Just return the contents
289 of the cpu register */
290 addr
->offset
= address
;
291 return (void __user
*) address
;
294 /* 8 bit signed displacement */
295 RE_ENTRANT_CHECK_OFF
;
296 FPU_code_access_ok(1);
297 FPU_get_user(address
, (signed char __user
*) (*fpu_eip
));
302 /* 32 bit displacement */
303 RE_ENTRANT_CHECK_OFF
;
304 FPU_code_access_ok(4);
305 FPU_get_user(address
, (long __user
*) (*fpu_eip
));
310 /* Not legal for the FPU */
311 EXCEPTION(EX_Invalid
);
313 address
+= *cpu_reg_ptr
;
316 addr
->offset
= address
;
318 switch ( addr_modes
.default_mode
)
323 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
327 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
331 EXCEPTION(EX_INTERNAL
|0x133);
334 return (void __user
*)address
;
338 void __user
*FPU_get_address_16(u_char FPU_modrm
, unsigned long *fpu_eip
,
339 struct address
*addr
,
340 fpu_addr_modes addr_modes
)
343 unsigned rm
= FPU_modrm
& 7;
344 int address
= 0; /* Default used for mod == 0 */
346 /* Memory accessed via the cs selector is write protected
347 in `non-segmented' 32 bit protected mode. */
348 if ( !addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
349 && (addr_modes
.override
.segment
== PREFIX_CS_
) )
351 math_abort(FPU_info
,SIGSEGV
);
354 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
356 mod
= (FPU_modrm
>> 6) & 3;
363 /* Special case: disp16 */
364 RE_ENTRANT_CHECK_OFF
;
365 FPU_code_access_ok(2);
366 FPU_get_user(address
, (unsigned short __user
*) (*fpu_eip
));
373 /* 8 bit signed displacement */
374 RE_ENTRANT_CHECK_OFF
;
375 FPU_code_access_ok(1);
376 FPU_get_user(address
, (signed char __user
*) (*fpu_eip
));
381 /* 16 bit displacement */
382 RE_ENTRANT_CHECK_OFF
;
383 FPU_code_access_ok(2);
384 FPU_get_user(address
, (unsigned short __user
*) (*fpu_eip
));
389 /* Not legal for the FPU */
390 EXCEPTION(EX_Invalid
);
396 address
+= FPU_info
->___ebx
+ FPU_info
->___esi
;
399 address
+= FPU_info
->___ebx
+ FPU_info
->___edi
;
402 address
+= FPU_info
->___ebp
+ FPU_info
->___esi
;
403 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
404 addr_modes
.override
.segment
= PREFIX_SS_
;
407 address
+= FPU_info
->___ebp
+ FPU_info
->___edi
;
408 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
409 addr_modes
.override
.segment
= PREFIX_SS_
;
412 address
+= FPU_info
->___esi
;
415 address
+= FPU_info
->___edi
;
418 address
+= FPU_info
->___ebp
;
419 if ( addr_modes
.override
.segment
== PREFIX_DEFAULT
)
420 addr_modes
.override
.segment
= PREFIX_SS_
;
423 address
+= FPU_info
->___ebx
;
430 addr
->offset
= address
;
432 switch ( addr_modes
.default_mode
)
437 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
441 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
445 EXCEPTION(EX_INTERNAL
|0x131);
448 return (void __user
*)address
;