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;
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);
168 #endif /* PARANOID */
172 /* fs and gs aren't used by the kernel, so they still have their
173 user-space values. */
175 /* N.B. - movl %seg, mem is a 2 byte write regardless of prefix */
176 savesegment(fs, addr->selector);
179 savesegment(gs, addr->selector);
182 addr->selector = PM_REG_(segment);
185 descriptor = LDT_DESCRIPTOR(PM_REG_(segment));
186 base_address = SEG_BASE_ADDR(descriptor);
187 address = base_address + offset;
189 + (SEG_LIMIT(descriptor)+1) * SEG_GRANULARITY(descriptor) - 1;
190 if ( limit < base_address ) limit = 0xffffffff;
192 if ( SEG_EXPAND_DOWN(descriptor) )
194 if ( SEG_G_BIT(descriptor) )
195 seg_top = 0xffffffff;
198 seg_top = base_address + (1 << 20);
199 if ( seg_top < base_address ) seg_top = 0xffffffff;
202 (address <= limit) || (address >= seg_top) ? 0 :
203 ((seg_top-address) >= 255 ? 255 : seg_top-address);
208 (address > limit) || (address < base_address) ? 0 :
209 ((limit-address) >= 254 ? 255 : limit-address+1);
211 if ( SEG_EXECUTE_ONLY(descriptor) ||
212 (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT)) )
221 MOD R/M byte: MOD == 3 has a special use for the FPU
222 SIB byte used iff R/M = 100b
225 ..... ......... .........
232 ..... ......... .........
237 void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
238 struct address *addr,
239 fpu_addr_modes addr_modes)
242 unsigned rm = FPU_modrm & 7;
244 int address = 0; /* Initialized just to stop compiler warnings. */
246 /* Memory accessed via the cs selector is write protected
247 in `non-segmented' 32 bit protected mode. */
248 if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
249 && (addr_modes.override.segment == PREFIX_CS_) )
251 math_abort(FPU_info,SIGSEGV);
254 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
256 mod = (FPU_modrm >> 6) & 3;
258 if (rm == 4 && mod != 3)
260 address = sib(mod, fpu_eip);
264 cpu_reg_ptr = & REG_(rm);
270 /* Special case: disp32 */
271 RE_ENTRANT_CHECK_OFF;
272 FPU_code_access_ok(4);
273 FPU_get_user(address, (unsigned long __user *) (*fpu_eip));
276 addr->offset = address;
277 return (void __user *) address;
281 address = *cpu_reg_ptr; /* Just return the contents
282 of the cpu register */
283 addr->offset = address;
284 return (void __user *) address;
287 /* 8 bit signed displacement */
288 RE_ENTRANT_CHECK_OFF;
289 FPU_code_access_ok(1);
290 FPU_get_user(address, (signed char __user *) (*fpu_eip));
295 /* 32 bit displacement */
296 RE_ENTRANT_CHECK_OFF;
297 FPU_code_access_ok(4);
298 FPU_get_user(address, (long __user *) (*fpu_eip));
303 /* Not legal for the FPU */
304 EXCEPTION(EX_Invalid);
306 address += *cpu_reg_ptr;
309 addr->offset = address;
311 switch ( addr_modes.default_mode )
316 address += vm86_segment(addr_modes.override.segment, addr);
320 address = pm_address(FPU_modrm, addr_modes.override.segment,
324 EXCEPTION(EX_INTERNAL|0x133);
327 return (void __user *)address;
331 void __user *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
332 struct address *addr,
333 fpu_addr_modes addr_modes)
336 unsigned rm = FPU_modrm & 7;
337 int address = 0; /* Default used for mod == 0 */
339 /* Memory accessed via the cs selector is write protected
340 in `non-segmented' 32 bit protected mode. */
341 if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
342 && (addr_modes.override.segment == PREFIX_CS_) )
344 math_abort(FPU_info,SIGSEGV);
347 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
349 mod = (FPU_modrm >> 6) & 3;
356 /* Special case: disp16 */
357 RE_ENTRANT_CHECK_OFF;
358 FPU_code_access_ok(2);
359 FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
366 /* 8 bit signed displacement */
367 RE_ENTRANT_CHECK_OFF;
368 FPU_code_access_ok(1);
369 FPU_get_user(address, (signed char __user *) (*fpu_eip));
374 /* 16 bit displacement */
375 RE_ENTRANT_CHECK_OFF;
376 FPU_code_access_ok(2);
377 FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
382 /* Not legal for the FPU */
383 EXCEPTION(EX_Invalid);
389 address += FPU_info->___ebx + FPU_info->___esi;
392 address += FPU_info->___ebx + FPU_info->___edi;
395 address += FPU_info->___ebp + FPU_info->___esi;
396 if ( addr_modes.override.segment == PREFIX_DEFAULT )
397 addr_modes.override.segment = PREFIX_SS_;
400 address += FPU_info->___ebp + FPU_info->___edi;
401 if ( addr_modes.override.segment == PREFIX_DEFAULT )
402 addr_modes.override.segment = PREFIX_SS_;
405 address += FPU_info->___esi;
408 address += FPU_info->___edi;
411 address += FPU_info->___ebp;
412 if ( addr_modes.override.segment == PREFIX_DEFAULT )
413 addr_modes.override.segment = PREFIX_SS_;
416 address += FPU_info->___ebx;
423 addr->offset = address;
425 switch ( addr_modes.default_mode )
430 address += vm86_segment(addr_modes.override.segment, addr);
434 address = pm_address(FPU_modrm, addr_modes.override.segment,
438 EXCEPTION(EX_INTERNAL|0x131);
441 return (void __user *)address ;