1 // SPDX-License-Identifier: GPL-2.0
2 /*---------------------------------------------------------------------------+
5 | Get the effective address from an FPU instruction. |
7 | Copyright (C) 1992,1993,1994,1997 |
8 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
9 | Australia. E-mail billm@suburbia.net |
12 +---------------------------------------------------------------------------*/
14 /*---------------------------------------------------------------------------+
16 | The file contains code which accesses user memory. |
17 | Emulator static data may change when user memory is accessed, due to |
18 | other processes using the emulator while swapping is in progress. |
19 +---------------------------------------------------------------------------*/
21 #include <linux/stddef.h>
23 #include <linux/uaccess.h>
26 #include "fpu_system.h"
27 #include "exception.h"
30 #define FPU_WRITE_BIT 0x10
32 static int reg_offset
[] = {
33 offsetof(struct pt_regs
, ax
),
34 offsetof(struct pt_regs
, cx
),
35 offsetof(struct pt_regs
, dx
),
36 offsetof(struct pt_regs
, bx
),
37 offsetof(struct pt_regs
, sp
),
38 offsetof(struct pt_regs
, bp
),
39 offsetof(struct pt_regs
, si
),
40 offsetof(struct pt_regs
, di
)
43 #define REG_(x) (*(long *)(reg_offset[(x)] + (u_char *)FPU_info->regs))
45 static int reg_offset_vm86
[] = {
46 offsetof(struct pt_regs
, cs
),
47 offsetof(struct kernel_vm86_regs
, ds
),
48 offsetof(struct kernel_vm86_regs
, es
),
49 offsetof(struct kernel_vm86_regs
, fs
),
50 offsetof(struct kernel_vm86_regs
, gs
),
51 offsetof(struct pt_regs
, ss
),
52 offsetof(struct kernel_vm86_regs
, ds
)
55 #define VM86_REG_(x) (*(unsigned short *) \
56 (reg_offset_vm86[((unsigned)x)] + (u_char *)FPU_info->regs))
58 static int reg_offset_pm
[] = {
59 offsetof(struct pt_regs
, cs
),
60 offsetof(struct pt_regs
, ds
),
61 offsetof(struct pt_regs
, es
),
62 offsetof(struct pt_regs
, fs
),
63 offsetof(struct pt_regs
, ds
), /* dummy, not saved on stack */
64 offsetof(struct pt_regs
, ss
),
65 offsetof(struct pt_regs
, ds
)
68 #define PM_REG_(x) (*(unsigned short *) \
69 (reg_offset_pm[((unsigned)x)] + (u_char *)FPU_info->regs))
71 /* Decode the SIB byte. This function assumes mod != 0 */
72 static int sib(int mod
, unsigned long *fpu_eip
)
74 u_char ss
, index
, base
;
78 FPU_code_access_ok(1);
79 FPU_get_user(base
, (u_char __user
*) (*fpu_eip
)); /* The SIB byte */
83 index
= (base
>> 3) & 7;
86 if ((mod
== 0) && (base
== 5))
87 offset
= 0; /* No base register */
92 /* No index register */
93 /* A non-zero ss is illegal */
95 EXCEPTION(EX_Invalid
);
97 offset
+= (REG_(index
)) << ss
;
101 /* 8 bit signed displacement */
103 RE_ENTRANT_CHECK_OFF
;
104 FPU_code_access_ok(1);
105 FPU_get_user(displacement
, (signed char __user
*)(*fpu_eip
));
106 offset
+= displacement
;
109 } else if (mod
== 2 || base
== 5) { /* The second condition also has mod==0 */
110 /* 32 bit displacement */
112 RE_ENTRANT_CHECK_OFF
;
113 FPU_code_access_ok(4);
114 FPU_get_user(displacement
, (long __user
*)(*fpu_eip
));
115 offset
+= displacement
;
123 static unsigned long vm86_segment(u_char segment
, struct address
*addr
)
127 if (segment
> PREFIX_SS_
) {
128 EXCEPTION(EX_INTERNAL
| 0x130);
129 math_abort(FPU_info
, SIGSEGV
);
131 #endif /* PARANOID */
132 addr
->selector
= VM86_REG_(segment
);
133 return (unsigned long)VM86_REG_(segment
) << 4;
136 /* This should work for 16 and 32 bit protected mode. */
137 static long pm_address(u_char FPU_modrm
, u_char segment
,
138 struct address
*addr
, long offset
)
140 struct desc_struct descriptor
;
141 unsigned long base_address
, limit
, address
, seg_top
;
146 /* segment is unsigned, so this also detects if segment was 0: */
147 if (segment
> PREFIX_SS_
) {
148 EXCEPTION(EX_INTERNAL
| 0x132);
149 math_abort(FPU_info
, SIGSEGV
);
151 #endif /* PARANOID */
155 /* user gs handling can be lazy, use special accessors */
156 addr
->selector
= get_user_gs(FPU_info
->regs
);
159 addr
->selector
= PM_REG_(segment
);
162 descriptor
= FPU_get_ldt_descriptor(addr
->selector
);
163 base_address
= seg_get_base(&descriptor
);
164 address
= base_address
+ offset
;
165 limit
= seg_get_limit(&descriptor
) + 1;
166 limit
*= seg_get_granularity(&descriptor
);
167 limit
+= base_address
- 1;
168 if (limit
< base_address
)
171 if (seg_expands_down(&descriptor
)) {
173 seg_top
= 0xffffffff;
175 seg_top
= base_address
+ (1 << 20);
176 if (seg_top
< base_address
)
177 seg_top
= 0xffffffff;
180 (address
<= limit
) || (address
>= seg_top
) ? 0 :
181 ((seg_top
- address
) >= 255 ? 255 : seg_top
- address
);
184 (address
> limit
) || (address
< base_address
) ? 0 :
185 ((limit
- address
) >= 254 ? 255 : limit
- address
+ 1);
187 if (seg_execute_only(&descriptor
) ||
188 (!seg_writable(&descriptor
) && (FPU_modrm
& FPU_WRITE_BIT
))) {
195 MOD R/M byte: MOD == 3 has a special use for the FPU
196 SIB byte used iff R/M = 100b
199 ..... ......... .........
205 ..... ......... .........
210 void __user
*FPU_get_address(u_char FPU_modrm
, unsigned long *fpu_eip
,
211 struct address
*addr
, fpu_addr_modes addr_modes
)
214 unsigned rm
= FPU_modrm
& 7;
216 int address
= 0; /* Initialized just to stop compiler warnings. */
218 /* Memory accessed via the cs selector is write protected
219 in `non-segmented' 32 bit protected mode. */
220 if (!addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
221 && (addr_modes
.override
.segment
== PREFIX_CS_
)) {
222 math_abort(FPU_info
, SIGSEGV
);
225 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
227 mod
= (FPU_modrm
>> 6) & 3;
229 if (rm
== 4 && mod
!= 3) {
230 address
= sib(mod
, fpu_eip
);
232 cpu_reg_ptr
= ®_(rm
);
236 /* Special case: disp32 */
237 RE_ENTRANT_CHECK_OFF
;
238 FPU_code_access_ok(4);
239 FPU_get_user(address
,
240 (unsigned long __user
244 addr
->offset
= address
;
245 return (void __user
*)address
;
247 address
= *cpu_reg_ptr
; /* Just return the contents
248 of the cpu register */
249 addr
->offset
= address
;
250 return (void __user
*)address
;
253 /* 8 bit signed displacement */
254 RE_ENTRANT_CHECK_OFF
;
255 FPU_code_access_ok(1);
256 FPU_get_user(address
, (signed char __user
*)(*fpu_eip
));
261 /* 32 bit displacement */
262 RE_ENTRANT_CHECK_OFF
;
263 FPU_code_access_ok(4);
264 FPU_get_user(address
, (long __user
*)(*fpu_eip
));
269 /* Not legal for the FPU */
270 EXCEPTION(EX_Invalid
);
272 address
+= *cpu_reg_ptr
;
275 addr
->offset
= address
;
277 switch (addr_modes
.default_mode
) {
281 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
285 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
289 EXCEPTION(EX_INTERNAL
| 0x133);
292 return (void __user
*)address
;
295 void __user
*FPU_get_address_16(u_char FPU_modrm
, unsigned long *fpu_eip
,
296 struct address
*addr
, fpu_addr_modes addr_modes
)
299 unsigned rm
= FPU_modrm
& 7;
300 int address
= 0; /* Default used for mod == 0 */
302 /* Memory accessed via the cs selector is write protected
303 in `non-segmented' 32 bit protected mode. */
304 if (!addr_modes
.default_mode
&& (FPU_modrm
& FPU_WRITE_BIT
)
305 && (addr_modes
.override
.segment
== PREFIX_CS_
)) {
306 math_abort(FPU_info
, SIGSEGV
);
309 addr
->selector
= FPU_DS
; /* Default, for 32 bit non-segmented mode. */
311 mod
= (FPU_modrm
>> 6) & 3;
316 /* Special case: disp16 */
317 RE_ENTRANT_CHECK_OFF
;
318 FPU_code_access_ok(2);
319 FPU_get_user(address
,
320 (unsigned short __user
*)(*fpu_eip
));
327 /* 8 bit signed displacement */
328 RE_ENTRANT_CHECK_OFF
;
329 FPU_code_access_ok(1);
330 FPU_get_user(address
, (signed char __user
*)(*fpu_eip
));
335 /* 16 bit displacement */
336 RE_ENTRANT_CHECK_OFF
;
337 FPU_code_access_ok(2);
338 FPU_get_user(address
, (unsigned short __user
*)(*fpu_eip
));
343 /* Not legal for the FPU */
344 EXCEPTION(EX_Invalid
);
349 address
+= FPU_info
->regs
->bx
+ FPU_info
->regs
->si
;
352 address
+= FPU_info
->regs
->bx
+ FPU_info
->regs
->di
;
355 address
+= FPU_info
->regs
->bp
+ FPU_info
->regs
->si
;
356 if (addr_modes
.override
.segment
== PREFIX_DEFAULT
)
357 addr_modes
.override
.segment
= PREFIX_SS_
;
360 address
+= FPU_info
->regs
->bp
+ FPU_info
->regs
->di
;
361 if (addr_modes
.override
.segment
== PREFIX_DEFAULT
)
362 addr_modes
.override
.segment
= PREFIX_SS_
;
365 address
+= FPU_info
->regs
->si
;
368 address
+= FPU_info
->regs
->di
;
371 address
+= FPU_info
->regs
->bp
;
372 if (addr_modes
.override
.segment
== PREFIX_DEFAULT
)
373 addr_modes
.override
.segment
= PREFIX_SS_
;
376 address
+= FPU_info
->regs
->bx
;
383 addr
->offset
= address
;
385 switch (addr_modes
.default_mode
) {
389 address
+= vm86_segment(addr_modes
.override
.segment
, addr
);
393 address
= pm_address(FPU_modrm
, addr_modes
.override
.segment
,
397 EXCEPTION(EX_INTERNAL
| 0x131);
400 return (void __user
*)address
;