Previous 15994 Revisions Next

r15215 Thursday 17th May, 2012 at 23:05:43 UTC by Barry Rodewald
i386: IRET/Virtual 8086 mode fixes. [Carl]
[src/emu/cpu/i386]i386.c i386op16.c i386op32.c i386ops.c i386priv.h

src/emu/cpu/i386/i386op16.c
r15214r15215
16011601   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
16021602}
16031603
1604static void I386OP(pop_ds16)(i386_state *cpustate)         // Opcode 0x1f
1604static bool I386OP(pop_seg16)(i386_state *cpustate, int segment)
16051605{
1606   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1606   UINT32 ea, offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1607   UINT16 value;
1608   bool fault;
16071609   if(i386_limit_check(cpustate,SS,offset+1) == 0)
16081610   {
1609      cpustate->sreg[DS].selector = POP16(cpustate);
1610      if( PROTECTED_MODE ) {
1611         i386_load_segment_descriptor(cpustate,DS);
1612      } else {
1613         i386_load_segment_descriptor(cpustate,DS);
1614      }
1611      ea = i386_translate(cpustate, SS, offset, 0);
1612      value = READ16(cpustate, ea);
1613      i386_sreg_load(cpustate,value, segment, &fault);
1614      if(fault) return false;
1615      if(STACK_32BIT)
1616         REG32(ESP) = offset + 2;
1617      else
1618         REG16(SP) = offset + 2;
16151619   }
16161620   else
1617      FAULT(FAULT_SS,0)
1621   {
1622      cpustate->ext = 1;
1623      i386_trap_with_error(cpustate,FAULT_SS,0,0,0);
1624      return false;
1625   }
16181626   CYCLES(cpustate,CYCLES_POP_SREG);
1627   return true;
16191628}
16201629
1630static void I386OP(pop_ds16)(i386_state *cpustate)         // Opcode 0x1f
1631{
1632   I386OP(pop_seg16)(cpustate, DS);
1633}
1634
16211635static void I386OP(pop_es16)(i386_state *cpustate)         // Opcode 0x07
16221636{
1623   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1624   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1625   {
1626      cpustate->sreg[ES].selector = POP16(cpustate);
1627      if( PROTECTED_MODE ) {
1628         i386_load_segment_descriptor(cpustate,ES);
1629      } else {
1630         i386_load_segment_descriptor(cpustate,ES);
1631      }
1632   }
1633   else
1634      FAULT(FAULT_SS,0)
1635   CYCLES(cpustate,CYCLES_POP_SREG);
1637   I386OP(pop_seg16)(cpustate, ES);
16361638}
16371639
16381640static void I386OP(pop_fs16)(i386_state *cpustate)         // Opcode 0x0f a1
16391641{
1640   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1641   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1642   {
1643      cpustate->sreg[FS].selector = POP16(cpustate);
1644      if( PROTECTED_MODE ) {
1645         i386_load_segment_descriptor(cpustate,FS);
1646      } else {
1647         i386_load_segment_descriptor(cpustate,FS);
1648      }
1649   }
1650   else
1651      FAULT(FAULT_SS,0)
1652   CYCLES(cpustate,CYCLES_POP_SREG);
1642   I386OP(pop_seg16)(cpustate, FS);
16531643}
16541644
16551645static void I386OP(pop_gs16)(i386_state *cpustate)         // Opcode 0x0f a9
16561646{
1657   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1658   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1659   {
1660      cpustate->sreg[GS].selector = POP16(cpustate);
1661      if( PROTECTED_MODE ) {
1662         i386_load_segment_descriptor(cpustate,GS);
1663      } else {
1664         i386_load_segment_descriptor(cpustate,GS);
1665      }
1666   }
1667   else
1668      FAULT(FAULT_SS,0)
1669   CYCLES(cpustate,CYCLES_POP_SREG);
1647   I386OP(pop_seg16)(cpustate, GS);
16701648}
16711649
16721650static void I386OP(pop_ss16)(i386_state *cpustate)         // Opcode 0x17
16731651{
1674   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1652   if(!I386OP(pop_seg16)(cpustate, SS)) return;
16751653   if(cpustate->IF != 0) // if external interrupts are enabled
16761654   {
16771655      cpustate->IF = 0;  // reset IF for the next instruction
16781656      cpustate->delayed_interrupt_enable = 1;
16791657   }
1680
1681   if(i386_limit_check(cpustate,SS,offset+1) == 0)
1682   {
1683      cpustate->sreg[SS].selector = POP16(cpustate);
1684      if( PROTECTED_MODE ) {
1685         i386_load_segment_descriptor(cpustate,SS);
1686      } else {
1687         i386_load_segment_descriptor(cpustate,SS);
1688      }
1689   }
1690   else
1691      FAULT(FAULT_SS,0)
1692   CYCLES(cpustate,CYCLES_POP_SREG);
16931658}
16941659
16951660static void I386OP(pop_rm16)(i386_state *cpustate)         // Opcode 0x8f
16961661{
16971662   UINT8 modrm = FETCH(cpustate);
16981663   UINT16 value;
1699   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1664   UINT32 ea, offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
17001665
17011666   if(i386_limit_check(cpustate,SS,offset+1) == 0)
17021667   {
1668      UINT32 temp_sp = REG32(ESP);
17031669      value = POP16(cpustate);
17041670
17051671      if( modrm >= 0xc0 ) {
17061672         STORE_RM16(modrm, value);
17071673      } else {
1708         UINT32 ea = GetEA(cpustate,modrm,1);
1709         WRITE16(cpustate,ea, value);
1674         ea = GetEA(cpustate,modrm,1);
1675         try
1676         {
1677            WRITE16(cpustate,ea, value);
1678         }
1679         catch(UINT64 e)
1680         {
1681            REG32(ESP) = temp_sp;
1682            throw e;
1683         }
17101684      }
17111685   }
17121686   else
r15214r15215
37193693      UINT32 ea = GetEA(cpustate,modrm,0);
37203694      STORE_REG16(modrm, READ16(cpustate,ea + 0));
37213695      selector = READ16(cpustate,ea + 2);
3722      if(PROTECTED_MODE && !(V8086_MODE))
3723         i386_protected_mode_sreg_load(cpustate,selector,s);
3724      else
3725      {
3726         cpustate->sreg[s].selector = selector;
3727         i386_load_segment_descriptor(cpustate, s );
3728      }
3696      i386_sreg_load(cpustate,selector,s,NULL);
37293697   }
37303698}
37313699
src/emu/cpu/i386/i386priv.h
r15214r15215
401401      if(i386_limit_check(cpustate, segment, ip))
402402         FAULT_THROW((segment==SS)?FAULT_SS:FAULT_GP, 0);
403403      if((rwn == 0) && ((cpustate->sreg[segment].flags & 8) && !(cpustate->sreg[segment].flags & 2)))
404         FAULT_THROW((segment==SS)?FAULT_SS:FAULT_GP, 0);
404         FAULT_THROW(FAULT_GP, 0);
405405      if((rwn == 1) && ((cpustate->sreg[segment].flags & 8) || !(cpustate->sreg[segment].flags & 2)))
406         FAULT_THROW((segment==SS)?FAULT_SS:FAULT_GP, 0);
406         FAULT_THROW(FAULT_GP, 0);
407407   }
408408   return cpustate->sreg[segment].base + ip;
409409}
410410
411// rwn; read = 0, write = 1, none = -1
411// rwn; read = 0, write = 1, none = -1, read at PL 0 = -2
412412INLINE int translate_address(i386_state *cpustate, int rwn, UINT32 *address, UINT32 *error)
413413{
414414   UINT32 a = *address;
r15214r15215
418418   UINT32 offset = a & 0xfff;
419419   UINT32 page_entry;
420420   UINT32 ret = 1;
421   bool user = (cpustate->CPL == 3);
421   bool user = (cpustate->CPL == 3) && (rwn >= 0);
422422   *error = 0;
423423
424424   // TODO: cr0 wp bit, 486 and higher
r15214r15215
675675   }
676676   return value;
677677}
678INLINE UINT8 READ8PL0(i386_state *cpustate,UINT32 ea)
679{
680   UINT32 address = ea, error;
678681
682   if (cpustate->cr[0] & 0x80000000)      // page translation enabled
683   {
684      if(!translate_address(cpustate,-2,&address,&error))
685         PF_THROW(error);
686   }
687
688   address &= cpustate->a20_mask;
689   return cpustate->program->read_byte(address);
690}
691INLINE UINT16 READ16PL0(i386_state *cpustate,UINT32 ea)
692{
693   UINT16 value;
694   UINT32 address = ea, error;
695
696   if( ea & 0x1 ) {      /* Unaligned read */
697      value = (READ8PL0( cpustate, address+0 ) << 0) |
698            (READ8PL0( cpustate, address+1 ) << 8);
699   } else {
700      if (cpustate->cr[0] & 0x80000000)      // page translation enabled
701      {
702         if(!translate_address(cpustate,-2,&address,&error))
703            PF_THROW(error);
704      }
705
706      address &= cpustate->a20_mask;
707      value = cpustate->program->read_word( address );
708   }
709   return value;
710}
711INLINE UINT32 READ32PL0(i386_state *cpustate,UINT32 ea)
712{
713   UINT32 value;
714   UINT32 address = ea, error;
715
716   if( ea & 0x3 ) {      /* Unaligned read */
717      value = (READ8PL0( cpustate, address+0 ) << 0) |
718            (READ8PL0( cpustate, address+1 ) << 8) |
719            (READ8PL0( cpustate, address+2 ) << 16) |
720            (READ8PL0( cpustate, address+3 ) << 24);
721   } else {
722      if (cpustate->cr[0] & 0x80000000)      // page translation enabled
723      {
724         if(!translate_address(cpustate,-2,&address,&error))
725            PF_THROW(error);
726      }
727
728      address &= cpustate->a20_mask;
729      value = cpustate->program->read_dword( address );
730   }
731   return value;
732}
733
679734INLINE void WRITE_TEST(i386_state *cpustate,UINT32 ea)
680735{
681736   UINT32 address = ea, error;
r15214r15215
10801135      FAULT_THROW(FAULT_GP,0);
10811136
10821137   address = cpustate->task.base;
1083   IOPB = READ16(cpustate, address+0x66);
1084   if((IOPB+(port/8)) >= cpustate->task.limit)
1138   IOPB = READ16PL0(cpustate, address+0x66);
1139   if((IOPB+(port/8)) > cpustate->task.limit)
10851140      FAULT_THROW(FAULT_GP,0);
10861141
1087   map = READ8(cpustate, address+IOPB+(port/8));
1142   map = READ8PL0(cpustate, address+IOPB+(port/8));
10881143   map >>= (port%8);
10891144   if(map & mask)
10901145      FAULT_THROW(FAULT_GP,0);
src/emu/cpu/i386/i386.c
r15214r15215
5353   if (limit == 0 || entry + 7 > limit)
5454      return 0;
5555
56   int cpl = cpustate->CPL;
57   cpustate->CPL = 0;
56   v1 = READ32PL0(cpustate, base + entry );
57   v2 = READ32PL0(cpustate, base + entry + 4 );
5858
59   v1 = READ32(cpustate, base + entry );
60   v2 = READ32(cpustate, base + entry + 4 );
61
62   cpustate->CPL = cpl;
63
6459   seg->flags = (v2 >> 8) & 0xf0ff;
6560   seg->base = (v2 & 0xff000000) | ((v2 & 0xff) << 16) | ((v1 >> 16) & 0xffff);
6661   seg->limit = (v2 & 0xf0000) | (v1 & 0xffff);
r15214r15215
9388   if (limit == 0 || entry + 7 > limit)
9489      return;
9590
96   int cpl = cpustate->CPL;
97   cpustate->CPL = 0;
91   v1 = READ32PL0(cpustate, base + entry );
92   v2 = READ32PL0(cpustate, base + entry + 4 );
9893
99   v1 = READ32(cpustate, base + entry );
100   v2 = READ32(cpustate, base + entry + 4 );
101
102   cpustate->CPL = cpl;
103
10494   /* Note that for task gates, offset and dword_count are not used */
10595   gate->selector = (v1 >> 16) & 0xffff;
10696   gate->offset = (v1 & 0x0000ffff) | (v2 & 0xffff0000);
r15214r15215
110100   gate->dpl = (gate->ar >> 5) & 0x03;
111101}
112102
103static void i386_set_descriptor_accessed(i386_state *cpustate, UINT16 selector)
104{
105   // assume the selector is valid, we don't need to check it again
106   UINT32 base, addr, error;
107   UINT8 rights;
108   if ( selector & 0x4 )
109      base = cpustate->ldtr.base;
110   else
111      base = cpustate->gdtr.base;
112
113   addr = base + (selector & ~7) + 5;
114   translate_address(cpustate, -2, &addr, &error);
115   rights = cpustate->program->read_byte(addr);
116   // Should a fault be thrown if the table is read only?
117   cpustate->program->write_byte(addr, rights | 1);
118}
119
113120static void i386_load_segment_descriptor(i386_state *cpustate, int segment )
114121{
115122   if (PROTECTED_MODE)
116123   {
117124      if (!V8086_MODE)
125      {
118126         i386_load_protected_mode_segment(cpustate, &cpustate->sreg[segment], NULL );
127         i386_set_descriptor_accessed(cpustate, cpustate->sreg[segment].selector);
128      }
119129      else
120130      {
121131         cpustate->sreg[segment].base = cpustate->sreg[segment].selector << 4;
r15214r15215
143153   if(privilege >= 3)
144154      return 0;
145155
146   int cpl = cpustate->CPL;
147   cpustate->CPL = 0;
148
149156   if(cpustate->task.flags & 8)
150      ret = READ32(cpustate,(cpustate->task.base+8) + (8*privilege));
157      ret = READ32PL0(cpustate,(cpustate->task.base+8) + (8*privilege));
151158   else
152      ret = READ16(cpustate,(cpustate->task.base+4) + (4*privilege));
159      ret = READ16PL0(cpustate,(cpustate->task.base+4) + (4*privilege));
153160
154   cpustate->CPL = cpl;
155161   return ret;
156162}
157163
r15214r15215
162168   if(privilege >= 3)
163169      return 0;
164170
165   int cpl = cpustate->CPL;
166   cpustate->CPL = 0;
167
168171   if(cpustate->task.flags & 8)
169      ret = READ32(cpustate,(cpustate->task.base+4) + (8*privilege));
172      ret = READ32PL0(cpustate,(cpustate->task.base+4) + (8*privilege));
170173   else
171      ret = READ16(cpustate,(cpustate->task.base+2) + (4*privilege));
174      ret = READ16PL0(cpustate,(cpustate->task.base+2) + (4*privilege));
172175
173   cpustate->CPL = cpl;
174176   return ret;
175177}
176178
r15214r15215
439441   return 0;
440442}
441443
442static void i386_protected_mode_sreg_load(i386_state *cpustate, UINT16 selector, UINT8 reg)
444static void i386_sreg_load(i386_state *cpustate, UINT16 selector, UINT8 reg, bool *fault)
443445{
444446   // Checks done when MOV changes a segment register in protected mode
445447   UINT8 CPL,RPL,DPL;
r15214r15215
447449   CPL = cpustate->CPL;
448450   RPL = selector & 0x0003;
449451
452   if(!PROTECTED_MODE || V8086_MODE)
453   {
454      cpustate->sreg[reg].selector = selector;
455      i386_load_segment_descriptor(cpustate, reg);
456      if(fault) *fault = false;
457      return;
458   }     
459   
460   if(fault) *fault = true;
450461   if(reg == SS)
451462   {
452463      I386_SREG stack;
r15214r15215
506517      {
507518         cpustate->sreg[reg].selector = selector;
508519         i386_load_segment_descriptor(cpustate, reg );
520         if(fault) *fault = false;
509521         return;
510522      }
511523
r15214r15215
532544      }
533545      if((desc.flags & 0x0018) != 0x10)
534546      {
535         if(((desc.flags & 0x0002) != 0) && ((desc.flags & 0x0018) != 0x18))
547         if((((desc.flags & 0x0002) != 0) && ((desc.flags & 0x0018) != 0x18)) || !(desc.flags & 0x10))
536548         {
537549            logerror("SReg Load (%08x): Segment is not a data segment or readable code segment.\n",cpustate->pc);
538550            FAULT(FAULT_GP,selector & ~0x03)
r15214r15215
556568
557569   cpustate->sreg[reg].selector = selector;
558570   i386_load_segment_descriptor(cpustate, reg );
571   if(fault) *fault = false;
559572}
560573
561574static void i386_trap(i386_state *cpustate,int irq, int irq_gate, int trap_level)
r15214r15215
581594     *  0x10    Coprocessor error
582595     */
583596   UINT32 v1, v2;
584   UINT32 offset;
597   UINT32 offset, oldflags = get_flags(cpustate);
585598   UINT16 segment;
586599   int entry = irq * (PROTECTED_MODE ? 8 : 4);
587600   int SetRPL = 0;
r15214r15215
589602   if( !(PROTECTED_MODE) )
590603   {
591604      /* 16-bit */
592      PUSH16(cpustate, get_flags(cpustate) & 0xffff );
605      PUSH16(cpustate, oldflags & 0xffff );
593606      PUSH16(cpustate, cpustate->sreg[CS].selector );
594607      if(irq == 3 || irq == 4 || irq == 9 || irq_gate == 1)
595608         PUSH16(cpustate, cpustate->eip );
r15214r15215
610623      UINT8 CPL = cpustate->CPL, DPL = 0; //, RPL = 0;
611624
612625      /* 32-bit */
613      cpustate->CPL = 0;
614      v1 = READ32(cpustate, cpustate->idtr.base + entry );
615      v2 = READ32(cpustate, cpustate->idtr.base + entry + 4 );
616      cpustate->CPL = CPL;
626      v1 = READ32PL0(cpustate, cpustate->idtr.base + entry );
627      v2 = READ32PL0(cpustate, cpustate->idtr.base + entry + 4 );
617628      offset = (v2 & 0xffff0000) | (v1 & 0xffff);
618629      segment = (v1 >> 16) & 0xffff;
619630      type = (v2>>8) & 0x1F;
r15214r15215
640651      /* segment must be interrupt gate, trap gate, or task gate */
641652      if(type != 0x05 && type != 0x06 && type != 0x07 && type != 0x0e && type != 0x0f)
642653      {
643         logerror("IRQ#%i (%08x): Vector segment %04x is not an interrupt, trap or task gate.\n",irq,cpustate->pc,segment);
654         logerror("IRQ#%02x (%08x): Vector segment %04x is not an interrupt, trap or task gate.\n",irq,cpustate->pc,segment);
644655         FAULT_EXP(FAULT_GP,entry+2)
645656      }
646657
r15214r15215
659670         FAULT_EXP(FAULT_NP,entry+2)
660671      }
661672
662      if(V8086_MODE)
663      {
664         UINT32 tempflags;
665         UINT32 tempESP,tempSS;
666         /* Interrupt for a Virtual 8086 task */
667
668         logerror("IRQ (%08x): Interrupt during V8086 task\n",cpustate->pc);
669         tempflags = get_flags(cpustate);
670         cpustate->VM = 0;
671         cpustate->TF = 0;
672         cpustate->NT = 0;
673         if(type == 0x0e || type == 0x06)
674            cpustate->IF = 0;
675         tempSS = cpustate->sreg[SS].selector;
676         tempESP = REG32(ESP);
677         /* Get privilege level 0 stack pointer from TSS */
678         cpustate->sreg[SS].selector = i386_get_stack_segment(cpustate,0);
679         REG32(ESP) = i386_get_stack_ptr(cpustate,0);
680         cpustate->CPL = segment & 0x03;
681         i386_load_segment_descriptor(cpustate,SS);
682         PUSH32(cpustate,cpustate->sreg[GS].selector & 0xffff);
683         PUSH32(cpustate,cpustate->sreg[FS].selector & 0xffff);
684         PUSH32(cpustate,cpustate->sreg[DS].selector & 0xffff);
685         PUSH32(cpustate,cpustate->sreg[ES].selector & 0xffff);
686         cpustate->sreg[GS].selector = 0;
687         cpustate->sreg[FS].selector = 0;
688         cpustate->sreg[DS].selector = 0;
689         cpustate->sreg[ES].selector = 0;
690         i386_load_segment_descriptor(cpustate,GS);
691         i386_load_segment_descriptor(cpustate,FS);
692         i386_load_segment_descriptor(cpustate,DS);
693         i386_load_segment_descriptor(cpustate,ES);
694         PUSH32(cpustate,tempSS & 0xffff);
695         PUSH32(cpustate,tempESP);
696         PUSH32(cpustate,tempflags);
697         PUSH32(cpustate,cpustate->sreg[CS].selector & 0xffff);
698         if(irq == 3 || irq == 4 || irq == 9 || irq_gate == 1)
699            PUSH32(cpustate, cpustate->eip );
700         else
701            PUSH32(cpustate, cpustate->prev_eip );
702
703         cpustate->sreg[CS].selector = segment;
704         cpustate->eip = offset;
705         // CPL set to CS RPL?
706
707         i386_load_segment_descriptor(cpustate,CS);
708         CHANGE_PC(cpustate,cpustate->eip);
709         return;
710      }
711
712673      if(type == 0x05)
713674      {
714675         /* Task gate */
r15214r15215
793754            I386_SREG stack;
794755            UINT32 newESP,oldSS,oldESP;
795756
757            if(V8086_MODE && DPL)
758            {
759               logerror("IRQ: Gate to CPL>0 from VM86 mode.\n");
760               FAULT_EXP(FAULT_GP,segment & ~0x03);
761            }
796762            /* Check new stack segment in TSS */
797763            memset(&stack, 0, sizeof(stack));
798764            stack.selector = i386_get_stack_segment(cpustate,DPL);
r15214r15215
846812            newESP = i386_get_stack_ptr(cpustate,DPL);
847813            if(type & 0x08) // 32-bit gate
848814            {
849               if(newESP < 20)
815               if(newESP < (V8086_MODE?36:20))
850816               {
851817                  logerror("IRQ: New stack has no space for return addresses.\n");
852818                  FAULT_EXP(FAULT_SS,0)
r15214r15215
855821            else // 16-bit gate
856822            {
857823               newESP &= 0xffff;
858               if(newESP < 10)
824               if(newESP < (V8086_MODE?18:10))
859825               {
860826                  logerror("IRQ: New stack has no space for return addresses.\n");
861827                  FAULT_EXP(FAULT_SS,0)
r15214r15215
872838            WRITE_TEST(cpustate, stack.base+newESP-1);
873839            /* Load new stack segment descriptor */
874840            cpustate->sreg[SS].selector = stack.selector;
875            i386_load_segment_descriptor(cpustate,SS);
841            i386_load_protected_mode_segment(cpustate,&cpustate->sreg[SS],NULL);
842            i386_set_descriptor_accessed(cpustate, stack.selector);
876843            if(flags & 0x0008)
877844               REG32(ESP) = i386_get_stack_ptr(cpustate,DPL);
878845            else
879846               REG16(SP) = i386_get_stack_ptr(cpustate,DPL);
847            if(V8086_MODE)
848            {
849               logerror("IRQ (%08x): Interrupt during V8086 task\n",cpustate->pc);
850               if(type & 0x08)
851               {
852                  PUSH32(cpustate,cpustate->sreg[GS].selector & 0xffff);
853                  PUSH32(cpustate,cpustate->sreg[FS].selector & 0xffff);
854                  PUSH32(cpustate,cpustate->sreg[DS].selector & 0xffff);
855                  PUSH32(cpustate,cpustate->sreg[ES].selector & 0xffff);
856               }
857               else
858               {
859                  PUSH16(cpustate,cpustate->sreg[GS].selector);
860                  PUSH16(cpustate,cpustate->sreg[FS].selector);
861                  PUSH16(cpustate,cpustate->sreg[DS].selector);
862                  PUSH16(cpustate,cpustate->sreg[ES].selector);
863               }
864               cpustate->sreg[GS].selector = 0;
865               cpustate->sreg[FS].selector = 0;
866               cpustate->sreg[DS].selector = 0;
867               cpustate->sreg[ES].selector = 0;
868               cpustate->VM = 0;
869               i386_load_segment_descriptor(cpustate,GS);
870               i386_load_segment_descriptor(cpustate,FS);
871               i386_load_segment_descriptor(cpustate,DS);
872               i386_load_segment_descriptor(cpustate,ES);
873            }
880874            if(type & 0x08)
881875            {
882876               // 32-bit gate
r15214r15215
897891            if((desc.flags & 0x0004) || (DPL == CPL))
898892            {
899893               /* IRQ to same privilege */
894               if(V8086_MODE)
895               {
896                  logerror("IRQ: Gate to same privilege from VM86 mode.\n");
897                  FAULT_EXP(FAULT_GP,segment & ~0x03);
898               }
900899               if(type == 0x0e || type == 0x0f)  // 32-bit gate
901900                  stack_limit = 10;
902901               else
r15214r15215
924923
925924      if(type != 0x0e && type != 0x0f)  // if not 386 interrupt or trap gate
926925      {
927         PUSH16(cpustate, get_flags(cpustate) & 0xffff );
926         PUSH16(cpustate, oldflags & 0xffff );
928927         PUSH16(cpustate, cpustate->sreg[CS].selector );
929928         if(irq == 3 || irq == 4 || irq == 9 || irq_gate == 1)
930929            PUSH16(cpustate, cpustate->eip );
r15214r15215
933932      }
934933      else
935934      {
936         PUSH32(cpustate, get_flags(cpustate) & 0x00fcffff );
935         PUSH32(cpustate, oldflags & 0x00ffffff );
937936         PUSH32(cpustate, cpustate->sreg[CS].selector );
938937         if(irq == 3 || irq == 4 || irq == 9 || irq_gate == 1)
939938            PUSH32(cpustate, cpustate->eip );
r15214r15215
967966      {
968967         UINT32 entry = irq * 8;
969968         UINT32 v2,type;
970         int cpl = cpustate->CPL;
971         cpustate->CPL = 0;
972         v2 = READ32(cpustate, cpustate->idtr.base + entry + 4 );
969         v2 = READ32PL0(cpustate, cpustate->idtr.base + entry + 4 );
973970         type = (v2>>8) & 0x1F;
974971         if(type == 5)
975972         {
976            v2 = READ32(cpustate, cpustate->idtr.base + entry);
977            v2 = READ32(cpustate, cpustate->gdtr.base + ((v2 >> 16) & 0xfff8) + 4);
973            v2 = READ32PL0(cpustate, cpustate->idtr.base + entry);
974            v2 = READ32PL0(cpustate, cpustate->gdtr.base + ((v2 >> 16) & 0xfff8) + 4);
978975            type = (v2>>8) & 0x1F;
979976         }
980         cpustate->CPL = cpl;
981977         if(type >= 9)
982978            PUSH32(cpustate,error);
983979         else
r15214r15215
10311027   WRITE16(cpustate,tss+0x28,cpustate->sreg[DS].selector);
10321028
10331029   old_task = cpustate->task.segment;
1034
1030   
10351031   /* Load task register with the selector of the incoming task */
10361032   cpustate->task.segment = selector;
10371033   memset(&seg, 0, sizeof(seg));
r15214r15215
11551151   cpustate->cr[0] |= 0x08;
11561152
11571153   /* Load incoming task state from the new task's TSS */
1158   cpustate->CPL = 0;
11591154   tss = cpustate->task.base;
11601155   cpustate->ldtr.segment = READ32(cpustate,tss+0x60) & 0xffff;
11611156   seg.selector = cpustate->ldtr.segment;
r15214r15215
22402235         logerror("IRET (%08x): Is in Virtual 8086 mode and IOPL != 3.\n",cpustate->pc);
22412236         FAULT(FAULT_GP,0)
22422237      }
2243      /* Is this correct?  The 80386 programmers' reference says IRET should always trigger #GP(0) in V86 mode */
22442238      if(operand32 == 0)
22452239      {
22462240         cpustate->eip = newEIP & 0xffff;
r15214r15215
22992293   {
23002294      if(newflags & 0x00020000) // if returning to virtual 8086 mode
23012295      {
2302//          UINT8 SSRPL,SSDPL;
2303         if(operand32 == 0)
2304         {
2305            newESP = READ16(cpustate, ea+6) & 0xffff;
2306            newSS = READ16(cpustate, ea+8) & 0xffff;
2307         }
2308         else
2309         {
2310            newESP = READ32(cpustate, ea+12);
2311            newSS = READ32(cpustate, ea+16) & 0xffff;
2312         }
2296         // 16-bit iret can't reach here
2297         newESP = READ32(cpustate, ea+12);
2298         newSS = READ32(cpustate, ea+16) & 0xffff;
23132299         memset(&desc, 0, sizeof(desc));
23142300         desc.selector = newCS;
23152301         i386_load_protected_mode_segment(cpustate,&desc,NULL);
r15214r15215
23182304         memset(&stack, 0, sizeof(stack));
23192305         stack.selector = newSS;
23202306         i386_load_protected_mode_segment(cpustate,&stack,NULL);
2321         //SSRPL = newSS & 0x03;
2322         //SSDPL = (stack.flags >> 5) & 0x03;
23232307
23242308         /* Return to v86 mode */
23252309         logerror("IRET (%08x): Returning to Virtual 8086 mode.\n",cpustate->pc);
2326         // Should these be done at this point?  The 386 programmers' reference is a bit confusing about this
2327/*          if(RPL != 3)
2328            {
2329                logerror("IRET to V86 (%08x): Return CS RPL is not 3\n",cpustate->pc);
2330                FAULT(FAULT_GP,newCS);
2331            }
2332            if(operand32 == 0)
2333            {
2334                if(REG16(SP)+36 > cpustate->sreg[SS].limit)
2335                {
2336                    logerror("IRET to V86 (%08x): Stack does not have enough room left\n",cpustate->pc);
2337                    FAULT(FAULT_SS,0);
2338                }
2339            }
2340            else
2341            {
2342                if(REG32(ESP)+36 > cpustate->sreg[SS].limit)
2343                {
2344                    logerror("IRET to V86 (%08x): Stack does not have enough space left\n",cpustate->pc);
2345                    FAULT(FAULT_SS,0);
2346                }
2347            }
2348            // code segment checks
2349            if((newCS & ~0x07) == 0)
2350            {
2351                logerror("IRET to V86 (%08x): Return CS selector is null\n",cpustate->pc);
2352                FAULT(FAULT_GP,newCS);
2353            }
2354            if(desc.flags & 0x04)
2355            {  // LDT
2356                if(newCS > cpustate->ldtr.limit)
2357                {
2358                    logerror("IRET to V86 (%08x): Return CS selector is past LDT limit\n",cpustate->pc);
2359                    FAULT(FAULT_GP,newCS);
2360                }
2361            }
2362            else
2363            {  // GDT
2364                if(newCS > cpustate->gdtr.limit)
2365                {
2366                    logerror("IRET to V86 (%08x): Return CS selector is past GDT limit\n",cpustate->pc);
2367                    FAULT(FAULT_GP,newCS);
2368                }
2369            }
2370            if((desc.flags & 0x18) != 0x18)
2371            {
2372                logerror("IRET to V86 (%08x): Return CS segment is not a code segment\n",cpustate->pc);
2373                FAULT(FAULT_GP,newCS);
2374            }
2375            if(DPL != 3)
2376            {
2377                logerror("IRET to V86 (%08x): Return CS segment does not have a DPL of 3\n",cpustate->pc);
2378                FAULT(FAULT_GP,newCS);
2379            }
2380            if(!(desc.flags & 0x0080))
2381            {
2382                logerror("IRET to V86 (%08x): Return CS segment is not present\n",cpustate->pc);
2383                FAULT(FAULT_NP,newCS);
2384            }
2385            // Stack segment checks
2386            if((newSS & ~0x07) == 0)
2387            {
2388                logerror("IRET to V86 (%08x): Return SS segment is null\n",cpustate->pc);
2389                FAULT(FAULT_GP,newSS);
2390            }
2391            if(desc.flags & 0x04)
2392            {  // LDT
2393                if(newSS > cpustate->ldtr.limit)
2394                {
2395                    logerror("IRET to V86 (%08x): Return SS selector is past LDT limit\n",cpustate->pc);
2396                    FAULT(FAULT_GP,newSS);
2397                }
2398            }
2399            else
2400            {  // GDT
2401                if(newSS > cpustate->gdtr.limit)
2402                {
2403                    logerror("IRET to V86 (%08x): Return SS selector is past GDT limit\n",cpustate->pc);
2404                    FAULT(FAULT_GP,newSS);
2405                }
2406            }
2407            if(SSRPL != RPL)
2408            {
2409                logerror("IRET to V86 (%08x): Return SS selector RPL is not equal to CS selector RPL\n",cpustate->pc);
2410                FAULT(FAULT_GP,newSS);
2411            }
2412            if(((stack.flags & 0x0018) != 0x10) && (!(stack.flags & 0x02)))
2413            {
2414                logerror("IRET to V86 (%08x): Return SS segment is not a writable data segment\n",cpustate->pc);
2415                FAULT(FAULT_GP,newSS);
2416            }
2417            if(SSDPL != RPL)
2418            {
2419                logerror("IRET to V86 (%08x): Return SS segment DPL is not equal to CS selector RPL\n",cpustate->pc);
2420                FAULT(FAULT_GP,newSS);
2421            }
2422            if(!(stack.flags & 0x0080))
2423            {
2424                logerror("IRET to V86 (%08x): Return SS segment is not present\n",cpustate->pc);
2425                FAULT(FAULT_NP,newSS);
2426            }
2427
2428            if(newEIP > desc.limit)
2429            {
2430                logerror("IRET to V86 (%08x): New EIP is past CS segment limit\n",cpustate->pc);
2431                FAULT(FAULT_GP,0);
2432            }
2433            */
24342310         if(CPL != 0)
24352311         {
24362312            UINT32 oldflags = get_flags(cpustate);
r15214r15215
24402316         cpustate->eip = POP32(cpustate) & 0xffff;  // high 16 bits are ignored
24412317         cpustate->sreg[CS].selector = POP32(cpustate) & 0xffff;
24422318         POP32(cpustate);  // already set flags
2443//          if(RPL > CPL)
2444         {
2445            newESP = POP32(cpustate);
2446            newSS = POP32(cpustate) & 0xffff;
2447         }
2319         newESP = POP32(cpustate);
2320         newSS = POP32(cpustate) & 0xffff;
24482321         cpustate->sreg[ES].selector = POP32(cpustate) & 0xffff;
24492322         cpustate->sreg[DS].selector = POP32(cpustate) & 0xffff;
24502323         cpustate->sreg[FS].selector = POP32(cpustate) & 0xffff;
src/emu/cpu/i386/i386ops.c
r15214r15215
781781{
782782   UINT16 selector;
783783   UINT8 modrm = FETCH(cpustate);
784   bool fault;
784785   int s = (modrm >> 3) & 0x7;
785786
786787   if( modrm >= 0xc0 ) {
r15214r15215
792793      CYCLES(cpustate,CYCLES_MOV_MEM_SREG);
793794   }
794795
795   if(s == SS)
796   i386_sreg_load(cpustate,selector,s,&fault);
797   if((s == SS) && !fault)
796798   {
797799      if(cpustate->IF != 0) // if external interrupts are enabled
798800      {
r15214r15215
800802         cpustate->delayed_interrupt_enable = 1;
801803      }
802804   }
803
804   if(PROTECTED_MODE && !(V8086_MODE))  // no checks in virtual 8086 mode?
805      i386_protected_mode_sreg_load(cpustate,selector,s);
806   else
807   {
808      cpustate->sreg[s].selector = selector;
809      i386_load_segment_descriptor(cpustate, s );
810   }
811805}
812806
813807static void I386OP(mov_al_i8)(i386_state *cpustate)         // Opcode 0xb0
src/emu/cpu/i386/i386op32.c
r15214r15215
14511451   CYCLES(cpustate,CYCLES_POP_REG_SHORT);
14521452}
14531453
1454static void I386OP(pop_ds32)(i386_state *cpustate)         // Opcode 0x1f
1454static bool I386OP(pop_seg32)(i386_state *cpustate, int segment)
14551455{
1456   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1456   UINT32 ea, offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1457   UINT32 value;
1458   bool fault;
14571459   if(i386_limit_check(cpustate,SS,offset+3) == 0)
14581460   {
1459      cpustate->sreg[DS].selector = POP32(cpustate);
1460      if( PROTECTED_MODE ) {
1461         i386_load_segment_descriptor(cpustate,DS);
1462      } else {
1463         i386_load_segment_descriptor(cpustate,DS);
1464      }
1461      ea = i386_translate(cpustate, SS, offset, 0);
1462      value = READ32(cpustate, ea);
1463      i386_sreg_load(cpustate,value, segment, &fault);
1464      if(fault) return false;
1465      if(STACK_32BIT)
1466         REG32(ESP) = offset + 4;
1467      else
1468         REG16(SP) = offset + 4;
14651469   }
14661470   else
1467      FAULT(FAULT_SS,0)
1471   {
1472      cpustate->ext = 1;
1473      i386_trap_with_error(cpustate,FAULT_SS,0,0,0);
1474      return false;
1475   }
14681476   CYCLES(cpustate,CYCLES_POP_SREG);
1477   return true;
14691478}
14701479
1480static void I386OP(pop_ds32)(i386_state *cpustate)         // Opcode 0x1f
1481{
1482   I386OP(pop_seg32)(cpustate, DS);
1483}
1484
14711485static void I386OP(pop_es32)(i386_state *cpustate)         // Opcode 0x07
14721486{
1473   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1474   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1475   {
1476      cpustate->sreg[ES].selector = POP32(cpustate);
1477      if( PROTECTED_MODE ) {
1478         i386_load_segment_descriptor(cpustate,ES);
1479      } else {
1480         i386_load_segment_descriptor(cpustate,ES);
1481      }
1482   }
1483   else
1484      FAULT(FAULT_SS,0)
1485   CYCLES(cpustate,CYCLES_POP_SREG);
1487   I386OP(pop_seg32)(cpustate, ES);
14861488}
14871489
14881490static void I386OP(pop_fs32)(i386_state *cpustate)         // Opcode 0x0f a1
14891491{
1490   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1491   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1492   {
1493      cpustate->sreg[FS].selector = POP32(cpustate);
1494      if( PROTECTED_MODE ) {
1495         i386_load_segment_descriptor(cpustate,FS);
1496      } else {
1497         i386_load_segment_descriptor(cpustate,FS);
1498      }
1499   }
1500   else
1501      FAULT(FAULT_SS,0)
1502   CYCLES(cpustate,CYCLES_POP_SREG);
1492   I386OP(pop_seg32)(cpustate, FS);
15031493}
15041494
15051495static void I386OP(pop_gs32)(i386_state *cpustate)         // Opcode 0x0f a9
15061496{
1507   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1508   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1509   {
1510      cpustate->sreg[GS].selector = POP32(cpustate);
1511      if( PROTECTED_MODE ) {
1512         i386_load_segment_descriptor(cpustate,GS);
1513      } else {
1514         i386_load_segment_descriptor(cpustate,GS);
1515      }
1516   }
1517   else
1518      FAULT(FAULT_SS,0)
1519   CYCLES(cpustate,CYCLES_POP_SREG);
1497   I386OP(pop_seg32)(cpustate, GS);
15201498}
15211499
15221500static void I386OP(pop_ss32)(i386_state *cpustate)         // Opcode 0x17
15231501{
1524   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1502   if(!I386OP(pop_seg32)(cpustate, SS)) return;
15251503   if(cpustate->IF != 0) // if external interrupts are enabled
15261504   {
15271505      cpustate->IF = 0;  // reset IF for the next instruction
15281506      cpustate->delayed_interrupt_enable = 1;
15291507   }
1530
1531   if(i386_limit_check(cpustate,SS,offset+3) == 0)
1532   {
1533      cpustate->sreg[SS].selector = POP32(cpustate);
1534      if( PROTECTED_MODE ) {
1535         i386_load_segment_descriptor(cpustate,SS);
1536      } else {
1537         i386_load_segment_descriptor(cpustate,SS);
1538      }
1539   }
1540   else
1541      FAULT(FAULT_SS,0)
1542   CYCLES(cpustate,CYCLES_POP_SREG);
15431508}
15441509
15451510static void I386OP(pop_rm32)(i386_state *cpustate)         // Opcode 0x8f
15461511{
15471512   UINT8 modrm = FETCH(cpustate);
15481513   UINT32 value;
1549   UINT32 offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
1514   UINT32 ea, offset = (STACK_32BIT ? REG32(ESP) : REG16(SP));
15501515   if(i386_limit_check(cpustate,SS,offset+3) == 0)
15511516   {
1517      // be careful here, if the write references the esp register
1518      // it expects the post-pop value but esp must be wound back
1519      // if the write faults
1520      UINT32 temp_sp = REG32(ESP);
15521521      value = POP32(cpustate);
15531522
15541523      if( modrm >= 0xc0 ) {
15551524         STORE_RM32(modrm, value);
15561525      } else {
1557         UINT32 ea = GetEA(cpustate,modrm,1);
1558         WRITE32(cpustate,ea, value);
1526         ea = GetEA(cpustate,modrm,1);
1527         try
1528         {
1529            WRITE32(cpustate,ea, value);
1530         }
1531         catch(UINT64 e)
1532         {
1533            REG32(ESP) = temp_sp;
1534            throw e;
1535         }
15591536      }
15601537   }
15611538   else
r15214r15215
35143491      UINT32 ea = GetEA(cpustate,modrm,0);
35153492      STORE_REG32(modrm, READ32(cpustate,ea + 0));
35163493      selector = READ16(cpustate,ea + 4);
3517      if(PROTECTED_MODE && !(V8086_MODE))
3518         i386_protected_mode_sreg_load(cpustate,selector,s);
3519      else
3520      {
3521         cpustate->sreg[s].selector = selector;
3522         i386_load_segment_descriptor(cpustate, s );
3523      }
3494      i386_sreg_load(cpustate,selector,s,NULL);
35243495   }
35253496}
35263497

Previous 15994 Revisions Next


© 1998-2018 The MESS Team