Previous 15994 Revisions Next

r14112 Saturday 21st January, 2012 at 09:08:47 UTC by Barry Rodewald
i386: Fixed stack segment limit checking for IRET and RETF instructions.
Added segment register checks for LDS/LES/LFS/LGS instructions.
Corrected exception error codes that returned a segment selector to
include the local/global bit. [Barry Rodewald]
Fix for JCXZ instruction. [Carl]
[src/emu/cpu/i386]i386.c i386op16.c i386op32.c

src/emu/cpu/i386/i386op16.c
r14111r14112
10721072static void I386OP(jcxz16)(i386_state *cpustate)         // Opcode 0xe3
10731073{
10741074   INT8 disp = FETCH(cpustate);
1075   if( REG16(CX) == 0 ) {
1075   int val = (cpustate->address_size)?(REG32(ECX) == 0):(REG16(CX) == 0);
1076   if( val ) {
10761077      if (cpustate->sreg[CS].d)
10771078      {
10781079         cpustate->eip += disp;
r14111r14112
34723473static void I386OP(load_far_pointer16)(i386_state *cpustate, int s)
34733474{
34743475   UINT8 modrm = FETCH(cpustate);
3476   UINT16 selector;
34753477
34763478   if( modrm >= 0xc0 ) {
34773479      fatalerror("i386: load_far_pointer16 NYI");
34783480   } else {
34793481      UINT32 ea = GetEA(cpustate,modrm);
34803482      STORE_REG16(modrm, READ16(cpustate,ea + 0));
3481      cpustate->sreg[s].selector = READ16(cpustate,ea + 2);
3482      i386_load_segment_descriptor(cpustate, s );
3483      selector = READ16(cpustate,ea + 2);
3484      if(PROTECTED_MODE && !(V8086_MODE))
3485         i386_protected_mode_sreg_load(cpustate,selector,s);
3486      else
3487      {
3488         cpustate->sreg[s].selector = selector;
3489         i386_load_segment_descriptor(cpustate, s );
3490      }
34833491   }
34843492}
34853493
src/emu/cpu/i386/i386.c
r14111r14112
441441         if((selector & ~0x0007) > cpustate->ldtr.limit)
442442         {
443443            logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc);
444            FAULT(FAULT_GP,selector)
444            FAULT(FAULT_GP,selector & ~0x03)
445445         }
446446      }
447447      else  // GDT
r14111r14112
449449         if((selector & ~0x0007) > cpustate->gdtr.limit)
450450         {
451451            logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc);
452            FAULT(FAULT_GP,selector)
452            FAULT(FAULT_GP,selector & ~0x03)
453453         }
454454      }
455455      if (RPL != CPL)
456456      {
457457         logerror("SReg Load (%08x): Selector RPL does not equal CPL.\n",cpustate->pc);
458         FAULT(FAULT_GP,selector)
458         FAULT(FAULT_GP,selector & ~0x03)
459459      }
460460      if(((stack.flags & 0x0018) != 0x10) && (stack.flags & 0x0002) != 0)
461461      {
462462         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)
464464      }
465465      if(DPL != CPL)
466466      {
467467         logerror("SReg Load (%08x): Segment DPL does not equal CPL.\n",cpustate->pc);
468         FAULT(FAULT_GP,selector)
468         FAULT(FAULT_GP,selector & ~0x03)
469469      }
470470      if(!(stack.flags & 0x0080))
471471      {
472472         logerror("SReg Load (%08x): Segment is not present.\n",cpustate->pc);
473         FAULT(FAULT_SS,selector)
473         FAULT(FAULT_SS,selector & ~0x03)
474474      }
475475   }
476476   if(reg == DS || reg == ES || reg == FS || reg == GS)
r14111r14112
495495         if((selector & ~0x0007) > cpustate->ldtr.limit)
496496         {
497497            logerror("SReg Load (%08x): Selector is out of LDT bounds.\n",cpustate->pc);
498            FAULT(FAULT_GP,selector)
498            FAULT(FAULT_GP,selector & ~0x03)
499499         }
500500      }
501501      else  // GDT
r14111r14112
503503         if((selector & ~0x0007) > cpustate->gdtr.limit)
504504         {
505505            logerror("SReg Load (%08x): Selector is out of GDT bounds.\n",cpustate->pc);
506            FAULT(FAULT_GP,selector)
506            FAULT(FAULT_GP,selector & ~0x03)
507507         }
508508      }
509509      if((desc.flags & 0x0018) != 0x10)
r14111r14112
511511         if(((desc.flags & 0x0002) != 0) && ((desc.flags & 0x0018) != 0x18))
512512         {
513513            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)
515515         }
516516      }
517517      if(((desc.flags & 0x0018) == 0x10) || ((!(desc.flags & 0x0004)) && ((desc.flags & 0x0018) == 0x18)))
r14111r14112
520520         if((RPL > DPL) || (CPL > DPL))
521521         {
522522            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)
524524         }
525525      }
526526      if(!(desc.flags & 0x0080))
527527      {
528528         logerror("SReg Load (%08x): Segment is not present.\n",cpustate->pc);
529         FAULT(FAULT_NP,selector)
529         FAULT(FAULT_NP,selector & ~0x03)
530530      }
531531   }
532532
r14111r14112
14841484      if((selector & ~0x07) > cpustate->ldtr.limit)
14851485      {
14861486         logerror("CALL: Selector is past LDT limit.\n");
1487         FAULT(FAULT_GP,selector & ~0x07)  // #GP(selector)
1487         FAULT(FAULT_GP,selector & ~0x03)  // #GP(selector)
14881488      }
14891489   }
14901490   else
r14111r14112
14921492      if((selector & ~0x07) > cpustate->gdtr.limit)
14931493      {
14941494         logerror("CALL: Selector is past GDT limit.\n");
1495         FAULT(FAULT_GP,selector & ~0x07)  // #GP(selector)
1495         FAULT(FAULT_GP,selector & ~0x03)  // #GP(selector)
14961496      }
14971497   }
14981498
r14111r14112
15111511         if(DPL > CPL)
15121512         {
15131513            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)
15151515         }
15161516      }
15171517      else
r14111r14112
15201520         if(RPL > CPL)
15211521         {
15221522            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)
15241524         }
15251525         if(DPL != CPL)
15261526         {
15271527            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)
15291529         }
15301530         SetRPL = 1;
15311531      }
15321532      if((desc.flags & 0x0080) == 0)
15331533      {
15341534         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)
15361536      }
15371537      if (operand32 != 0)  // if 32-bit
15381538      {
r14111r14112
15621562      if(desc.flags & 0x0010)
15631563      {
15641564         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)
15661566      }
15671567      else
15681568      {
r14111r14112
15741574            if(DPL < CPL)
15751575            {
15761576               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)
15781578            }
15791579            if(DPL < RPL)
15801580            {
15811581               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)
15831583            }
15841584            if(desc.flags & 0x0002)
15851585            {
15861586               logerror("CALL: TSS: TSS is busy.\n");
1587               FAULT(FAULT_TS,selector & ~0x07) // #TS(selector)
1587               FAULT(FAULT_TS,selector & ~0x03) // #TS(selector)
15881588            }
15891589            if(desc.flags & 0x0080)
15901590            {
15911591               logerror("CALL: TSS: Segment is not present.\n");
1592               FAULT(FAULT_NP,selector & ~0x07) // #NP(selector)
1592               FAULT(FAULT_NP,selector & ~0x03) // #NP(selector)
15931593            }
15941594            if(desc.flags & 0x08)
15951595               i386_task_switch(cpustate,desc.selector,1);
r14111r14112
16111611            if(DPL < CPL)
16121612            {
16131613               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)
16151615            }
16161616            if(DPL < RPL)
16171617            {
16181618               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)
16201620            }
16211621            if(gate.present == 0)
16221622            {
16231623               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)
16251625            }
16261626            desc.selector = gate.selector;
16271627            if((gate.selector & ~0x07) == 0)
r14111r14112
16341634               if((desc.selector & ~0x07) > cpustate->ldtr.limit)
16351635               {
16361636                  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)
16381638               }
16391639            }
16401640            else
r14111r14112
16421642               if((desc.selector & ~0x07) > cpustate->gdtr.limit)
16431643               {
16441644                  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)
16461646               }
16471647            }
16481648            i386_load_protected_mode_segment(cpustate,&desc);
16491649            if((desc.flags & 0x0018) != 0x18)
16501650            {
16511651               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)
16531653            }
16541654            DPL = ((desc.flags >> 5) & 0x03);
16551655            if(DPL > CPL)
16561656            {
16571657               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)
16591659            }
16601660            if(DPL < CPL && (desc.flags & 0x0004) == 0)
16611661            {
r14111r14112
19361936   if(RPL < CPL)
19371937   {
19381938      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)
19401940   }
19411941
19421942   if(RPL == CPL)
r14111r14112
19521952         if((newCS & ~0x07) > cpustate->ldtr.limit)
19531953         {
19541954            logerror("RETF: Return segment is past LDT limit.\n");
1955            FAULT(FAULT_GP,newCS & ~0x07)
1955            FAULT(FAULT_GP,newCS & ~0x03)
19561956         }
19571957      }
19581958      else
r14111r14112
19601960         if((newCS & ~0x07) > cpustate->gdtr.limit)
19611961         {
19621962            logerror("RETF: Return segment is past GDT limit.\n");
1963            FAULT(FAULT_GP,newCS & ~0x07)
1963            FAULT(FAULT_GP,newCS & ~0x03)
19641964         }
19651965      }
19661966      if((desc.flags & 0x0018) != 0x0018)
19671967      {
19681968         logerror("RETF: Return segment is not a code segment.\n");
1969         FAULT(FAULT_GP,newCS & ~0x07)
1969         FAULT(FAULT_GP,newCS & ~0x03)
19701970      }
19711971      if(desc.flags & 0x0004)
19721972      {
19731973         if(DPL > CPL)
19741974         {
19751975            logerror("RETF: Conforming code segment DPL is greater than CPL.\n");
1976            FAULT(FAULT_GP,newCS & ~0x07)
1976            FAULT(FAULT_GP,newCS & ~0x03)
19771977         }
19781978      }
19791979      else
r14111r14112
19811981         if(DPL != CPL)
19821982         {
19831983            logerror("RETF: Non-conforming code segment DPL does not equal CPL.\n");
1984            FAULT(FAULT_GP,newCS & ~0x07)
1984            FAULT(FAULT_GP,newCS & ~0x03)
19851985         }
19861986      }
19871987      if((desc.flags & 0x0080) == 0)
19881988      {
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)
19911991      }
19921992      if(newEIP > desc.limit)
19931993      {
r14111r14112
19961996      }
19971997      if(operand32 == 0)
19981998      {
1999         if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff))
1999         if(REG16(SP) > (cpustate->sreg[SS].limit & 0xffff)+1)
20002000         {
20012001            logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc);
20022002            FAULT(FAULT_SS,0)
r14111r14112
20042004      }
20052005      else
20062006      {
2007         if(REG32(ESP) > cpustate->sreg[SS].limit)
2007         if(REG32(ESP) > cpustate->sreg[SS].limit+1)
20082008         {
20092009            logerror("RETF: ESP is past stack segment limit.\n");
20102010            FAULT(FAULT_SS,0)
r14111r14112
20202020      /* outer privilege level */
20212021      if(operand32 == 0)
20222022      {
2023         if(REG16(SP)+8+count > cpustate->sreg[SS].limit)
2023         if(REG16(SP)+8+count > cpustate->sreg[SS].limit+1)
20242024         {
20252025            logerror("RETF (%08x): SP is past stack segment limit.\n",cpustate->pc);
20262026            FAULT(FAULT_SS,0)
r14111r14112
20282028      }
20292029      else
20302030      {
2031         if(REG32(ESP)+16+count > cpustate->sreg[SS].limit)
2031         if(REG32(ESP)+16+count > cpustate->sreg[SS].limit+1)
20322032         {
20332033            logerror("RETF: ESP is past stack segment limit.\n");
20342034            FAULT(FAULT_SS,0)
r14111r14112
24082408      {
24092409         if(operand32 == 0)
24102410         {
2411            if(REG16(SP)+4 > cpustate->sreg[SS].limit)
2411            if(REG16(SP)+4 > cpustate->sreg[SS].limit+1)
24122412            {
24132413               logerror("IRET: Data on stack is past SS limit.\n");
24142414               FAULT(FAULT_SS,0)
r14111r14112
24162416         }
24172417         else
24182418         {
2419            if(REG32(ESP)+6 > cpustate->sreg[SS].limit)
2419            if(REG32(ESP)+6 > cpustate->sreg[SS].limit+1)
24202420            {
24212421               logerror("IRET: Data on stack is past SS limit.\n");
24222422               FAULT(FAULT_SS,0)
r14111r14112
24332433            /* return to same privilege level */
24342434            if(operand32 == 0)
24352435            {
2436               if(REG16(SP)+6 > cpustate->sreg[SS].limit)
2436               if(REG16(SP)+6 > cpustate->sreg[SS].limit+1)
24372437               {
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);
24392439                  FAULT(FAULT_SS,0)
24402440               }
24412441            }
24422442            else
24432443            {
2444               if(REG32(ESP)+12 > cpustate->sreg[SS].limit)
2444               if(REG32(ESP)+12 > cpustate->sreg[SS].limit+1)
24452445               {
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);
24472447                  FAULT(FAULT_SS,0)
24482448               }
24492449            }
r14111r14112
25332533            i386_load_protected_mode_segment(cpustate,&stack);
25342534            if(operand32 == 0)
25352535            {
2536               if(REG16(SP)+10 > cpustate->sreg[SS].limit)
2536               if(REG16(SP)+10 > cpustate->sreg[SS].limit+1)
25372537               {
25382538                  logerror("IRET: SP is past SS limit.\n");
25392539                  FAULT(FAULT_SS,0)               }
25402540            }
25412541            else
25422542            {
2543               if(REG32(ESP)+20 > cpustate->sreg[SS].limit)
2543               if(REG32(ESP)+20 > cpustate->sreg[SS].limit+1)
25442544               {
25452545                  logerror("IRET: ESP is past SS limit.\n");
25462546                  FAULT(FAULT_SS,0)
src/emu/cpu/i386/i386op32.c
r14111r14112
952952static void I386OP(jcxz32)(i386_state *cpustate)         // Opcode 0xe3
953953{
954954   INT8 disp = FETCH(cpustate);
955   if( REG32(ECX) == 0 ) {
955   int val = (cpustate->address_size)?(REG32(ECX) == 0):(REG16(CX) == 0);
956   if( val ) {
956957      cpustate->eip += disp;
957958      CHANGE_PC(cpustate,cpustate->eip);
958959      CYCLES(cpustate,CYCLES_JCXZ);      /* TODO: Timing = 9 + m */
r14111r14112
32893290static void I386OP(load_far_pointer32)(i386_state *cpustate, int s)
32903291{
32913292   UINT8 modrm = FETCH(cpustate);
3293   UINT16 selector;
32923294
32933295   if( modrm >= 0xc0 ) {
32943296      fatalerror("i386: load_far_pointer32 NYI");
32953297   } else {
32963298      UINT32 ea = GetEA(cpustate,modrm);
32973299      STORE_REG32(modrm, READ32(cpustate,ea + 0));
3298      cpustate->sreg[s].selector = READ16(cpustate,ea + 4);
3299      i386_load_segment_descriptor(cpustate, s );
3300      selector = READ16(cpustate,ea + 4);
3301      if(PROTECTED_MODE && !(V8086_MODE))
3302         i386_protected_mode_sreg_load(cpustate,selector,s);
3303      else
3304      {
3305         cpustate->sreg[s].selector = selector;
3306         i386_load_segment_descriptor(cpustate, s );
3307      }
33003308   }
33013309}
33023310

Previous 15994 Revisions Next


© 1998-2018 The MESS Team