Diff for /np2/i386c/ia32/exception.c between versions 1.7 and 1.28

version 1.7, 2004/02/04 13:24:35 version 1.28, 2011/12/21 16:25:52
Line 1 Line 1
 /*      $Id$    */  
   
 /*  /*
  * Copyright (c) 2003 NONAKA Kimihiro   * Copyright (c) 2003 NONAKA Kimihiro
  * All rights reserved.   * All rights reserved.
Line 12 Line 10
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    documentation and/or other materials provided with the distribution.
  * 3. The name of the author may not be used to endorse or promote products  
  *    derived from this software without specific prior written permission.  
  *   *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Line 72  exception(int num, int error_code) Line 68  exception(int num, int error_code)
   
         __ASSERT((unsigned int)num < EXCEPTION_NUM);          __ASSERT((unsigned int)num < EXCEPTION_NUM);
   
   #if 0
           iptrace_out();
           debugwriteseg("execption.bin", &CPU_CS_DESC, CPU_PREV_EIP & 0xffff0000, 0x10000);
   #endif
   
           VERBOSE(("exception: -------------------------------------------------------------- start"));
         VERBOSE(("exception: %s, error_code = %x at %04x:%08x", exception_str[num], error_code, CPU_CS, CPU_PREV_EIP));          VERBOSE(("exception: %s, error_code = %x at %04x:%08x", exception_str[num], error_code, CPU_CS, CPU_PREV_EIP));
         VERBOSE(("exception:------------------------------------------------"));  
         VERBOSE(("%s", cpu_reg2str()));          VERBOSE(("%s", cpu_reg2str()));
         VERBOSE(("exception:------------------------------------------------"));  
   
         CPU_STAT_NERROR++;          CPU_STAT_EXCEPTION_COUNTER_INC();
         if ((CPU_STAT_NERROR >= 3)           if ((CPU_STAT_EXCEPTION_COUNTER >= 3) 
          || (CPU_STAT_NERROR == 2 && CPU_STAT_PREV_EXCEPTION == DF_EXCEPTION)) {           || (CPU_STAT_EXCEPTION_COUNTER == 2 && CPU_STAT_PREV_EXCEPTION == DF_EXCEPTION)) {
                 /* Triple fault */                  /* Triple fault */
                 ia32_panic("exception: catch triple fault!");                  ia32_panic("exception: catch triple fault!");
         }          }
   
         switch (num) {          switch (num) {
         case DE_EXCEPTION:      /* (F) 除算エラー */          case DE_EXCEPTION:      /* (F) 除算エラー */
           case DB_EXCEPTION:      /* (F/T) デバッグ */
         case BR_EXCEPTION:      /* (F) BOUND の範囲外 */          case BR_EXCEPTION:      /* (F) BOUND の範囲外 */
         case UD_EXCEPTION:      /* (F) 無効オペコード */          case UD_EXCEPTION:      /* (F) 無効オペコード */
         case NM_EXCEPTION:      /* (F) デバイス使用不可 (FPU が無い) */          case NM_EXCEPTION:      /* (F) デバイス使用不可 (FPU が無い) */
           case MF_EXCEPTION:      /* (F) 浮動小数点エラー */
                 CPU_EIP = CPU_PREV_EIP;                  CPU_EIP = CPU_PREV_EIP;
                   if (CPU_STATSAVE.cpu_stat.backout_sp)
                           CPU_ESP = CPU_PREV_ESP;
                 /*FALLTHROUGH*/                  /*FALLTHROUGH*/
         case DB_EXCEPTION:      /* (F/T) デバッグ */  
         case NMI_EXCEPTION:     /* (I) NMI 割り込み */          case NMI_EXCEPTION:     /* (I) NMI 割り込み */
         case BP_EXCEPTION:      /* (T) ブレークポイント */          case BP_EXCEPTION:      /* (T) ブレークポイント */
         case OF_EXCEPTION:      /* (T) オーバーフロー */          case OF_EXCEPTION:      /* (T) オーバーフロー */
Line 103  exception(int num, int error_code) Line 106  exception(int num, int error_code)
                 error_code = 0;                  error_code = 0;
                 break;                  break;
   
 #if CPU_FAMILY >= 4  
         case AC_EXCEPTION:      /* (F) アラインメントチェック (errcode: 0) */          case AC_EXCEPTION:      /* (F) アラインメントチェック (errcode: 0) */
                 error_code = 0;                  error_code = 0;
                 /*FALLTHROUGH*/                  /*FALLTHROUGH*/
 #endif  
         case TS_EXCEPTION:      /* (F) 無効 TSS (errcode) */          case TS_EXCEPTION:      /* (F) 無効 TSS (errcode) */
         case NP_EXCEPTION:      /* (F) セグメント不在 (errcode) */          case NP_EXCEPTION:      /* (F) セグメント不在 (errcode) */
         case SS_EXCEPTION:      /* (F) スタックセグメントフォルト (errcode) */          case SS_EXCEPTION:      /* (F) スタックセグメントフォルト (errcode) */
         case GP_EXCEPTION:      /* (F) 一般保護例外 (errcode) */          case GP_EXCEPTION:      /* (F) 一般保護例外 (errcode) */
         case PF_EXCEPTION:      /* (F) ページフォルト (errcode) */          case PF_EXCEPTION:      /* (F) ページフォルト (errcode) */
                 CPU_EIP = CPU_PREV_EIP;                  CPU_EIP = CPU_PREV_EIP;
                   if (CPU_STATSAVE.cpu_stat.backout_sp)
                           CPU_ESP = CPU_PREV_ESP;
                 errorp = 1;                  errorp = 1;
                 break;                  break;
   
         case MF_EXCEPTION:      /* (F) 浮動小数点エラー */  
                 CPU_EIP = CPU_PREV_EIP;  
                 errorp = 0;  
                 break;  
   
 #if CPU_FAMILY >= 5  
         case MC_EXCEPTION:      /* (A) マシンチェック */  
                 CPU_EIP = CPU_PREV_EIP;  
                 errorp = 0;  
                 break;  
 #endif  
   
 #if CPU_FAMILY >= 6  
         case XF_EXCEPTION:      /* (F) ストリーミング SIMD 拡張命令 */  
                 CPU_EIP = CPU_PREV_EIP;  
                 errorp = 0;  
                 break;  
 #endif  
   
         default:          default:
                 ia32_panic("exception: unknown exception (%d)", num);                  ia32_panic("exception: unknown exception (%d)", num);
                 break;                  break;
         }          }
   
         if (CPU_STAT_NERROR >= 2) {          if (CPU_STAT_EXCEPTION_COUNTER >= 2) {
                 if (dftable[exctype[CPU_STAT_PREV_EXCEPTION]][exctype[num]]) {                  if (dftable[exctype[CPU_STAT_PREV_EXCEPTION]][exctype[num]]) {
                         num = DF_EXCEPTION;                          num = DF_EXCEPTION;
                           errorp = 1;
                           error_code = 0;
                 }                  }
         }          }
         CPU_STAT_PREV_EXCEPTION = num;          CPU_STAT_PREV_EXCEPTION = num;
   
         INTERRUPT(num, FALSE, errorp, error_code);          VERBOSE(("exception: ---------------------------------------------------------------- end"));
   
           interrupt(num, INTR_TYPE_EXTINTR, errorp, error_code);
   #if defined(IA32_SUPPORT_DEBUG_REGISTER)
           if (num != BP_EXCEPTION) {
                   if (CPU_INST_OP32) {
                           set_eflags(REAL_EFLAGREG|RF_FLAG, RF_FLAG);
                   }
           }
   #endif
           CPU_STAT_EXCEPTION_COUNTER_CLEAR();
         siglongjmp(exec_1step_jmpbuf, 1);          siglongjmp(exec_1step_jmpbuf, 1);
 }  }
   
