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 +---------------------------------------------------------------------------*/
12
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 +---------------------------------------------------------------------------*/
19
20
21 #include <linux/stddef.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/desc.h>
25
26 #include "fpu_system.h"
27 #include "exception.h"
28 #include "fpu_emu.h"
29
30
31 #define FPU_WRITE_BIT 0x10
32
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)
42 };
43
44 #define REG_(x) (*(long *)(reg_offset[(x)]+(u_char *) FPU_info))
45
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)
54 };
55
56 #define VM86_REG_(x) (*(unsigned short *) \
57 (reg_offset_vm86[((unsigned)x)]+(u_char *) FPU_info))
58
59 /* These are dummy, fs and gs are not saved on the stack. */
60 #define ___FS ___ds
61 #define ___GS ___ds
62
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)
71 };
72
73 #define PM_REG_(x) (*(unsigned short *) \
74 (reg_offset_pm[((unsigned)x)]+(u_char *) FPU_info))
75
76
77 /* Decode the SIB byte. This function assumes mod != 0 */
sib(int mod,unsigned long * fpu_eip)78 static int sib(int mod, unsigned long *fpu_eip)
79 {
80 u_char ss,index,base;
81 long offset;
82
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;
91
92 if ((mod == 0) && (base == 5))
93 offset = 0; /* No base register */
94 else
95 offset = REG_(base);
96
97 if (index == 4)
98 {
99 /* No index register */
100 /* A non-zero ss is illegal */
101 if ( ss )
102 EXCEPTION(EX_Invalid);
103 }
104 else
105 {
106 offset += (REG_(index)) << ss;
107 }
108
109 if (mod == 1)
110 {
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)++;
119 }
120 else if (mod == 2 || base == 5) /* The second condition also has mod==0 */
121 {
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;
130 }
131
132 return offset;
133 }
134
135
vm86_segment(u_char segment,struct address * addr)136 static unsigned long vm86_segment(u_char segment,
137 struct address *addr)
138 {
139 segment--;
140 #ifdef PARANOID
141 if ( segment > PREFIX_SS_ )
142 {
143 EXCEPTION(EX_INTERNAL|0x130);
144 math_abort(FPU_info,SIGSEGV);
145 }
146 #endif /* PARANOID */
147 addr->selector = VM86_REG_(segment);
148 return (unsigned long)VM86_REG_(segment) << 4;
149 }
150
151
152 /* This should work for 16 and 32 bit protected mode. */
pm_address(u_char FPU_modrm,u_char segment,struct address * addr,long offset)153 static long pm_address(u_char FPU_modrm, u_char segment,
154 struct address *addr, long offset)
155 {
156 struct desc_struct descriptor;
157 unsigned long base_address, limit, address, seg_top;
158 unsigned short selector;
159
160 segment--;
161
162 #ifdef PARANOID
163 /* segment is unsigned, so this also detects if segment was 0: */
164 if ( segment > PREFIX_SS_ )
165 {
166 EXCEPTION(EX_INTERNAL|0x132);
167 math_abort(FPU_info,SIGSEGV);
168 }
169 #endif /* PARANOID */
170
171 switch ( segment )
172 {
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. */
178
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);
190 }
191
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;
198
199 if ( SEG_EXPAND_DOWN(descriptor) )
200 {
201 if ( SEG_G_BIT(descriptor) )
202 seg_top = 0xffffffff;
203 else
204 {
205 seg_top = base_address + (1 << 20);
206 if ( seg_top < base_address ) seg_top = 0xffffffff;
207 }
208 access_limit =
209 (address <= limit) || (address >= seg_top) ? 0 :
210 ((seg_top-address) >= 255 ? 255 : seg_top-address);
211 }
212 else
213 {
214 access_limit =
215 (address > limit) || (address < base_address) ? 0 :
216 ((limit-address) >= 254 ? 255 : limit-address+1);
217 }
218 if ( SEG_EXECUTE_ONLY(descriptor) ||
219 (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT)) )
220 {
221 access_limit = 0;
222 }
223 return address;
224 }
225
226
227 /*
228 MOD R/M byte: MOD == 3 has a special use for the FPU
229 SIB byte used iff R/M = 100b
230
231 7 6 5 4 3 2 1 0
232 ..... ......... .........
233 MOD OPCODE(2) R/M
234
235
236 SIB byte
237
238 7 6 5 4 3 2 1 0
239 ..... ......... .........
240 SS INDEX BASE
241
242 */
243
FPU_get_address(u_char FPU_modrm,unsigned long * fpu_eip,struct address * addr,fpu_addr_modes addr_modes)244 void *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
245 struct address *addr,
246 fpu_addr_modes addr_modes)
247 {
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. */
252
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_) )
257 {
258 math_abort(FPU_info,SIGSEGV);
259 }
260
261 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
262
263 mod = (FPU_modrm >> 6) & 3;
264
265 if (rm == 4 && mod != 3)
266 {
267 address = sib(mod, fpu_eip);
268 }
269 else
270 {
271 cpu_reg_ptr = & REG_(rm);
272 switch (mod)
273 {
274 case 0:
275 if (rm == 5)
276 {
277 /* Special case: disp32 */
278 RE_ENTRANT_CHECK_OFF;
279 FPU_code_verify_area(4);
280 FPU_get_user(address, (unsigned long *) (*fpu_eip));
281 (*fpu_eip) += 4;
282 RE_ENTRANT_CHECK_ON;
283 addr->offset = address;
284 return (void *) address;
285 }
286 else
287 {
288 address = *cpu_reg_ptr; /* Just return the contents
289 of the cpu register */
290 addr->offset = address;
291 return (void *) address;
292 }
293 case 1:
294 /* 8 bit signed displacement */
295 RE_ENTRANT_CHECK_OFF;
296 FPU_code_verify_area(1);
297 FPU_get_user(address, (signed char *) (*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_verify_area(4);
305 FPU_get_user(address, (long *) (*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);
312 }
313 address += *cpu_reg_ptr;
314 }
315
316 addr->offset = address;
317
318 switch ( addr_modes.default_mode )
319 {
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);
332 }
333
334 return (void *)address;
335 }
336
337
FPU_get_address_16(u_char FPU_modrm,unsigned long * fpu_eip,struct address * addr,fpu_addr_modes addr_modes)338 void *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
339 struct address *addr,
340 fpu_addr_modes addr_modes)
341 {
342 u_char mod;
343 unsigned rm = FPU_modrm & 7;
344 int address = 0; /* Default used for mod == 0 */
345
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_) )
350 {
351 math_abort(FPU_info,SIGSEGV);
352 }
353
354 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
355
356 mod = (FPU_modrm >> 6) & 3;
357
358 switch (mod)
359 {
360 case 0:
361 if (rm == 6)
362 {
363 /* Special case: disp16 */
364 RE_ENTRANT_CHECK_OFF;
365 FPU_code_verify_area(2);
366 FPU_get_user(address, (unsigned short *) (*fpu_eip));
367 (*fpu_eip) += 2;
368 RE_ENTRANT_CHECK_ON;
369 goto add_segment;
370 }
371 break;
372 case 1:
373 /* 8 bit signed displacement */
374 RE_ENTRANT_CHECK_OFF;
375 FPU_code_verify_area(1);
376 FPU_get_user(address, (signed char *) (*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_verify_area(2);
384 FPU_get_user(address, (unsigned short *) (*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;
392 }
393 switch ( rm )
394 {
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;
425 }
426
427 add_segment:
428 address &= 0xffff;
429
430 addr->offset = address;
431
432 switch ( addr_modes.default_mode )
433 {
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);
446 }
447
448 return (void *)address ;
449 }
450