Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
| 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_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; |
| 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_access_ok(1); |
| 115 | FPU_get_user(displacement, (signed char __user *) (*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_access_ok(4); |
| 126 | FPU_get_user(displacement, (long __user *) (*fpu_eip)); |
| 127 | offset += displacement; |
| 128 | RE_ENTRANT_CHECK_ON; |
| 129 | (*fpu_eip) += 4; |
| 130 | } |
| 131 | |
| 132 | return offset; |
| 133 | } |
| 134 | |
| 135 | |
| 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. */ |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | segment--; |
| 160 | |
| 161 | #ifdef PARANOID |
| 162 | /* segment is unsigned, so this also detects if segment was 0: */ |
| 163 | if ( segment > PREFIX_SS_ ) |
| 164 | { |
| 165 | EXCEPTION(EX_INTERNAL|0x132); |
| 166 | math_abort(FPU_info,SIGSEGV); |
| 167 | } |
| 168 | #endif /* PARANOID */ |
| 169 | |
| 170 | switch ( segment ) |
| 171 | { |
| 172 | /* fs and gs aren't used by the kernel, so they still have their |
| 173 | user-space values. */ |
| 174 | case PREFIX_FS_-1: |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 175 | /* N.B. - movl %seg, mem is a 2 byte write regardless of prefix */ |
| 176 | savesegment(fs, addr->selector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | break; |
| 178 | case PREFIX_GS_-1: |
Zachary Amsden | 4d37e7e | 2005-09-03 15:56:38 -0700 | [diff] [blame] | 179 | savesegment(gs, addr->selector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | break; |
| 181 | default: |
| 182 | addr->selector = PM_REG_(segment); |
| 183 | } |
| 184 | |
| 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; |
| 191 | |
| 192 | if ( SEG_EXPAND_DOWN(descriptor) ) |
| 193 | { |
| 194 | if ( SEG_G_BIT(descriptor) ) |
| 195 | seg_top = 0xffffffff; |
| 196 | else |
| 197 | { |
| 198 | seg_top = base_address + (1 << 20); |
| 199 | if ( seg_top < base_address ) seg_top = 0xffffffff; |
| 200 | } |
| 201 | access_limit = |
| 202 | (address <= limit) || (address >= seg_top) ? 0 : |
| 203 | ((seg_top-address) >= 255 ? 255 : seg_top-address); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | access_limit = |
| 208 | (address > limit) || (address < base_address) ? 0 : |
| 209 | ((limit-address) >= 254 ? 255 : limit-address+1); |
| 210 | } |
| 211 | if ( SEG_EXECUTE_ONLY(descriptor) || |
| 212 | (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT)) ) |
| 213 | { |
| 214 | access_limit = 0; |
| 215 | } |
| 216 | return address; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | MOD R/M byte: MOD == 3 has a special use for the FPU |
| 222 | SIB byte used iff R/M = 100b |
| 223 | |
| 224 | 7 6 5 4 3 2 1 0 |
| 225 | ..... ......... ......... |
| 226 | MOD OPCODE(2) R/M |
| 227 | |
| 228 | |
| 229 | SIB byte |
| 230 | |
| 231 | 7 6 5 4 3 2 1 0 |
| 232 | ..... ......... ......... |
| 233 | SS INDEX BASE |
| 234 | |
| 235 | */ |
| 236 | |
| 237 | void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip, |
| 238 | struct address *addr, |
| 239 | fpu_addr_modes addr_modes) |
| 240 | { |
| 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. */ |
| 245 | |
| 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_) ) |
| 250 | { |
| 251 | math_abort(FPU_info,SIGSEGV); |
| 252 | } |
| 253 | |
| 254 | addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */ |
| 255 | |
| 256 | mod = (FPU_modrm >> 6) & 3; |
| 257 | |
| 258 | if (rm == 4 && mod != 3) |
| 259 | { |
| 260 | address = sib(mod, fpu_eip); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | cpu_reg_ptr = & REG_(rm); |
| 265 | switch (mod) |
| 266 | { |
| 267 | case 0: |
| 268 | if (rm == 5) |
| 269 | { |
| 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; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | address = *cpu_reg_ptr; /* Just return the contents |
| 282 | of the cpu register */ |
| 283 | addr->offset = address; |
| 284 | return (void __user *) address; |
| 285 | } |
| 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); |
| 305 | } |
| 306 | address += *cpu_reg_ptr; |
| 307 | } |
| 308 | |
| 309 | addr->offset = address; |
| 310 | |
| 311 | switch ( addr_modes.default_mode ) |
| 312 | { |
| 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); |
| 325 | } |
| 326 | |
| 327 | return (void __user *)address; |
| 328 | } |
| 329 | |
| 330 | |
| 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) |
| 334 | { |
| 335 | u_char mod; |
| 336 | unsigned rm = FPU_modrm & 7; |
| 337 | int address = 0; /* Default used for mod == 0 */ |
| 338 | |
| 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_) ) |
| 343 | { |
| 344 | math_abort(FPU_info,SIGSEGV); |
| 345 | } |
| 346 | |
| 347 | addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */ |
| 348 | |
| 349 | mod = (FPU_modrm >> 6) & 3; |
| 350 | |
| 351 | switch (mod) |
| 352 | { |
| 353 | case 0: |
| 354 | if (rm == 6) |
| 355 | { |
| 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; |
| 363 | } |
| 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; |
| 385 | } |
| 386 | switch ( rm ) |
| 387 | { |
| 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; |
| 418 | } |
| 419 | |
| 420 | add_segment: |
| 421 | address &= 0xffff; |
| 422 | |
| 423 | addr->offset = address; |
| 424 | |
| 425 | switch ( addr_modes.default_mode ) |
| 426 | { |
| 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); |
| 439 | } |
| 440 | |
| 441 | return (void __user *)address ; |
| 442 | } |