Line 208  exception(int num, int error_code) Line 204  exception(int num, int error_code)
  * D          : ゲートのサイズ.0 = 16 bit, 1 = 32 bit   * D          : ゲートのサイズ.0 = 16 bit, 1 = 32 bit
  */   */
   
 static void interrupt_task(descriptor_t *gdp, int softintp, int errorp, int error_code);  static void interrupt_task_gate(const descriptor_t *gsdp, int intrtype, int errorp, int error_code);
 static void interrupt_intr_or_trap(descriptor_t *gdp, int softintp, int errorp, int error_code);  static void interrupt_intr_or_trap(const descriptor_t *gsdp, int intrtype, int errorp, int error_code);
   
 void  void
 interrupt(int num, int softintp, int errorp, int error_code)  interrupt(int num, int intrtype, int errorp, int error_code)
 {  {
         descriptor_t gd;          descriptor_t gsd;
         DWORD idt_idx;          UINT idt_idx;
         DWORD new_ip;          UINT32 new_ip;
         WORD new_cs;          UINT16 new_cs;
           int exc_errcode;
   
         VERBOSE(("interrupt: num = 0x%02x, softintp = %s, errorp = %s, error_code = %02x", num, softintp ? "on" : "off", errorp ? "on" : "off", error_code));          VERBOSE(("interrupt: num = 0x%02x, intrtype = %s, errorp = %s, error_code = %08x", num, intrtype ? "on" : "off", errorp ? "on" : "off", error_code));
   
           CPU_SET_PREV_ESP();
   
         if (!CPU_STAT_PM) {          if (!CPU_STAT_PM) {
                 /* real mode */                  /* real mode */
                   CPU_WORKCLOCK(20);
   
                 idt_idx = num * 4;                  idt_idx = num * 4;
                 if (idt_idx + 3 > CPU_IDTR_LIMIT) {                  if (idt_idx + 3 > CPU_IDTR_LIMIT) {
                         VERBOSE(("interrupt: real-mode IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT));                          VERBOSE(("interrupt: real-mode IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT));
                         EXCEPTION(GP_EXCEPTION, num * 4 | 2);                          EXCEPTION(GP_EXCEPTION, idt_idx + 2);
                 }                  }
   
                 if (!softintp) {                  if ((intrtype == INTR_TYPE_EXTINTR) && CPU_STAT_HLT) {
                         BYTE op = cpu_codefetch(CPU_IP);                          VERBOSE(("interrupt: reset HTL in real mode"));
                         if (op == 0xf4) {       /* hlt */                          CPU_EIP++;
                                 CPU_EIP++;                          CPU_STAT_HLT = 0;
                         }  
                 }                  }
   
                 REGPUSH0(REAL_FLAGREG);                  REGPUSH0(REAL_FLAGREG);
Line 243  interrupt(int num, int softintp, int err Line 243  interrupt(int num, int softintp, int err
                 CPU_EFLAG &= ~(T_FLAG | I_FLAG | AC_FLAG | RF_FLAG);                  CPU_EFLAG &= ~(T_FLAG | I_FLAG | AC_FLAG | RF_FLAG);
                 CPU_TRAP = 0;                  CPU_TRAP = 0;
   
                 new_ip = cpu_memoryread_w(CPU_IDTR_BASE + num * 4);                  new_ip = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx);
                 new_cs = cpu_memoryread_w(CPU_IDTR_BASE + num * 4 + 2);                  new_cs = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx + 2);
                 CPU_SET_SEGREG(CPU_CS_INDEX, new_cs);                  LOAD_SEGREG(CPU_CS_INDEX, new_cs);
                 SET_EIP(new_ip);                  CPU_EIP = new_ip;
                 CPU_WORKCLOCK(20);  
         } else {          } else {
                 /* protected mode */                  /* protected mode */
                   CPU_WORKCLOCK(200);
   
                 /* VM86 && IOPL < 3 && interrupt cause == INTn */                  VERBOSE(("interrupt: -------------------------------------------------------------- start"));
                 if (CPU_STAT_VM86 && (CPU_STAT_IOPL < CPU_IOPL3) && (softintp == -1)) {                  VERBOSE(("interrupt: old EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_EIP, CPU_SS, CPU_ESP));
                         VERBOSE(("interrupt: VM86 && IOPL < 3 && INTn"));  
                         EXCEPTION(GP_EXCEPTION, 0);  #if defined(DEBUG)
                   if (num == 0x80) {
                           /* Linux, FreeBSD, NetBSD, OpenBSD system call */
                           VERBOSE(("interrupt: syscall# = %d\n%s", CPU_EAX, cpu_reg2str()));
                 }                  }
   #endif
   
                 idt_idx = num * 8;                  idt_idx = num * 8;
                   exc_errcode = idt_idx + 2;
                   if (intrtype == INTR_TYPE_EXTINTR)
                           exc_errcode++;
   
                 if (idt_idx + 7 > CPU_IDTR_LIMIT) {                  if (idt_idx + 7 > CPU_IDTR_LIMIT) {
                         VERBOSE(("interrupt: IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT));                          VERBOSE(("interrupt: IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT));
                         EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                 }                  }
   
                 memset(&gd, 0, sizeof(gd));                  /* load a gate descriptor from interrupt descriptor table */
                 CPU_SET_GATEDESC(&gd, CPU_IDTR_BASE + idt_idx, CPU_MODE_SUPERVISER);                  memset(&gsd, 0, sizeof(gsd));
                 if (!gd.valid || !gd.p) {                  load_descriptor(&gsd, CPU_IDTR_BASE + idt_idx);
                   if (!SEG_IS_VALID(&gsd)) {
                         VERBOSE(("interrupt: gate descripter is invalid."));                          VERBOSE(("interrupt: gate descripter is invalid."));
                         EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                   }
                   if (!SEG_IS_SYSTEM(&gsd)) {
                           VERBOSE(("interrupt: gate descriptor is not system segment."));
                           EXCEPTION(GP_EXCEPTION, exc_errcode);
                 }                  }
   
                 switch (gd.type) {                  switch (gsd.type) {
                 case CPU_SYSDESC_TYPE_TASK:                  case CPU_SYSDESC_TYPE_TASK:
                 case CPU_SYSDESC_TYPE_INTR_16:                  case CPU_SYSDESC_TYPE_INTR_16:
                 case CPU_SYSDESC_TYPE_INTR_32:                  case CPU_SYSDESC_TYPE_INTR_32:
Line 279  interrupt(int num, int softintp, int err Line 292  interrupt(int num, int softintp, int err
                         break;                          break;
   
                 default:                  default:
                         VERBOSE(("interrupt: invalid gate type (%d)", gd.type));                          VERBOSE(("interrupt: invalid gate type (%d)", gsd.type));
                         EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                         break;                          break;
                 }                  }
   
                 /* 5.10.1.1. 例外/割り込みハンドラ・プロシージャの保護 */                  /* 5.10.1.1. 例外/割り込みハンドラ・プロシージャの保護 */
                 if (softintp && (gd.dpl < CPU_STAT_CPL)) {                  if ((intrtype != INTR_TYPE_EXTINTR) && (gsd.dpl < CPU_STAT_CPL)) {
                         VERBOSE(("interrupt: softintp && DPL(%d) < CPL(%d)", gd.dpl, CPU_STAT_CPL));                          VERBOSE(("interrupt: intrtype(%d) && DPL(%d) < CPL(%d)", intrtype, gsd.dpl, CPU_STAT_CPL));
                         EXCEPTION(GP_EXCEPTION, num * 8 | 2);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                   }
   
                   if (!SEG_IS_PRESENT(&gsd)) {
                           VERBOSE(("interrupt: gate descriptor is not present."));
                           EXCEPTION(NP_EXCEPTION, exc_errcode);
                   }
   
                   if ((intrtype == INTR_TYPE_EXTINTR) && CPU_STAT_HLT) {
                           VERBOSE(("interrupt: reset HTL in protected mode"));
                           CPU_EIP++;
                           CPU_STAT_HLT = 0;
                 }                  }
   
                 switch (gd.type) {                  switch (gsd.type) {
                 case CPU_SYSDESC_TYPE_TASK:                  case CPU_SYSDESC_TYPE_TASK:
                         interrupt_task(&gd, softintp, errorp, error_code);                          interrupt_task_gate(&gsd, intrtype, errorp, error_code);
                         break;                          break;
   
                 case CPU_SYSDESC_TYPE_INTR_16:                  case CPU_SYSDESC_TYPE_INTR_16:
                 case CPU_SYSDESC_TYPE_INTR_32:                  case CPU_SYSDESC_TYPE_INTR_32:
                 case CPU_SYSDESC_TYPE_TRAP_16:                  case CPU_SYSDESC_TYPE_TRAP_16:
                 case CPU_SYSDESC_TYPE_TRAP_32:                  case CPU_SYSDESC_TYPE_TRAP_32:
                         interrupt_intr_or_trap(&gd, softintp, errorp, error_code);                          interrupt_intr_or_trap(&gsd, intrtype, errorp, error_code);
                         break;                          break;
   
                 default:                  default:
                         EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                         break;                          break;
                 }                  }
   
                   VERBOSE(("interrupt: ---------------------------------------------------------------- end"));
         }          }
   
           CPU_CLEAR_PREV_ESP();
 }  }
   
 static void  static void
 interrupt_task(descriptor_t *gdp, int softintp, int errorp, int error_code)  interrupt_task_gate(const descriptor_t *gsdp, int intrtype, int errorp, int error_code)
 {  {
         selector_t task_sel;          selector_t task_sel;
         int rv;          int rv;
   
         VERBOSE(("interrupt: TASK-GATE"));          VERBOSE(("interrupt: TASK-GATE"));
   
         (void)softintp;          rv = parse_selector(&task_sel, gsdp->u.gate.selector);
           if (rv < 0 || task_sel.ldt || !SEG_IS_SYSTEM(&task_sel.desc)) {
         rv = parse_selector_sv(&task_sel, gdp->u.gate.selector);                  VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d, %cDT, type = %s)", gsdp->u.gate.selector, rv, task_sel.ldt ? 'L' : 'G', task_sel.desc.s ? "code/data" : "system"));
         if (rv < 0 || task_sel.ldt) {  
                 VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d, %cDT)", gdp->u.gate.selector, rv, task_sel.ldt ? 'L' : 'G'));  
                 EXCEPTION(TS_EXCEPTION, task_sel.idx);                  EXCEPTION(TS_EXCEPTION, task_sel.idx);
         }          }
   
Line 349  interrupt_task(descriptor_t *gdp, int so Line 375  interrupt_task(descriptor_t *gdp, int so
   
         task_switch(&task_sel, TASK_SWITCH_INTR);          task_switch(&task_sel, TASK_SWITCH_INTR);
   
           CPU_SET_PREV_ESP();
   
         if (errorp) {          if (errorp) {
                 XPUSH0(error_code);                  XPUSH0(error_code);
         }          }
   
           /* out of range */
           if (CPU_EIP > CPU_STAT_CS_LIMIT) {
                   VERBOSE(("interrupt: new_ip is out of range. new_ip = %08x, limit = %08x", CPU_EIP, CPU_STAT_CS_LIMIT));
                   EXCEPTION(GP_EXCEPTION, 0);
           }
 }  }
   
 static void  static void
 interrupt_intr_or_trap(descriptor_t *gdp, int softintp, int errorp, int error_code)  interrupt_intr_or_trap(const descriptor_t *gsdp, int intrtype, int errorp, int error_code)
 {  {
         selector_t intr_sel, ss_sel;          selector_t cs_sel, ss_sel;
         DWORD old_flags = REAL_EFLAGREG;          UINT stacksize;
         DWORD flags = REAL_EFLAGREG;          UINT32 old_flags;
         DWORD mask = 0;          UINT32 new_flags;
         DWORD stacksize;          UINT32 mask;
         DWORD new_ip, new_sp;          UINT32 sp;
         DWORD old_ip, old_sp;          UINT32 new_ip, new_sp;
         WORD old_cs, old_ss, new_ss;          UINT32 old_ip, old_sp;
           UINT16 old_cs, old_ss, new_ss;
           int exc_errcode;
         int rv;           int rv; 
   
         VERBOSE(("interrupt: old EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_PREV_EIP, CPU_SS, CPU_ESP));          new_ip = gsdp->u.gate.offset;
   
         new_ip = gdp->u.gate.offset;  
         old_ss = CPU_SS;          old_ss = CPU_SS;
         old_cs = CPU_CS;          old_cs = CPU_CS;
         old_ip = CPU_EIP;          old_ip = CPU_EIP;
         old_sp = CPU_ESP;          old_sp = CPU_ESP;
           old_flags = REAL_EFLAGREG;
           new_flags = REAL_EFLAGREG & ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG);
           mask = T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG;
   
         switch (gdp->type) {          switch (gsdp->type) {
         case CPU_SYSDESC_TYPE_INTR_16:          case CPU_SYSDESC_TYPE_INTR_16:
                 old_ip &= 0xffff;  
                 old_sp &= 0xffff;  
                 /*FALLTHROUGH*/  
         case CPU_SYSDESC_TYPE_INTR_32:          case CPU_SYSDESC_TYPE_INTR_32:
                 VERBOSE(("interrupt: INTERRUPT-GATE"));                  VERBOSE(("interrupt: INTERRUPT-GATE"));
                 flags &= ~I_FLAG;                  new_flags &= ~I_FLAG;
                 mask |= I_FLAG;                  mask |= I_FLAG;
                 break;                  break;
   
         case CPU_SYSDESC_TYPE_TRAP_16:          case CPU_SYSDESC_TYPE_TRAP_16:
                 old_ip &= 0xffff;  
                 old_sp &= 0xffff;  
                 /*FALLTHROUGH*/  
         case CPU_SYSDESC_TYPE_TRAP_32:          case CPU_SYSDESC_TYPE_TRAP_32:
                 VERBOSE(("interrupt: TRAP-GATE"));                  VERBOSE(("interrupt: TRAP-GATE"));
                 break;                  break;
   
           default:
                   ia32_panic("interrupt: gate descriptor type is invalid (type = %d)", gsdp->type);
                   break;
         }          }
         flags &= ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG);  
         mask |= T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG;  
   
         rv = parse_selector_sv(&intr_sel, gdp->u.gate.selector);          exc_errcode = gsdp->u.gate.selector & ~3;
           if (intrtype == INTR_TYPE_EXTINTR)
                   exc_errcode++;
   
           rv = parse_selector(&cs_sel, gsdp->u.gate.selector);
         if (rv < 0) {          if (rv < 0) {
                 VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", gdp->u.gate.selector, rv));                  VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", gsdp->u.gate.selector, rv));
                 EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp);                  EXCEPTION(GP_EXCEPTION, exc_errcode);
         }          }
   
         /* check segment type */          /* check segment type */
         if (!intr_sel.desc.s) {          if (SEG_IS_SYSTEM(&cs_sel.desc)) {
                 VERBOSE(("interrupt: code segment is system segment"));                  VERBOSE(("interrupt: code segment is system segment"));
                 EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp);                  EXCEPTION(GP_EXCEPTION, exc_errcode);
         }          }
         if (!intr_sel.desc.u.seg.c) {          if (SEG_IS_DATA(&cs_sel.desc)) {
                 VERBOSE(("interrupt: code segment is data segment"));                  VERBOSE(("interrupt: code segment is data segment"));
                 EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp);                  EXCEPTION(GP_EXCEPTION, exc_errcode);
         }          }
   
         /* check privilege level */          /* check privilege level */
         if (intr_sel.desc.dpl > CPU_STAT_CPL) {          if (cs_sel.desc.dpl > CPU_STAT_CPL) {
                 VERBOSE(("interrupt: DPL(%d) > CPL(%d)", intr_sel.desc.dpl, CPU_STAT_CPL));                  VERBOSE(("interrupt: DPL(%d) > CPL(%d)", cs_sel.desc.dpl, CPU_STAT_CPL));
                 EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp);                  EXCEPTION(GP_EXCEPTION, exc_errcode);
         }          }
   
         /* not present */          /* not present */
         if (selector_is_not_present(&intr_sel)) {          if (selector_is_not_present(&cs_sel)) {
                 VERBOSE(("interrupt: selector is not present"));                  VERBOSE(("interrupt: selector is not present"));
                 EXCEPTION(NP_EXCEPTION, intr_sel.idx | !softintp);                  EXCEPTION(NP_EXCEPTION, exc_errcode);
         }          }
   
         if (!intr_sel.desc.u.seg.ec          if (!SEG_IS_CONFORMING_CODE(&cs_sel.desc) && (cs_sel.desc.dpl < CPU_STAT_CPL)) {
          && (intr_sel.desc.dpl < CPU_STAT_CPL)) {                  stacksize = errorp ? 12 : 10;
                 if (!CPU_STAT_VM86) {                  if (!CPU_STAT_VM86) {
                         VERBOSE(("interrupt: INTER-PRIVILEGE-LEVEL-INTERRUPT"));                          VERBOSE(("interrupt: INTER-PRIVILEGE-LEVEL-INTERRUPT"));
                         stacksize = errorp ? 12 : 10;  
                 } else {                  } else {
                         /* VM86 */                          /* VM86 */
                         if (intr_sel.desc.dpl != 0) {                          VERBOSE(("interrupt: INTERRUPT-FROM-VIRTUAL-8086-MODE"));
                           if (cs_sel.desc.dpl != 0) {
                                 /* 16.3.1.1 */                                  /* 16.3.1.1 */
                                 VERBOSE(("interrupt: DPL[CS](%d) != 0", intr_sel.desc.dpl));                                  VERBOSE(("interrupt: DPL[CS](%d) != 0", cs_sel.desc.dpl));
                                 EXCEPTION(GP_EXCEPTION, intr_sel.idx);                                  EXCEPTION(GP_EXCEPTION, exc_errcode);
                         }                          }
                         VERBOSE(("interrupt: INTERRUPT-FROM-VIRTUAL-8086-MODE"));                          stacksize += 8;
                         stacksize = errorp ? 20 : 18;  
                 }                  }
                 switch (gdp->type) {                  if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) {
                 case CPU_SYSDESC_TYPE_INTR_32:  
                 case CPU_SYSDESC_TYPE_TRAP_32:  
                         stacksize *= 2;                          stacksize *= 2;
                         break;  
                 }                  }
   
                 get_stack_from_tss(intr_sel.desc.dpl, &new_ss, &new_sp);                  /* get stack pointer from TSS */
                   get_stack_pointer_from_tss(cs_sel.desc.dpl, &new_ss, &new_sp);
   
                   /* parse stack segment descriptor */
                   rv = parse_selector(&ss_sel, new_ss);
   
                   /* update exception error code */
                   exc_errcode = ss_sel.idx;
                   if (intrtype == INTR_TYPE_EXTINTR)
                           exc_errcode++;
   
                 rv = parse_selector_sv(&ss_sel, new_ss);  
                 if (rv < 0) {                  if (rv < 0) {
                         VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", new_ss, rv));                          VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", new_ss, rv));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
   
                 /* check privilege level */                  /* check privilege level */
                 if (ss_sel.rpl != intr_sel.desc.dpl) {                  if (ss_sel.rpl != cs_sel.desc.dpl) {
                         VERBOSE(("interrupt: RPL[SS](%d) != DPL[CS](%d)", ss_sel.rpl, intr_sel.desc.dpl));                          VERBOSE(("interrupt: selector RPL[SS](%d) != DPL[CS](%d)", ss_sel.rpl, cs_sel.desc.dpl));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
                 if (ss_sel.desc.dpl != intr_sel.desc.dpl) {                  if (ss_sel.desc.dpl != cs_sel.desc.dpl) {
                         VERBOSE(("interrupt: DPL[SS](%d) != DPL[CS](%d)", ss_sel.desc.dpl, intr_sel.desc.dpl));                          VERBOSE(("interrupt: descriptor DPL[SS](%d) != DPL[CS](%d)", ss_sel.desc.dpl, cs_sel.desc.dpl));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
   
                 /* check segment type */                  /* stack segment must be writable data segment. */
                 if (!ss_sel.desc.s) {                  if (SEG_IS_SYSTEM(&ss_sel.desc)) {
                         VERBOSE(("interrupt: stack segment is system segment"));                          VERBOSE(("interrupt: stack segment is system segment"));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
                 if (ss_sel.desc.u.seg.c) {                  if (SEG_IS_CODE(&ss_sel.desc)) {
                         VERBOSE(("interrupt: stack segment is code segment"));                          VERBOSE(("interrupt: stack segment is code segment"));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
                 if (!ss_sel.desc.u.seg.wr) {                  if (!SEG_IS_WRITABLE_DATA(&ss_sel.desc)) {
                         VERBOSE(("interrupt: stack segment is read-only data segment"));                          VERBOSE(("interrupt: stack segment is read-only data segment"));
                         EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(TS_EXCEPTION, exc_errcode);
                 }                  }
   
                 /* not present */                  /* not present */
                 if (selector_is_not_present(&ss_sel)) {                  if (selector_is_not_present(&ss_sel)) {
                         VERBOSE(("interrupt: selector is not present"));                          VERBOSE(("interrupt: selector is not present"));
                         EXCEPTION(SS_EXCEPTION, ss_sel.idx | !softintp);                          EXCEPTION(SS_EXCEPTION, exc_errcode);
                 }                  }
   
                 /* check stack room size */                  /* check stack room size */
                 CHECK_STACK_PUSH(&ss_sel.desc, new_sp, stacksize);                  cpu_stack_push_check(ss_sel.idx, &ss_sel.desc, new_sp, stacksize);
   
                 /* out of range */                  /* out of range */
                 if (new_ip > intr_sel.desc.u.seg.limit) {                  if (new_ip > cs_sel.desc.u.seg.limit) {
                         VERBOSE(("interrupt: new_ip is out of range. new_ip = %08x, limit = %08x", new_ip, intr_sel.desc.u.seg.limit));                          VERBOSE(("interrupt: new_ip is out of range. new_ip = %08x, limit = %08x", new_ip, cs_sel.desc.u.seg.limit));
                         EXCEPTION(GP_EXCEPTION, 0);                          EXCEPTION(GP_EXCEPTION, 0);
                 }                  }
   
                 load_ss(ss_sel.selector, &ss_sel.desc, intr_sel.desc.dpl);                  load_ss(ss_sel.selector, &ss_sel.desc, cs_sel.desc.dpl);
                 CPU_ESP = new_sp;                  CPU_ESP = new_sp;
   
                 load_cs(intr_sel.selector, &intr_sel.desc, intr_sel.desc.dpl);                  load_cs(cs_sel.selector, &cs_sel.desc, cs_sel.desc.dpl);
                 SET_EIP(new_ip);                  CPU_EIP = new_ip;
   
                 if (CPU_STAT_VM86) {                  if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) {
                         switch (gdp->type) {                          if (CPU_STAT_VM86) {
                         case CPU_SYSDESC_TYPE_INTR_32:  
                         case CPU_SYSDESC_TYPE_TRAP_32:  
                                 PUSH0_32(CPU_GS);                                  PUSH0_32(CPU_GS);
                                 PUSH0_32(CPU_FS);                                  PUSH0_32(CPU_FS);
                                 PUSH0_32(CPU_DS);                                  PUSH0_32(CPU_DS);
                                 PUSH0_32(CPU_ES);                                  PUSH0_32(CPU_ES);
                                 break;  
   
                         case CPU_SYSDESC_TYPE_INTR_16:                                  LOAD_SEGREG(CPU_GS_INDEX, 0);
                         case CPU_SYSDESC_TYPE_TRAP_16:                                  CPU_STAT_SREG(CPU_GS_INDEX).valid = 0;
                                 ia32_panic("interrupt: 16bit gate");                                  LOAD_SEGREG(CPU_FS_INDEX, 0);
                                 break;                                  CPU_STAT_SREG(CPU_FS_INDEX).valid = 0;
                                   LOAD_SEGREG(CPU_DS_INDEX, 0);
                                   CPU_STAT_SREG(CPU_DS_INDEX).valid = 0;
                                   LOAD_SEGREG(CPU_ES_INDEX, 0);
                                   CPU_STAT_SREG(CPU_ES_INDEX).valid = 0;
                         }                          }
   
                         CPU_SET_SEGREG(CPU_GS_INDEX, 0);  
                         CPU_SET_SEGREG(CPU_FS_INDEX, 0);  
                         CPU_SET_SEGREG(CPU_DS_INDEX, 0);  
                         CPU_SET_SEGREG(CPU_ES_INDEX, 0);  
                 }  
   
                 switch (gdp->type) {  
                 case CPU_SYSDESC_TYPE_INTR_32:  
                 case CPU_SYSDESC_TYPE_TRAP_32:  
                         PUSH0_32(old_ss);                          PUSH0_32(old_ss);
                         PUSH0_32(old_sp);                          PUSH0_32(old_sp);
                         PUSH0_32(old_flags);                          PUSH0_32(old_flags);
Line 533  interrupt_intr_or_trap(descriptor_t *gdp Line 565  interrupt_intr_or_trap(descriptor_t *gdp
                         if (errorp) {                          if (errorp) {
                                 PUSH0_32(error_code);                                  PUSH0_32(error_code);
                         }                          }
                         break;                  } else {
                           if (CPU_STAT_VM86) {
                 case CPU_SYSDESC_TYPE_INTR_16:                                  ia32_panic("interrupt: 16bit gate && VM86");
                 case CPU_SYSDESC_TYPE_TRAP_16:                          }
                         PUSH0_16(old_ss);                          PUSH0_16(old_ss);
                         PUSH0_16(old_sp);                          PUSH0_16(old_sp);
                         PUSH0_16(old_flags);                          PUSH0_16(old_flags);
Line 545  interrupt_intr_or_trap(descriptor_t *gdp Line 577  interrupt_intr_or_trap(descriptor_t *gdp
                         if (errorp) {                          if (errorp) {
                                 PUSH0_16(error_code);                                  PUSH0_16(error_code);
                         }                          }
                         break;  
                 }                  }
   
                 set_eflags(flags, mask);  
         } else {          } else {
                 if (CPU_STAT_VM86) {                  if (CPU_STAT_VM86) {
                         VERBOSE(("interrupt: VM86"));                          VERBOSE(("interrupt: VM86"));
                         EXCEPTION(GP_EXCEPTION, intr_sel.idx);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                 }                  }
                 if (!intr_sel.desc.u.seg.ec && (intr_sel.desc.dpl != CPU_STAT_CPL)) {                  if (!SEG_IS_CONFORMING_CODE(&cs_sel.desc) && (cs_sel.desc.dpl != CPU_STAT_CPL)) {
                         VERBOSE(("interrupt: NON-CONFORMING-CODE-SEGMENT(%s) and DPL[CS](%d) != CPL", intr_sel.desc.u.seg.ec ? "false" : "true", intr_sel.desc.dpl, CPU_STAT_CPL));                          VERBOSE(("interrupt: %sCONFORMING-CODE-SEGMENT(%s) && DPL[CS](%d) != CPL", SEG_IS_CONFORMING_CODE(&cs_sel.desc) ? "" : "NON-", cs_sel.desc.dpl, CPU_STAT_CPL));
                         EXCEPTION(GP_EXCEPTION, intr_sel.idx);                          EXCEPTION(GP_EXCEPTION, exc_errcode);
                 }                  }
                 VERBOSE(("interrupt: INTRA-PRIVILEGE-LEVEL-INTERRUPT"));  
   
                 if (CPU_STAT_SS32) {                  VERBOSE(("interrupt: INTRA-PRIVILEGE-LEVEL-INTERRUPT"));
                         new_sp = CPU_ESP;  
                 } else {  
                         new_sp = CPU_SP;  
                 }  
   
                 stacksize = errorp ? 8 : 6;                  stacksize = errorp ? 8 : 6;
                 switch (gdp->type) {                  if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) {
                 case CPU_SYSDESC_TYPE_INTR_32:  
                 case CPU_SYSDESC_TYPE_TRAP_32:  
                         stacksize *= 2;                          stacksize *= 2;
                         break;  
                 }                  }
                 CHECK_STACK_PUSH(&CPU_STAT_SREG(CPU_SS_INDEX), new_sp, stacksize);  
                   /* check stack room size */
                   if (CPU_STAT_SS32) {
                           sp = CPU_ESP;
                   } else {
                           sp = CPU_SP;
                   }
                   SS_PUSH_CHECK(sp, stacksize);
   
                 /* out of range */                  /* out of range */
                 if (new_ip > intr_sel.desc.u.seg.limit) {                  if (new_ip > cs_sel.desc.u.seg.limit) {
                         VERBOSE(("interrupt: new_ip is out of range. new_ip = %08x, limit = %08x", new_ip, intr_sel.desc.u.seg.limit));                          VERBOSE(("interrupt: new_ip is out of range. new_ip = %08x, limit = %08x", new_ip, cs_sel.desc.u.seg.limit));
                         EXCEPTION(GP_EXCEPTION, 0);                          EXCEPTION(GP_EXCEPTION, 0);
                 }                  }
   
                 switch (gdp->type) {                  load_cs(cs_sel.selector, &cs_sel.desc, CPU_STAT_CPL);
                 case CPU_SYSDESC_TYPE_INTR_32:                  CPU_EIP = new_ip;
                 case CPU_SYSDESC_TYPE_TRAP_32:  
                         PUSH0_32(REAL_EFLAGREG);                  if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) {
                         PUSH0_32(CPU_CS);                          PUSH0_32(old_flags);
                         PUSH0_32(CPU_EIP);                          PUSH0_32(old_cs);
                           PUSH0_32(old_ip);
                         if (errorp) {                          if (errorp) {
                                 PUSH0_32(error_code);                                  PUSH0_32(error_code);
                         }                          }
                         break;                  } else {
                           PUSH0_16(old_flags);
                 case CPU_SYSDESC_TYPE_INTR_16:                          PUSH0_16(old_cs);
                 case CPU_SYSDESC_TYPE_TRAP_16:                          PUSH0_16(old_ip);
                         PUSH0_16(REAL_FLAGREG);  
                         PUSH0_16(CPU_CS);  
                         PUSH0_16(CPU_IP);  
                         if (errorp) {                          if (errorp) {
                                 PUSH0_16(error_code);                                  PUSH0_16(error_code);
                         }                          }
                         break;  
                 }                  }
   
                 load_cs(intr_sel.selector, &intr_sel.desc, CPU_STAT_CPL);  
                 SET_EIP(new_ip);  
   
                 set_eflags(flags, mask);  
         }          }
           set_eflags(new_flags, mask);
   
         VERBOSE(("interrupt: new EIP = %04x:%08x, new ESP = %04x:%08x", CPU_CS, CPU_EIP, CPU_SS, CPU_ESP));          VERBOSE(("interrupt: new EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_EIP, CPU_SS, CPU_ESP));
 }  }

Removed from v.1.7  
changed lines
  Added in v.1.28


RetroPC.NET-CVS <cvs@retropc.net>