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 +---------------------------------------------------------------------------*/
20 #include <linux/stddef.h>
22 #include <asm/uaccess.h>
25 #include "fpu_system.h"
26 #include "exception.h"
29 #define FPU_WRITE_BIT 0x10
31 static int reg_offset[] = {
32 offsetof(struct info, ___eax),
33 offsetof(struct info, ___ecx),
34 offsetof(struct info, ___edx),
35 offsetof(struct info, ___ebx),
36 offsetof(struct info, ___esp),
37 offsetof(struct info, ___ebp),
38 offsetof(struct info, ___esi),
39 offsetof(struct info, ___edi)
42 #define REG_(x) (*(long *)(reg_offset[(x)]+(u_char *) FPU_info))
44 static int reg_offset_vm86[] = {
45 offsetof(struct info, ___cs),
46 offsetof(struct info, ___vm86_ds),
47 offsetof(struct info, ___vm86_es),
48 offsetof(struct info, ___vm86_fs),
49 offsetof(struct info, ___vm86_gs),
50 offsetof(struct info, ___ss),
51 offsetof(struct info, ___vm86_ds)
54 #define VM86_REG_(x) (*(unsigned short *) \
55 (reg_offset_vm86[((unsigned)x)]+(u_char *) FPU_info))
57 /* This dummy, gs is not saved on the stack. */
60 static int reg_offset_pm[] = {
61 offsetof(struct info, ___cs),
62 offsetof(struct info, ___ds),
63 offsetof(struct info, ___es),
64 offsetof(struct info, ___fs),
65 offsetof(struct info, ___GS),
66 offsetof(struct info, ___ss),
67 offsetof(struct info, ___ds)
70 #define PM_REG_(x) (*(unsigned short *) \
71 (reg_offset_pm[((unsigned)x)]+(u_char *) FPU_info))
73 /* Decode the SIB byte. This function assumes mod != 0 */
74 static int sib(int mod, unsigned long *fpu_eip)
76 u_char ss, index, base;
80 FPU_code_access_ok(1);
81 FPU_get_user(base, (u_char __user *) (*fpu_eip)); /* The SIB byte */
85 index = (base >> 3) & 7;
88 if ((mod == 0) && (base == 5))
89 offset = 0; /* No base register */
94 /* No index register */
95 /* A non-zero ss is illegal */
97 EXCEPTION(EX_Invalid);
99 offset += (REG_(index)) << ss;
103 /* 8 bit signed displacement */
105 RE_ENTRANT_CHECK_OFF;
106 FPU_code_access_ok(1);
107 FPU_get_user(displacement, (signed char __user *)(*fpu_eip));
108 offset += displacement;
111 } else if (mod == 2 || base == 5) { /* The second condition also has mod==0 */
112 /* 32 bit displacement */
114 RE_ENTRANT_CHECK_OFF;
115 FPU_code_access_ok(4);
116 FPU_get_user(displacement, (long __user *)(*fpu_eip));
117 offset += displacement;
125 static unsigned long vm86_segment(u_char segment, struct address *addr)
129 if (segment > PREFIX_SS_) {
130 EXCEPTION(EX_INTERNAL | 0x130);
131 math_abort(FPU_info, SIGSEGV);
133 #endif /* PARANOID */
134 addr->selector = VM86_REG_(segment);
135 return (unsigned long)VM86_REG_(segment) << 4;
138 /* This should work for 16 and 32 bit protected mode. */
139 static long pm_address(u_char FPU_modrm, u_char segment,
140 struct address *addr, long offset)
142 struct desc_struct descriptor;
143 unsigned long base_address, limit, address, seg_top;
148 /* segment is unsigned, so this also detects if segment was 0: */
149 if (segment > PREFIX_SS_) {
150 EXCEPTION(EX_INTERNAL | 0x132);
151 math_abort(FPU_info, SIGSEGV);
153 #endif /* PARANOID */
156 /* gs isn't used by the kernel, so it still has its
159 /* N.B. - movl %seg, mem is a 2 byte write regardless of prefix */
160 savesegment(gs, addr->selector);
163 addr->selector = PM_REG_(segment);
166 descriptor = LDT_DESCRIPTOR(PM_REG_(segment));
167 base_address = SEG_BASE_ADDR(descriptor);
168 address = base_address + offset;
170 + (SEG_LIMIT(descriptor) + 1) * SEG_GRANULARITY(descriptor) - 1;
171 if (limit < base_address)
174 if (SEG_EXPAND_DOWN(descriptor)) {
175 if (SEG_G_BIT(descriptor))
176 seg_top = 0xffffffff;
178 seg_top = base_address + (1 << 20);
179 if (seg_top < base_address)
180 seg_top = 0xffffffff;
183 (address <= limit) || (address >= seg_top) ? 0 :
184 ((seg_top - address) >= 255 ? 255 : seg_top - address);
187 (address > limit) || (address < base_address) ? 0 :
188 ((limit - address) >= 254 ? 255 : limit - address + 1);
190 if (SEG_EXECUTE_ONLY(descriptor) ||
191 (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT))) {
198 MOD R/M byte: MOD == 3 has a special use for the FPU
199 SIB byte used iff R/M = 100b
202 ..... ......... .........
208 ..... ......... .........
213 void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
214 struct address *addr, fpu_addr_modes addr_modes)
217 unsigned rm = FPU_modrm & 7;
219 int address = 0; /* Initialized just to stop compiler warnings. */
221 /* Memory accessed via the cs selector is write protected
222 in `non-segmented' 32 bit protected mode. */
223 if (!addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
224 && (addr_modes.override.segment == PREFIX_CS_)) {
225 math_abort(FPU_info, SIGSEGV);
228 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
230 mod = (FPU_modrm >> 6) & 3;
232 if (rm == 4 && mod != 3) {
233 address = sib(mod, fpu_eip);
235 cpu_reg_ptr = ®_(rm);
239 /* Special case: disp32 */
240 RE_ENTRANT_CHECK_OFF;
241 FPU_code_access_ok(4);
242 FPU_get_user(address,
243 (unsigned long __user
247 addr->offset = address;
248 return (void __user *)address;
250 address = *cpu_reg_ptr; /* Just return the contents
251 of the cpu register */
252 addr->offset = address;
253 return (void __user *)address;
256 /* 8 bit signed displacement */
257 RE_ENTRANT_CHECK_OFF;
258 FPU_code_access_ok(1);
259 FPU_get_user(address, (signed char __user *)(*fpu_eip));
264 /* 32 bit displacement */
265 RE_ENTRANT_CHECK_OFF;
266 FPU_code_access_ok(4);
267 FPU_get_user(address, (long __user *)(*fpu_eip));
272 /* Not legal for the FPU */
273 EXCEPTION(EX_Invalid);
275 address += *cpu_reg_ptr;
278 addr->offset = address;
280 switch (addr_modes.default_mode) {
284 address += vm86_segment(addr_modes.override.segment, addr);
288 address = pm_address(FPU_modrm, addr_modes.override.segment,
292 EXCEPTION(EX_INTERNAL | 0x133);
295 return (void __user *)address;
298 void __user *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
299 struct address *addr, fpu_addr_modes addr_modes)
302 unsigned rm = FPU_modrm & 7;
303 int address = 0; /* Default used for mod == 0 */
305 /* Memory accessed via the cs selector is write protected
306 in `non-segmented' 32 bit protected mode. */
307 if (!addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
308 && (addr_modes.override.segment == PREFIX_CS_)) {
309 math_abort(FPU_info, SIGSEGV);
312 addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
314 mod = (FPU_modrm >> 6) & 3;
319 /* Special case: disp16 */
320 RE_ENTRANT_CHECK_OFF;
321 FPU_code_access_ok(2);
322 FPU_get_user(address,
323 (unsigned short __user *)(*fpu_eip));
330 /* 8 bit signed displacement */
331 RE_ENTRANT_CHECK_OFF;
332 FPU_code_access_ok(1);
333 FPU_get_user(address, (signed char __user *)(*fpu_eip));
338 /* 16 bit displacement */
339 RE_ENTRANT_CHECK_OFF;
340 FPU_code_access_ok(2);
341 FPU_get_user(address, (unsigned short __user *)(*fpu_eip));
346 /* Not legal for the FPU */
347 EXCEPTION(EX_Invalid);
352 address += FPU_info->___ebx + FPU_info->___esi;
355 address += FPU_info->___ebx + FPU_info->___edi;
358 address += FPU_info->___ebp + FPU_info->___esi;
359 if (addr_modes.override.segment == PREFIX_DEFAULT)
360 addr_modes.override.segment = PREFIX_SS_;
363 address += FPU_info->___ebp + FPU_info->___edi;
364 if (addr_modes.override.segment == PREFIX_DEFAULT)
365 addr_modes.override.segment = PREFIX_SS_;
368 address += FPU_info->___esi;
371 address += FPU_info->___edi;
374 address += FPU_info->___ebp;
375 if (addr_modes.override.segment == PREFIX_DEFAULT)
376 addr_modes.override.segment = PREFIX_SS_;
379 address += FPU_info->___ebx;
386 addr->offset = address;
388 switch (addr_modes.default_mode) {
392 address += vm86_segment(addr_modes.override.segment, addr);
396 address = pm_address(FPU_modrm, addr_modes.override.segment,
400 EXCEPTION(EX_INTERNAL | 0x131);
403 return (void __user *)address;