src/emu/cpu/i386/i386.c
| r14111 | r14112 | |
| 441 | 441 | if((selector & ~0x0007) > cpustate->ldtr.limit) |
| 442 | 442 | { |
| 443 | 443 | logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc); |
| 444 | | FAULT(FAULT_GP,selector) |
| 444 | FAULT(FAULT_GP,selector & ~0x03) |
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | 447 | else // GDT |
| r14111 | r14112 | |
| 449 | 449 | if((selector & ~0x0007) > cpustate->gdtr.limit) |
| 450 | 450 | { |
| 451 | 451 | logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc); |
| 452 | | FAULT(FAULT_GP,selector) |
| 452 | FAULT(FAULT_GP,selector & ~0x03) |
| 453 | 453 | } |
| 454 | 454 | } |
| 455 | 455 | if (RPL != CPL) |
| 456 | 456 | { |
| 457 | 457 | logerror("SReg Load (%08x): Selector RPL does not equal CPL.\n",cpustate->pc); |
| 458 | | FAULT(FAULT_GP,selector) |
| 458 | FAULT(FAULT_GP,selector & ~0x03) |
| 459 | 459 | } |
| 460 | 460 | if(((stack.flags & 0x0018) != 0x10) && (stack.flags & 0x0002) != 0) |
| 461 | 461 | { |
| 462 | 462 | logerror("SReg Load (%08x): Segment is not a writable data segment.\n",cpustate->pc); |
| 463 | | FAULT(FAULT_GP,selector) |
| 463 | FAULT(FAULT_GP,selector & ~0x03) |
| 464 | 464 | } |
| 465 | 465 | if(DPL != CPL) |
| 466 | 466 | { |
| 467 | 467 | logerror("SReg Load (%08x): Segment DPL does not equal CPL.\n",cpustate->pc); |
| 468 | | FAULT(FAULT_GP,selector) |
| 468 | FAULT(FAULT_GP,selector & ~0x03) |
| 469 | 469 | } |
| 470 | 470 | if(!(stack.flags & 0x0080)) |
| 471 | 471 | { |
| 472 | 472 | logerror("SReg Load (%08x): Segment is not present.\n",cpustate->pc); |
| 473 | | FAULT(FAULT_SS,selector) |
| 473 | FAULT(FAULT_SS,selector & ~0x03) |
| 474 | 474 | } |
| 475 | 475 | } |
| 476 | 476 | if(reg == DS || reg == ES || reg == FS || reg == GS) |
| r14111 | r14112 | |
| 495 | 495 | if((selector & ~0x0007) > cpustate->ldtr.limit) |
| 496 | 496 | { |
| 497 | 497 | logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc); |
| 498 | | FAULT(FAULT_GP,selector) |
| 498 | FAULT(FAULT_GP,selector & ~0x03) |
| 499 | 499 | } |
| 500 | 500 | } |
| 501 | 501 | else // GDT |
| r14111 | r14112 | |
| 503 | 503 | if((selector & ~0x0007) > cpustate->gdtr.limit) |
| 504 | 504 | { |
| 505 | 505 | logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc); |
| 506 | | FAULT(FAULT_GP,selector) |
| 506 | FAULT(FAULT_GP,selector & ~0x03) |
| 507 | 507 | } |
| 508 | 508 | } |
| 509 | 509 | if((desc.flags & 0x0018) != 0x10) |
| r14111 | r14112 | |
| 511 | 511 | if(((desc.flags & 0x0002) != 0) && ((desc.flags & 0x0018) != 0x18)) |
| 512 | 512 | { |
| 513 | 513 | logerror("SReg Load (%08x): Segment is not a data segment or readable code segment.\n",cpustate->pc); |
| 514 | | FAULT(FAULT_GP,selector) |
| 514 | FAULT(FAULT_GP,selector & ~0x03) |
| 515 | 515 | } |
| 516 | 516 | } |
| 517 | 517 | if(((desc.flags & 0x0018) == 0x10) || ((!(desc.flags & 0x0004)) && ((desc.flags & 0x0018) == 0x18))) |
| r14111 | r14112 | |
| 520 | 520 | if((RPL > DPL) || (CPL > DPL)) |
| 521 | 521 | { |
| 522 | 522 | logerror("SReg Load (%08x): Selector RPL or CPL is not less or equal to segment DPL.\n",cpustate->pc); |
| 523 | | FAULT(FAULT_GP,selector) |
| 523 | FAULT(FAULT_GP,selector & ~0x03) |
| 524 | 524 | } |
| 525 | 525 | } |
| 526 | 526 | if(!(desc.flags & 0x0080)) |
| 527 | 527 | { |
| 528 | 528 | logerror("SReg Load (%08x): Segment is not present.\n",cpustate->pc); |
| 529 | | FAULT(FAULT_NP,selector) |
| 529 | FAULT(FAULT_NP,selector & ~0x03) |
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
| r14111 | r14112 | |
| 1484 | 1484 | if((selector & ~0x07) > cpustate->ldtr.limit) |
| 1485 | 1485 | { |
| 1486 | 1486 | logerror("CALL: Selector is past LDT limit.\n"); |
| 1487 | | FAULT(FAULT_GP,selector & ~0x07) // #GP(selector) |
| 1487 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| 1488 | 1488 | } |
| 1489 | 1489 | } |
| 1490 | 1490 | else |
| r14111 | r14112 | |
| 1492 | 1492 | if((selector & ~0x07) > cpustate->gdtr.limit) |
| 1493 | 1493 | { |
| 1494 | 1494 | logerror("CALL: Selector is past GDT limit.\n"); |
| 1495 | | FAULT(FAULT_GP,selector & ~0x07) // #GP(selector) |
| 1495 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| 1496 | 1496 | } |
| 1497 | 1497 | } |
| 1498 | 1498 | |
| r14111 | r14112 | |
| 1511 | 1511 | if(DPL > CPL) |
| 1512 | 1512 | { |
| 1513 | 1513 | logerror("CALL: Code segment DPL %i is greater than CPL %i\n",DPL,CPL); |
| 1514 | | FAULT(FAULT_GP,selector & ~0x07) // #GP(selector) |
| 1514 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| 1515 | 1515 | } |
| 1516 | 1516 | } |
| 1517 | 1517 | else |
| r14111 | r14112 | |
| 1520 | 1520 | if(RPL > CPL) |
| 1521 | 1521 | { |
| 1522 | 1522 | logerror("CALL: RPL %i is greater than CPL %i\n",RPL,CPL); |
| 1523 | | FAULT(FAULT_GP,selector & ~0x07) // #GP(selector) |
| 1523 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| 1524 | 1524 | } |
| 1525 | 1525 | if(DPL != CPL) |
| 1526 | 1526 | { |
| 1527 | 1527 | logerror("CALL: Code segment DPL %i is not equal to CPL %i\n",DPL,CPL); |
| 1528 | | FAULT(FAULT_GP,selector & ~0x07) // #GP(selector) |
| 1528 | FAULT(FAULT_GP,selector & ~0x03) // #GP(selector) |
| 1529 | 1529 | } |
| 1530 | 1530 | SetRPL = 1; |
| 1531 | 1531 | } |
| 1532 | 1532 | if((desc.flags & 0x0080) == 0) |
| 1533 | 1533 | { |
| 1534 | 1534 | logerror("CALL (%08x): Code segment is not present.\n",cpustate->pc); |
| 1535 | | FAULT(FAULT_NP,selector & ~0x07) // #NP(selector) |
| 1535 | FAULT(FAULT_NP,selector & ~0x03) // #NP(selector) |
| 1536 | 1536 | } |
| 1537 | 1537 | if (operand32 != 0) // if 32-bit |
| 1538 | 1538 | { |
| r14111 | r14112 | |
| 1562 | 1562 | if(desc.flags & 0x0010) |
| 1563 | 1563 | { |
| 1564 | 1564 | logerror("CALL: Segment is a data segment.\n"); |
| 1565 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1565 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1566 | 1566 | } |
| 1567 | 1567 | else |
| 1568 | 1568 | { |
| r14111 | r14112 | |
| 1574 | 1574 | if(DPL < CPL) |
| 1575 | 1575 | { |
| 1576 | 1576 | logerror("CALL: TSS: DPL is less than CPL.\n"); |
| 1577 | | FAULT(FAULT_TS,selector & ~0x07) // #TS(selector) |
| 1577 | FAULT(FAULT_TS,selector & ~0x03) // #TS(selector) |
| 1578 | 1578 | } |
| 1579 | 1579 | if(DPL < RPL) |
| 1580 | 1580 | { |
| 1581 | 1581 | logerror("CALL: TSS: DPL is less than RPL.\n"); |
| 1582 | | FAULT(FAULT_TS,selector & ~0x07) // #TS(selector) |
| 1582 | FAULT(FAULT_TS,selector & ~0x03) // #TS(selector) |
| 1583 | 1583 | } |
| 1584 | 1584 | if(desc.flags & 0x0002) |
| 1585 | 1585 | { |
| 1586 | 1586 | logerror("CALL: TSS: TSS is busy.\n"); |
| 1587 | | FAULT(FAULT_TS,selector & ~0x07) // #TS(selector) |
| 1587 | FAULT(FAULT_TS,selector & ~0x03) // #TS(selector) |
| 1588 | 1588 | } |
| 1589 | 1589 | if(desc.flags & 0x0080) |
| 1590 | 1590 | { |
| 1591 | 1591 | logerror("CALL: TSS: Segment is not present.\n"); |
| 1592 | | FAULT(FAULT_NP,selector & ~0x07) // #NP(selector) |
| 1592 | FAULT(FAULT_NP,selector & ~0x03) // #NP(selector) |
| 1593 | 1593 | } |
| 1594 | 1594 | if(desc.flags & 0x08) |
| 1595 | 1595 | i386_task_switch(cpustate,desc.selector,1); |
| r14111 | r14112 | |
| 1611 | 1611 | if(DPL < CPL) |
| 1612 | 1612 | { |
| 1613 | 1613 | logerror("CALL: Call gate DPL %i is less than CPL %i.\n",DPL,CPL); |
| 1614 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1614 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1615 | 1615 | } |
| 1616 | 1616 | if(DPL < RPL) |
| 1617 | 1617 | { |
| 1618 | 1618 | logerror("CALL: Call gate DPL %i is less than RPL %i.\n",DPL,RPL); |
| 1619 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1619 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1620 | 1620 | } |
| 1621 | 1621 | if(gate.present == 0) |
| 1622 | 1622 | { |
| 1623 | 1623 | logerror("CALL: Call gate is not present.\n"); |
| 1624 | | FAULT(FAULT_NP,desc.selector & ~0x07) // #GP(selector) |
| 1624 | FAULT(FAULT_NP,desc.selector & ~0x03) // #GP(selector) |
| 1625 | 1625 | } |
| 1626 | 1626 | desc.selector = gate.selector; |
| 1627 | 1627 | if((gate.selector & ~0x07) == 0) |
| r14111 | r14112 | |
| 1634 | 1634 | if((desc.selector & ~0x07) > cpustate->ldtr.limit) |
| 1635 | 1635 | { |
| 1636 | 1636 | logerror("CALL: Call gate: Segment is past LDT limit\n"); |
| 1637 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1637 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1638 | 1638 | } |
| 1639 | 1639 | } |
| 1640 | 1640 | else |
| r14111 | r14112 | |
| 1642 | 1642 | if((desc.selector & ~0x07) > cpustate->gdtr.limit) |
| 1643 | 1643 | { |
| 1644 | 1644 | logerror("CALL: Call gate: Segment is past GDT limit\n"); |
| 1645 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1645 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1646 | 1646 | } |
| 1647 | 1647 | } |
| 1648 | 1648 | i386_load_protected_mode_segment(cpustate,&desc); |
| 1649 | 1649 | if((desc.flags & 0x0018) != 0x18) |
| 1650 | 1650 | { |
| 1651 | 1651 | logerror("CALL: Call gate: Segment is not a code segment.\n"); |
| 1652 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1652 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1653 | 1653 | } |
| 1654 | 1654 | DPL = ((desc.flags >> 5) & 0x03); |
| 1655 | 1655 | if(DPL > CPL) |
| 1656 | 1656 | { |
| 1657 | 1657 | logerror("CALL: Call gate: Segment DPL %i is greater than CPL %i.\n",DPL,CPL); |
| 1658 | | FAULT(FAULT_GP,desc.selector & ~0x07) // #GP(selector) |
| 1658 | FAULT(FAULT_GP,desc.selector & ~0x03) // #GP(selector) |
| 1659 | 1659 | } |
| 1660 | 1660 | if(DPL < CPL && (desc.flags & 0x0004) == 0) |
| 1661 | 1661 | { |
| r14111 | r14112 | |
| 1936 | 1936 | if(RPL < CPL) |
| 1937 | 1937 | { |
| 1938 | 1938 | logerror("RETF (%08x): Return segment RPL is less than CPL.\n",cpustate->pc); |
| 1939 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1939 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1940 | 1940 | } |
| 1941 | 1941 | |
| 1942 | 1942 | if(RPL == CPL) |
| r14111 | r14112 | |
| 1952 | 1952 | if((newCS & ~0x07) > cpustate->ldtr.limit) |
| 1953 | 1953 | { |
| 1954 | 1954 | logerror("RETF: Return segment is past LDT limit.\n"); |
| 1955 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1955 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1956 | 1956 | } |
| 1957 | 1957 | } |
| 1958 | 1958 | else |
| r14111 | r14112 | |
| 1960 | 1960 | if((newCS & ~0x07) > cpustate->gdtr.limit) |
| 1961 | 1961 | { |
| 1962 | 1962 | logerror("RETF: Return segment is past GDT limit.\n"); |
| 1963 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1963 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1964 | 1964 | } |
| 1965 | 1965 | } |
| 1966 | 1966 | if((desc.flags & 0x0018) != 0x0018) |
| 1967 | 1967 | { |
| 1968 | 1968 | logerror("RETF: Return segment is not a code segment.\n"); |
| 1969 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1969 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1970 | 1970 | } |
| 1971 | 1971 | if(desc.flags & 0x0004) |
| 1972 | 1972 | { |
| 1973 | 1973 | if(DPL > CPL) |
| 1974 | 1974 | { |
| 1975 | 1975 | logerror("RETF: Conforming code segment DPL is greater than CPL.\n"); |
| 1976 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1976 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1977 | 1977 | } |
| 1978 | 1978 | } |
| 1979 | 1979 | else |
| r14111 | r14112 | |
| 1981 | 1981 | if(DPL != CPL) |
| 1982 | 1982 | { |
| 1983 | 1983 | logerror("RETF: Non-conforming code segment DPL does not equal CPL.\n"); |
| 1984 | | FAULT(FAULT_GP,newCS & ~0x07) |
| 1984 | FAULT(FAULT_GP,newCS & ~0x03) |
| 1985 | 1985 | } |
| 1986 | 1986 | } |
| 1987 | 1987 | if((desc.flags & 0x0080) == 0) |
| 1988 | 1988 | { |
| 1989 | | logerror("RETF: Code segment is not present.\n"); |
| 1990 | | FAULT(FAULT_NP,newCS & ~0x07) |
| 1989 | logerror("RETF (%08x): Code segment is not present.\n",cpustate->pc); |
| 1990 | FAULT(FAULT_NP,newCS & ~0x03) |
| 1991 | 1991 | } |
| 1992 | 1992 | if(newEIP > desc.limit) |
| 1993 | 1993 | { |
| r14111 | r14112 | |
| 1996 | 1996 | } |
| 1997 | 1997 | if(operand32 == 0) |
| 1998 | 1998 | { |
| 1999 | | if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff)) |
| 1999 | if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff)+1) |
| 2000 | 2000 | { |
| 2001 | 2001 | logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc); |
| 2002 | 2002 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2004 | 2004 | } |
| 2005 | 2005 | else |
| 2006 | 2006 | { |
| 2007 | | if(REG32(ESP) > cpustate->sreg[SS].limit) |
| 2007 | if(REG32(ESP) > cpustate->sreg[SS].limit+1) |
| 2008 | 2008 | { |
| 2009 | 2009 | logerror("RETF: ESP is past stack segment limit.\n"); |
| 2010 | 2010 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2020 | 2020 | /* outer privilege level */ |
| 2021 | 2021 | if(operand32 == 0) |
| 2022 | 2022 | { |
| 2023 | | if(REG16(SP)+8+count > cpustate->sreg[SS].limit) |
| 2023 | if(REG16(SP)+8+count > cpustate->sreg[SS].limit+1) |
| 2024 | 2024 | { |
| 2025 | 2025 | logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc); |
| 2026 | 2026 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2028 | 2028 | } |
| 2029 | 2029 | else |
| 2030 | 2030 | { |
| 2031 | | if(REG32(ESP)+16+count > cpustate->sreg[SS].limit) |
| 2031 | if(REG32(ESP)+16+count > cpustate->sreg[SS].limit+1) |
| 2032 | 2032 | { |
| 2033 | 2033 | logerror("RETF: ESP is past stack segment limit.\n"); |
| 2034 | 2034 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2408 | 2408 | { |
| 2409 | 2409 | if(operand32 == 0) |
| 2410 | 2410 | { |
| 2411 | | if(REG16(SP)+4 > cpustate->sreg[SS].limit) |
| 2411 | if(REG16(SP)+4 > cpustate->sreg[SS].limit+1) |
| 2412 | 2412 | { |
| 2413 | 2413 | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2414 | 2414 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2416 | 2416 | } |
| 2417 | 2417 | else |
| 2418 | 2418 | { |
| 2419 | | if(REG32(ESP)+6 > cpustate->sreg[SS].limit) |
| 2419 | if(REG32(ESP)+6 > cpustate->sreg[SS].limit+1) |
| 2420 | 2420 | { |
| 2421 | 2421 | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2422 | 2422 | FAULT(FAULT_SS,0) |
| r14111 | r14112 | |
| 2433 | 2433 | /* return to same privilege level */ |
| 2434 | 2434 | if(operand32 == 0) |
| 2435 | 2435 | { |
| 2436 | | if(REG16(SP)+6 > cpustate->sreg[SS].limit) |
| 2436 | if(REG16(SP)+6 > cpustate->sreg[SS].limit+1) |
| 2437 | 2437 | { |
| 2438 | | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2438 | logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc); |
| 2439 | 2439 | FAULT(FAULT_SS,0) |
| 2440 | 2440 | } |
| 2441 | 2441 | } |
| 2442 | 2442 | else |
| 2443 | 2443 | { |
| 2444 | | if(REG32(ESP)+12 > cpustate->sreg[SS].limit) |
| 2444 | if(REG32(ESP)+12 > cpustate->sreg[SS].limit+1) |
| 2445 | 2445 | { |
| 2446 | | logerror("IRET: Data on stack is past SS limit.\n"); |
| 2446 | logerror("IRET (%08x): Data on stack is past SS limit.\n",cpustate->pc); |
| 2447 | 2447 | FAULT(FAULT_SS,0) |
| 2448 | 2448 | } |
| 2449 | 2449 | } |
| r14111 | r14112 | |
| 2533 | 2533 | i386_load_protected_mode_segment(cpustate,&stack); |
| 2534 | 2534 | if(operand32 == 0) |
| 2535 | 2535 | { |
| 2536 | | if(REG16(SP)+10 > cpustate->sreg[SS].limit) |
| 2536 | if(REG16(SP)+10 > cpustate->sreg[SS].limit+1) |
| 2537 | 2537 | { |
| 2538 | 2538 | logerror("IRET: SP is past SS limit.\n"); |
| 2539 | 2539 | FAULT(FAULT_SS,0) } |
| 2540 | 2540 | } |
| 2541 | 2541 | else |
| 2542 | 2542 | { |
| 2543 | | if(REG32(ESP)+20 > cpustate->sreg[SS].limit) |
| 2543 | if(REG32(ESP)+20 > cpustate->sreg[SS].limit+1) |
| 2544 | 2544 | { |
| 2545 | 2545 | logerror("IRET: ESP is past SS limit.\n"); |
| 2546 | 2546 | FAULT(FAULT_SS,0) |