Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / math-emu / get_address.c
blob91175738e94808a8aa5a988f1fe5183211fa58ca
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_access_ok(1);
85 FPU_get_user(base, (u_char __user *) (*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_access_ok(1);
115 FPU_get_user(displacement, (signed char __user *) (*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_access_ok(4);
126 FPU_get_user(displacement, (long __user *) (*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;
158 unsigned short selector;
160 segment--;
162 #ifdef PARANOID
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 */
171 switch ( segment )
173 /* fs and gs aren't used by the kernel, so they still have their
174 user-space values. */
175 case PREFIX_FS_-1:
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;
181 break;
182 case PREFIX_GS_-1:
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;
187 break;
188 default:
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;
195 limit = base_address
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;
203 else
205 seg_top = base_address + (1 << 20);
206 if ( seg_top < base_address ) seg_top = 0xffffffff;
208 access_limit =
209 (address <= limit) || (address >= seg_top) ? 0 :
210 ((seg_top-address) >= 255 ? 255 : seg_top-address);
212 else
214 access_limit =
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)) )
221 access_limit = 0;
223 return address;
228 MOD R/M byte: MOD == 3 has a special use for the FPU
229 SIB byte used iff R/M = 100b
231 7 6 5 4 3 2 1 0
232 ..... ......... .........
233 MOD OPCODE(2) R/M
236 SIB byte
238 7 6 5 4 3 2 1 0
239 ..... ......... .........
240 SS INDEX BASE
244 void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
245 struct address *addr,
246 fpu_addr_modes addr_modes)
248 u_char mod;
249 unsigned rm = FPU_modrm & 7;
250 long *cpu_reg_ptr;
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);
269 else
271 cpu_reg_ptr = & REG_(rm);
272 switch (mod)
274 case 0:
275 if (rm == 5)
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));
281 (*fpu_eip) += 4;
282 RE_ENTRANT_CHECK_ON;
283 addr->offset = address;
284 return (void __user *) address;
286 else
288 address = *cpu_reg_ptr; /* Just return the contents
289 of the cpu register */
290 addr->offset = address;
291 return (void __user *) address;
293 case 1:
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));
298 RE_ENTRANT_CHECK_ON;
299 (*fpu_eip)++;
300 break;
301 case 2:
302 /* 32 bit displacement */
303 RE_ENTRANT_CHECK_OFF;
304 FPU_code_access_ok(4);
305 FPU_get_user(address, (long __user *) (*fpu_eip));
306 (*fpu_eip) += 4;
307 RE_ENTRANT_CHECK_ON;
308 break;
309 case 3:
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 )
320 case 0:
321 break;
322 case VM86:
323 address += vm86_segment(addr_modes.override.segment, addr);
324 break;
325 case PM16:
326 case SEG32:
327 address = pm_address(FPU_modrm, addr_modes.override.segment,
328 addr, address);
329 break;
330 default:
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)
342 u_char mod;
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;
358 switch (mod)
360 case 0:
361 if (rm == 6)
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));
367 (*fpu_eip) += 2;
368 RE_ENTRANT_CHECK_ON;
369 goto add_segment;
371 break;
372 case 1:
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));
377 RE_ENTRANT_CHECK_ON;
378 (*fpu_eip)++;
379 break;
380 case 2:
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));
385 (*fpu_eip) += 2;
386 RE_ENTRANT_CHECK_ON;
387 break;
388 case 3:
389 /* Not legal for the FPU */
390 EXCEPTION(EX_Invalid);
391 break;
393 switch ( rm )
395 case 0:
396 address += FPU_info->___ebx + FPU_info->___esi;
397 break;
398 case 1:
399 address += FPU_info->___ebx + FPU_info->___edi;
400 break;
401 case 2:
402 address += FPU_info->___ebp + FPU_info->___esi;
403 if ( addr_modes.override.segment == PREFIX_DEFAULT )
404 addr_modes.override.segment = PREFIX_SS_;
405 break;
406 case 3:
407 address += FPU_info->___ebp + FPU_info->___edi;
408 if ( addr_modes.override.segment == PREFIX_DEFAULT )
409 addr_modes.override.segment = PREFIX_SS_;
410 break;
411 case 4:
412 address += FPU_info->___esi;
413 break;
414 case 5:
415 address += FPU_info->___edi;
416 break;
417 case 6:
418 address += FPU_info->___ebp;
419 if ( addr_modes.override.segment == PREFIX_DEFAULT )
420 addr_modes.override.segment = PREFIX_SS_;
421 break;
422 case 7:
423 address += FPU_info->___ebx;
424 break;
427 add_segment:
428 address &= 0xffff;
430 addr->offset = address;
432 switch ( addr_modes.default_mode )
434 case 0:
435 break;
436 case VM86:
437 address += vm86_segment(addr_modes.override.segment, addr);
438 break;
439 case PM16:
440 case SEG32:
441 address = pm_address(FPU_modrm, addr_modes.override.segment,
442 addr, address);
443 break;
444 default:
445 EXCEPTION(EX_INTERNAL|0x131);
448 return (void __user *)address ;