[PARISC] Abstract shift register left in .S
[linux-2.6.22.y-op.git] / arch / i386 / math-emu / get_address.c
blob9819b705efa422af8c3f1ee5652fa0649f606811
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;
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 /* N.B. - movl %seg, mem is a 2 byte write regardless of prefix */
176 savesegment(fs, addr->selector);
177 break;
178 case PREFIX_GS_-1:
179 savesegment(gs, addr->selector);
180 break;
181 default:
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;
188 limit = base_address
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;
196 else
198 seg_top = base_address + (1 << 20);
199 if ( seg_top < base_address ) seg_top = 0xffffffff;
201 access_limit =
202 (address <= limit) || (address >= seg_top) ? 0 :
203 ((seg_top-address) >= 255 ? 255 : seg_top-address);
205 else
207 access_limit =
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)) )
214 access_limit = 0;
216 return address;
221 MOD R/M byte: MOD == 3 has a special use for the FPU
222 SIB byte used iff R/M = 100b
224 7 6 5 4 3 2 1 0
225 ..... ......... .........
226 MOD OPCODE(2) R/M
229 SIB byte
231 7 6 5 4 3 2 1 0
232 ..... ......... .........
233 SS INDEX BASE
237 void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
238 struct address *addr,
239 fpu_addr_modes addr_modes)
241 u_char mod;
242 unsigned rm = FPU_modrm & 7;
243 long *cpu_reg_ptr;
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);
262 else
264 cpu_reg_ptr = & REG_(rm);
265 switch (mod)
267 case 0:
268 if (rm == 5)
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));
274 (*fpu_eip) += 4;
275 RE_ENTRANT_CHECK_ON;
276 addr->offset = address;
277 return (void __user *) address;
279 else
281 address = *cpu_reg_ptr; /* Just return the contents
282 of the cpu register */
283 addr->offset = address;
284 return (void __user *) address;
286 case 1:
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));
291 RE_ENTRANT_CHECK_ON;
292 (*fpu_eip)++;
293 break;
294 case 2:
295 /* 32 bit displacement */
296 RE_ENTRANT_CHECK_OFF;
297 FPU_code_access_ok(4);
298 FPU_get_user(address, (long __user *) (*fpu_eip));
299 (*fpu_eip) += 4;
300 RE_ENTRANT_CHECK_ON;
301 break;
302 case 3:
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 )
313 case 0:
314 break;
315 case VM86:
316 address += vm86_segment(addr_modes.override.segment, addr);
317 break;
318 case PM16:
319 case SEG32:
320 address = pm_address(FPU_modrm, addr_modes.override.segment,
321 addr, address);
322 break;
323 default:
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)
335 u_char mod;
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;
351 switch (mod)
353 case 0:
354 if (rm == 6)
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));
360 (*fpu_eip) += 2;
361 RE_ENTRANT_CHECK_ON;
362 goto add_segment;
364 break;
365 case 1:
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));
370 RE_ENTRANT_CHECK_ON;
371 (*fpu_eip)++;
372 break;
373 case 2:
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));
378 (*fpu_eip) += 2;
379 RE_ENTRANT_CHECK_ON;
380 break;
381 case 3:
382 /* Not legal for the FPU */
383 EXCEPTION(EX_Invalid);
384 break;
386 switch ( rm )
388 case 0:
389 address += FPU_info->___ebx + FPU_info->___esi;
390 break;
391 case 1:
392 address += FPU_info->___ebx + FPU_info->___edi;
393 break;
394 case 2:
395 address += FPU_info->___ebp + FPU_info->___esi;
396 if ( addr_modes.override.segment == PREFIX_DEFAULT )
397 addr_modes.override.segment = PREFIX_SS_;
398 break;
399 case 3:
400 address += FPU_info->___ebp + FPU_info->___edi;
401 if ( addr_modes.override.segment == PREFIX_DEFAULT )
402 addr_modes.override.segment = PREFIX_SS_;
403 break;
404 case 4:
405 address += FPU_info->___esi;
406 break;
407 case 5:
408 address += FPU_info->___edi;
409 break;
410 case 6:
411 address += FPU_info->___ebp;
412 if ( addr_modes.override.segment == PREFIX_DEFAULT )
413 addr_modes.override.segment = PREFIX_SS_;
414 break;
415 case 7:
416 address += FPU_info->___ebx;
417 break;
420 add_segment:
421 address &= 0xffff;
423 addr->offset = address;
425 switch ( addr_modes.default_mode )
427 case 0:
428 break;
429 case VM86:
430 address += vm86_segment(addr_modes.override.segment, addr);
431 break;
432 case PM16:
433 case SEG32:
434 address = pm_address(FPU_modrm, addr_modes.override.segment,
435 addr, address);
436 break;
437 default:
438 EXCEPTION(EX_INTERNAL|0x131);
441 return (void __user *)address ;