Import 2.3.13pre1
[davej-history.git] / arch / i386 / math-emu / get_address.c
blob6462f0fa2415202de60539f75d268a5fb70db355
1 /*---------------------------------------------------------------------------+
2 | get_address.c |
3 | |
4 | Get the effective address from an FPU instruction. |
5 | |
6 | Copyright (C) 1992,1993,1994,1997 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
8 | Australia. E-mail billm@suburbia.net |
9 | |
10 | |
11 +---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------+
14 | Note: |
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>
24 #include <asm/desc.h>
26 #include "fpu_system.h"
27 #include "exception.h"
28 #include "fpu_emu.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. */
60 #define ___FS ___ds
61 #define ___GS ___ds
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)
80 u_char ss,index,base;
81 long offset;
83 RE_ENTRANT_CHECK_OFF;
84 FPU_code_verify_area(1);
85 FPU_get_user(base, (u_char *) (*fpu_eip)); /* The SIB byte */
86 RE_ENTRANT_CHECK_ON;
87 (*fpu_eip)++;
88 ss = base >> 6;
89 index = (base >> 3) & 7;
90 base &= 7;
92 if ((mod == 0) && (base == 5))
93 offset = 0; /* No base register */
94 else
95 offset = REG_(base);
97 if (index == 4)
99 /* No index register */
100 /* A non-zero ss is illegal */
101 if ( ss )
102 EXCEPTION(EX_Invalid);
104 else
106 offset += (REG_(index)) << ss;
109 if (mod == 1)
111 /* 8 bit signed displacement */
112 long displacement;
113 RE_ENTRANT_CHECK_OFF;
114 FPU_code_verify_area(1);
115 FPU_get_user(displacement, (signed char *) (*fpu_eip));
116 offset += displacement;
117 RE_ENTRANT_CHECK_ON;
118 (*fpu_eip)++;
120 else if (mod == 2 || base == 5) /* The second condition also has mod==0 */
122 /* 32 bit displacement */
123 long displacement;
124 RE_ENTRANT_CHECK_OFF;
125 FPU_code_verify_area(4);
126 FPU_get_user(displacement, (long *) (*fpu_eip));
127 offset += displacement;
128 RE_ENTRANT_CHECK_ON;
129 (*fpu_eip) += 4;
132 return offset;
136 static unsigned long vm86_segment(u_char segment,
137 struct address *addr)
139 segment--;
140 #ifdef PARANOID
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;
159 segment--;
161 #ifdef PARANOID
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
170 switch ( segment )
172 /* fs and gs aren't used by the kernel, so they still have their
173 user-space values. */
174 case PREFIX_FS_-1:
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));
178 break;
179 case PREFIX_GS_-1:
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));
183 break;
184 default:
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;
191 limit = base_address
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;
199 else
201 seg_top = base_address + (1 << 20);
202 if ( seg_top < base_address ) seg_top = 0xffffffff;
204 access_limit =
205 (address <= limit) || (address >= seg_top) ? 0 :
206 ((seg_top-address) >= 255 ? 255 : seg_top-address);
208 else
210 access_limit =
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)) )
217 access_limit = 0;
219 return address;
224 MOD R/M byte: MOD == 3 has a special use for the FPU
225 SIB byte used iff R/M = 100b
227 7 6 5 4 3 2 1 0
228 ..... ......... .........
229 MOD OPCODE(2) R/M
232 SIB byte
234 7 6 5 4 3 2 1 0
235 ..... ......... .........
236 SS INDEX BASE
240 void *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
241 struct address *addr,
242 fpu_addr_modes addr_modes)
244 u_char mod;
245 unsigned rm = FPU_modrm & 7;
246 long *cpu_reg_ptr;
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);
265 else
267 cpu_reg_ptr = & REG_(rm);
268 switch (mod)
270 case 0:
271 if (rm == 5)
273 /* Special case: disp32 */
274 RE_ENTRANT_CHECK_OFF;
275 FPU_code_verify_area(4);
276 FPU_get_user(address, (unsigned long *) (*fpu_eip));
277 (*fpu_eip) += 4;
278 RE_ENTRANT_CHECK_ON;
279 addr->offset = address;
280 return (void *) address;
282 else
284 address = *cpu_reg_ptr; /* Just return the contents
285 of the cpu register */
286 addr->offset = address;
287 return (void *) address;
289 case 1:
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));
294 RE_ENTRANT_CHECK_ON;
295 (*fpu_eip)++;
296 break;
297 case 2:
298 /* 32 bit displacement */
299 RE_ENTRANT_CHECK_OFF;
300 FPU_code_verify_area(4);
301 FPU_get_user(address, (long *) (*fpu_eip));
302 (*fpu_eip) += 4;
303 RE_ENTRANT_CHECK_ON;
304 break;
305 case 3:
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 )
316 case 0:
317 break;
318 case VM86:
319 address += vm86_segment(addr_modes.override.segment, addr);
320 break;
321 case PM16:
322 case SEG32:
323 address = pm_address(FPU_modrm, addr_modes.override.segment,
324 addr, address);
325 break;
326 default:
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)
338 u_char mod;
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;
354 switch (mod)
356 case 0:
357 if (rm == 6)
359 /* Special case: disp16 */
360 RE_ENTRANT_CHECK_OFF;
361 FPU_code_verify_area(2);
362 FPU_get_user(address, (unsigned short *) (*fpu_eip));
363 (*fpu_eip) += 2;
364 RE_ENTRANT_CHECK_ON;
365 goto add_segment;
367 break;
368 case 1:
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));
373 RE_ENTRANT_CHECK_ON;
374 (*fpu_eip)++;
375 break;
376 case 2:
377 /* 16 bit displacement */
378 RE_ENTRANT_CHECK_OFF;
379 FPU_code_verify_area(2);
380 FPU_get_user(address, (unsigned short *) (*fpu_eip));
381 (*fpu_eip) += 2;
382 RE_ENTRANT_CHECK_ON;
383 break;
384 case 3:
385 /* Not legal for the FPU */
386 EXCEPTION(EX_Invalid);
387 break;
389 switch ( rm )
391 case 0:
392 address += FPU_info->___ebx + FPU_info->___esi;
393 break;
394 case 1:
395 address += FPU_info->___ebx + FPU_info->___edi;
396 break;
397 case 2:
398 address += FPU_info->___ebp + FPU_info->___esi;
399 if ( addr_modes.override.segment == PREFIX_DEFAULT )
400 addr_modes.override.segment = PREFIX_SS_;
401 break;
402 case 3:
403 address += FPU_info->___ebp + FPU_info->___edi;
404 if ( addr_modes.override.segment == PREFIX_DEFAULT )
405 addr_modes.override.segment = PREFIX_SS_;
406 break;
407 case 4:
408 address += FPU_info->___esi;
409 break;
410 case 5:
411 address += FPU_info->___edi;
412 break;
413 case 6:
414 address += FPU_info->___ebp;
415 if ( addr_modes.override.segment == PREFIX_DEFAULT )
416 addr_modes.override.segment = PREFIX_SS_;
417 break;
418 case 7:
419 address += FPU_info->___ebx;
420 break;
423 add_segment:
424 address &= 0xffff;
426 addr->offset = address;
428 switch ( addr_modes.default_mode )
430 case 0:
431 break;
432 case VM86:
433 address += vm86_segment(addr_modes.override.segment, addr);
434 break;
435 case PM16:
436 case SEG32:
437 address = pm_address(FPU_modrm, addr_modes.override.segment,
438 addr, address);
439 break;
440 default:
441 EXCEPTION(EX_INTERNAL|0x131);
444 return (void *)address ;