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 /* This dummy, gs is not saved on the stack. */
62 static int reg_offset_pm[] = {
63 offsetof(struct info,___cs),
64 offsetof(struct info,___ds),
65 offsetof(struct info,___es),
66 offsetof(struct info,___fs),
67 offsetof(struct info,___GS),
68 offsetof(struct info,___ss),
69 offsetof(struct info,___ds)
72 #define PM_REG_(x) (*(unsigned short *) \
73 (reg_offset_pm[((unsigned)x)]+(u_char *) FPU_info))
76 /* Decode the SIB byte. This function assumes mod != 0 */
77 static int sib(int mod, unsigned long *fpu_eip)
83 FPU_code_access_ok(1);
84 FPU_get_user(base, (u_char __user *) (*fpu_eip)); /* The SIB byte */
88 index = (base >> 3) & 7;
91 if ((mod == 0) && (base == 5))
92 offset = 0; /* No base register */
98 /* No index register */
99 /* A non-zero ss is illegal */
101 EXCEPTION(EX_Invalid);
105 offset += (REG_(index)) << ss;
110 /* 8 bit signed displacement */
112 RE_ENTRANT_CHECK_OFF;
113 FPU_code_access_ok(1);
114 FPU_get_user(displacement, (signed char __user *) (*fpu_eip));
115 offset += displacement;
119 else if (mod == 2 || base == 5) /* The second condition also has mod==0 */
121 /* 32 bit displacement */
123 RE_ENTRANT_CHECK_OFF;
124 FPU_code_access_ok(4);
125 FPU_get_user(displacement, (long __user *) (*fpu_eip));
126 offset += displacement;
135 static unsigned long vm86_segment(u_char segment,
136 struct address *addr)
140 if ( segment > PREFIX_SS_ )
142 EXCEPTION(EX_INTERNAL|0x130);
143 math_abort(FPU_info,SIGSEGV);
145 #endif /* PARANOID */
146 addr->selector = VM86_REG_(segment);
147 return (unsigned long)VM86_REG_(segment) << 4;
151 /* This should work for 16 and 32 bit protected mode. */
152 static long pm_address(u_char FPU_modrm, u_char segment,
153 struct address *addr, long offset)
155 struct desc_struct descriptor;
156 unsigned long base_address, limit, address, seg_top;
161 /* segment is unsigned, so this also detects if segment was 0: */
162 if ( segment > PREFIX_SS_ )
164 EXCEPTION(EX_INTERNAL|0x132);
165 math_abort(FPU_info,SIGSEGV);
167 #endif /* PARANOID */
171 /* gs isn't used by the kernel, so it still has its
174 /* N.B. - movl %seg, mem is a 2 byte write regardless of prefix */
175 savesegment(gs, addr->selector);
178 addr->selector = PM_REG_(segment);
181 descriptor = LDT_DESCRIPTOR(PM_REG_(segment));
182 base_address = SEG_BASE_ADDR(descriptor);
183 address = base_address + offset;
185 + (SEG_LIMIT(descriptor)+1) * SEG_GRANULARITY(descriptor) - 1;
186 if ( limit < base_address ) limit = 0xffffffff;
188 if ( SEG_EXPAND_DOWN(descriptor) )
190 if ( SEG_G_BIT(descriptor) )
191 seg_top = 0xffffffff;
194 seg_top = base_address + (1 << 20);
195 if ( seg_top < base_address ) seg_top = 0xffffffff;
198 (address <= limit) || (address >= seg_top) ? 0 :
199 ((seg_top-address) >= 255 ? 255 : seg_top-address);
204 (address > limit) || (address < base_address) ? 0 :
205 ((limit-address) >= 254 ? 255 : limit-address+1);
207 if ( SEG_EXECUTE_ONLY(descriptor) ||
208 (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT)) )
217 MOD R/M byte: MOD == 3 has a special use for the FPU
218 SIB byte used iff R/M = 100b
221 ..... ......... .........
228 ..... ......... .........
233 void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
234 struct address *addr,
235 fpu_addr_modes addr_modes)
238 unsigned rm = FPU_modrm & 7;
240 int address = 0; /* Initialized just to stop compiler warnings. */
242 /* Memory accessed via the cs selector is write protected
243 in `non-segmented' 32 bit protected mode. */
244 if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
245 && (addr_modes.override.segment == PREFIX_CS_) )
247 math_abort(FPU_info,SIGSEGV);
250 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
252 mod = (FPU_modrm >> 6) & 3;
254 if (rm == 4 && mod != 3)
256 address = sib(mod, fpu_eip);
260 cpu_reg_ptr = & REG_(rm);
266 /* Special case: disp32 */
267 RE_ENTRANT_CHECK_OFF;
268 FPU_code_access_ok(4);
269 FPU_get_user(address, (unsigned long __user *) (*fpu_eip));
272 addr->offset = address;
273 return (void __user *) address;
277 address = *cpu_reg_ptr; /* Just return the contents
278 of the cpu register */
279 addr->offset = address;
280 return (void __user *) address;
283 /* 8 bit signed displacement */
284 RE_ENTRANT_CHECK_OFF;
285 FPU_code_access_ok(1);
286 FPU_get_user(address, (signed char __user *) (*fpu_eip));
291 /* 32 bit displacement */
292 RE_ENTRANT_CHECK_OFF;
293 FPU_code_access_ok(4);
294 FPU_get_user(address, (long __user *) (*fpu_eip));
299 /* Not legal for the FPU */
300 EXCEPTION(EX_Invalid);
302 address += *cpu_reg_ptr;
305 addr->offset = address;
307 switch ( addr_modes.default_mode )
312 address += vm86_segment(addr_modes.override.segment, addr);
316 address = pm_address(FPU_modrm, addr_modes.override.segment,
320 EXCEPTION(EX_INTERNAL|0x133);
323 return (void __user *)address;
327 void __user *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
328 struct address *addr,
329 fpu_addr_modes addr_modes)
332 unsigned rm = FPU_modrm & 7;
333 int address = 0; /* Default used for mod == 0 */
335 /* Memory accessed via the cs selector is write protected
336 in `non-segmented' 32 bit protected mode. */
337 if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
338 && (addr_modes.override.segment == PREFIX_CS_) )
340 math_abort(FPU_info,SIGSEGV);
343 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
345 mod = (FPU_modrm >> 6) & 3;
352 /* Special case: disp16 */
353 RE_ENTRANT_CHECK_OFF;
354 FPU_code_access_ok(2);
355 FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
362 /* 8 bit signed displacement */
363 RE_ENTRANT_CHECK_OFF;
364 FPU_code_access_ok(1);
365 FPU_get_user(address, (signed char __user *) (*fpu_eip));
370 /* 16 bit displacement */
371 RE_ENTRANT_CHECK_OFF;
372 FPU_code_access_ok(2);
373 FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
378 /* Not legal for the FPU */
379 EXCEPTION(EX_Invalid);
385 address += FPU_info->___ebx + FPU_info->___esi;
388 address += FPU_info->___ebx + FPU_info->___edi;
391 address += FPU_info->___ebp + FPU_info->___esi;
392 if ( addr_modes.override.segment == PREFIX_DEFAULT )
393 addr_modes.override.segment = PREFIX_SS_;
396 address += FPU_info->___ebp + FPU_info->___edi;
397 if ( addr_modes.override.segment == PREFIX_DEFAULT )
398 addr_modes.override.segment = PREFIX_SS_;
401 address += FPU_info->___esi;
404 address += FPU_info->___edi;
407 address += FPU_info->___ebp;
408 if ( addr_modes.override.segment == PREFIX_DEFAULT )
409 addr_modes.override.segment = PREFIX_SS_;
412 address += FPU_info->___ebx;
419 addr->offset = address;
421 switch ( addr_modes.default_mode )
426 address += vm86_segment(addr_modes.override.segment, addr);
430 address = pm_address(FPU_modrm, addr_modes.override.segment,
434 EXCEPTION(EX_INTERNAL|0x131);
437 return (void __user *)address ;