--- np2/i386c/ia32/exception.c 2003/12/08 00:55:31 1.1 +++ np2/i386c/ia32/exception.c 2011/12/21 18:07:57 1.30 @@ -1,5 +1,3 @@ -/* $Id: exception.c,v 1.1 2003/12/08 00:55:31 yui Exp $ */ - /* * Copyright (c) 2003 NONAKA Kimihiro * All rights reserved. @@ -12,8 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * 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 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -31,42 +27,28 @@ #include "cpu.h" #include "ia32.mcr" -void -memory_dump(int idx, DWORD madr) -{ - DWORD addr; - size_t size; - unsigned char buf[16]; - size_t s, i; - BYTE p; - - if (madr < 0x80) { - size = madr + 0x80; - addr = 0; - } else { - size = 0x100; - addr = madr - 0x80; - } - printf("memory dump\n-- \n"); - for (s = 0; s < size; s++) { - if ((s % 16) == 0) { - printf("%08x: ", addr + s); - memset(buf, '.', sizeof(buf)); - } - - p = cpu_vmemoryread(idx, addr + s); - printf("%02x ", p); - if (p >= 0x20 && p <= 0x7e) - buf[s % 16] = p; - - if ((s % 16) == 15) { - printf("| "); - for (i = 0; i < sizeof(buf); i++) - printf("%c", buf[i]); - printf("\n"); - } - } -} +const char *exception_str[EXCEPTION_NUM] = { + "DE_EXCEPTION", + "DB_EXCEPTION", + "NMI_EXCEPTION", + "BP_EXCEPTION", + "OF_EXCEPTION", + "BR_EXCEPTION", + "UD_EXCEPTION", + "NM_EXCEPTION", + "DF_EXCEPTION", + "CoProcesser Segment Overrun", + "TS_EXCEPTION", + "NP_EXCEPTION", + "SS_EXCEPTION", + "GP_EXCEPTION", + "PF_EXCEPTION", + "Reserved", + "MF_EXCEPTION", + "AC_EXCEPTION", + "MC_EXCEPTION", + "XF_EXCEPTION", +}; static const int exctype[EXCEPTION_NUM] = { 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, @@ -84,196 +66,224 @@ exception(int num, int error_code) { int errorp = 0; - VERBOSE(("exception: num = 0x%02x, error_code = %x", num, error_code)); + __ASSERT((unsigned int)num < EXCEPTION_NUM); - CPU_STAT_NERROR++; - if ((CPU_STAT_NERROR >= 3) - || (CPU_STAT_NERROR == 2 && CPU_STAT_PREV_EXCEPTION == DF_EXCEPTION)) { +#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(("%s", cpu_reg2str())); + + CPU_STAT_EXCEPTION_COUNTER_INC(); + if ((CPU_STAT_EXCEPTION_COUNTER >= 3) + || (CPU_STAT_EXCEPTION_COUNTER == 2 && CPU_STAT_PREV_EXCEPTION == DF_EXCEPTION)) { /* Triple fault */ ia32_panic("exception: catch triple fault!"); } switch (num) { - case DE_EXCEPTION: /* (F) ¥¹¥µ¡¦¥£¡¦ò£¥· */ - case BR_EXCEPTION: /* (F) BOUND ¡¢¥Û¥Í¥Þ¡¼¥Þ¥¦¡¼ */ - case UD_EXCEPTION: /* (F) ¥Õ¥ª¥¯ù饧¡¦¥ì¡¦¥¦¡£¥·¡¦¥Î */ - case NM_EXCEPTION: /* (F) ¡¦¥Ì¡¦¥ß¡¦¡¢¡¦¥±¥µ¥Í¥Ø¥à¥Î¥ä¥¤¥È (FPU ¡¢¥ã¥Õ¥ª¡¢¡¢) */ + case DE_EXCEPTION: /* (F) ñù¡¢î°Î太å㥥å㥷 */ + case DB_EXCEPTION: /* (F/T) åã®å¥ðåã¦å¤² */ + case BR_EXCEPTION: /* (F) BOUND åá¥ççÖ´ê¦*/ + case UD_EXCEPTION: /* (F) ̵¸ú¥ª¥Ú¥³¡¼¥É */ + case NM_EXCEPTION: /* (F) ¥Ç¥Ð¥¤¥¹»ÈÍÑÉÔ²Ä (FPU ¤¬Ìµ¤¤) */ + case MF_EXCEPTION: /* (F) ÉâÆ°¾®¿ôÅÀ¥¨¥é¡¼ */ CPU_EIP = CPU_PREV_EIP; + if (CPU_STATSAVE.cpu_stat.backout_sp) + CPU_ESP = CPU_PREV_ESP; /*FALLTHROUGH*/ - case DB_EXCEPTION: /* (F/T) ¥Ç¥Ð¥Ã¥° */ - case NMI_EXCEPTION: /* (I) NMI ³ä¤ê¹þ¤ß */ - case BP_EXCEPTION: /* (T) ¥Ö¥ì¡¼¥¯¥Ý¥¤¥ó¥È */ - case OF_EXCEPTION: /* (T) ¥ª¡¼¥Ð¡¼¥Õ¥í¡¼ */ + case NMI_EXCEPTION: /* (I) NMI ³ä¤ê¹þ¤ß */ + case BP_EXCEPTION: /* (T) ¥Ö¥ì¡¼¥¯¥Ý¥¤¥ó¥È */ + case OF_EXCEPTION: /* (T) ¥ª¡¼¥Ð¡¼¥Õ¥í¡¼ */ errorp = 0; break; - case DF_EXCEPTION: /* (A) ¡¦¥¿¡¦¥è¡¦ö§¥æ¡¦¥¥¡¦ö§¥Í (errcode: 0) */ + case DF_EXCEPTION: /* (A) åãc«ã©ã (errcode: 0) */ errorp = 1; error_code = 0; break; - case AC_EXCEPTION: /* (F) ¥¢¥é¥¤¥ó¥á¥ó¥È¥Á¥§¥Ã¥¯ (errcode: 0) */ + case AC_EXCEPTION: /* (F) åâ¡Öå㥥åâ¡¢å㥦åã¡£å㥦åã°å¥áå⥡åã¦å¤± (errcode: 0) */ error_code = 0; /*FALLTHROUGH*/ - case TS_EXCEPTION: /* (F) ¥Õ¥ª¥¯TSS (errcode) */ - case NP_EXCEPTION: /* (F) ¡¦¥µ¡¦¡¼¡¦â§ó¥ÈÉÔº(errcode) */ - case SS_EXCEPTION: /* (F) ¥¹¥¿¥Ã¥¯¥»¥°¥á¥ó¥È¥Õ¥©¥ë¥È (errcode) */ - case GP_EXCEPTION: /* (F) °ìÈÌÊݸîÎã³° (errcode) */ - case PF_EXCEPTION: /* (F) ¥Ú¡¼¥¸¥Õ¥©¥ë¥È (errcode) */ + case TS_EXCEPTION: /* (F) ̵¸ú TSS (errcode) */ + case NP_EXCEPTION: /* (F) ¥»¥°¥á¥ó¥ÈÉG (errcode) */ + case SS_EXCEPTION: /* (F) ¥¹¥¿¥Ã¥¯¥»¥°¥á¥ó¥È¥Õ¥©¥ë¥È (errcode) */ + case GP_EXCEPTION: /* (F) °ìÈÌÊݸîÎã³° (errcode) */ + case PF_EXCEPTION: /* (F) ¥Ú¡¼¥¸¥Õ¥©¥ë¥È (errcode) */ CPU_EIP = CPU_PREV_EIP; + if (CPU_STATSAVE.cpu_stat.backout_sp) + CPU_ESP = CPU_PREV_ESP; errorp = 1; break; - case MF_EXCEPTION: /* (F) ÉâÆ°¾®¿ôÅÀ¥¨¥é¡¼ */ - CPU_EIP = CPU_PREV_EIP; - errorp = 0; - break; - - case MC_EXCEPTION: /* (A) ¥Þ¥·¥ó¥Á¥§¥Ã¥¯ */ - CPU_EIP = CPU_PREV_EIP; - errorp = 0; - break; - - case XF_EXCEPTION: /* (F) ¥¹¥È¥ê¡¼¥ß¥ó¥° SIMD ³Èĥ̿Îá */ - CPU_EIP = CPU_PREV_EIP; - errorp = 0; - break; - default: - ia32_panic("exception(): unknown exception (%d)", num); + ia32_panic("exception: unknown exception (%d)", num); break; } - if (CPU_STAT_NERROR >= 2) { + if (CPU_STAT_EXCEPTION_COUNTER >= 2) { if (dftable[exctype[CPU_STAT_PREV_EXCEPTION]][exctype[num]]) { num = DF_EXCEPTION; + errorp = 1; + error_code = 0; } } CPU_STAT_PREV_EXCEPTION = num; - INTERRUPT(num, FALSE, errorp, error_code); - CPU_STAT_NERROR = 0; + 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); } /* - * ¥³¡¼¥ë¡¦¥²¡¼¥È¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿ + * å⥦å㥷å㥩å㥵å⥤å㥷åã°å¦½åã®å¤¥å⥱åâ¥Ãå㥧åãÎå¤Á * * 31 16 15 14 13 12 8 7 5 4 0 * +------------------------------------+--+-----+----------+-----+---------+ - * | ¡¦¥§¡¦¥æ¡¦¥µ¡¦¥Æ¡¦¥Í 31..16 | P| DPL | 0 1 1 0 0|0 0 0|¡¦¥©¡¦¥ò¡¦ó¥| 4 + * | ¥ª¥Õ¥»¥Ã¥È 31..16 | P| DPL | 0 D 1 0 0|0 0 0|¥«¥¦¥ó¥È | 4 * +------------------------------------+--+-----+----------+-----+---------+ * 31 16 15 0 * +------------------------------------+-----------------------------------+ - * | ¥»¥°¥á¥ó¥È¡¦¥»¥ì¥¯¥¿ | ¥ª¥Õ¥»¥Ã¥È 15..0 | 0 + * | å⥵å⡼åã¡£å㥦åã°å¦½å⥵åã¥ãåâ¥Ãå⥽ | å⥧åãÊ夽åã¦å¥è 15..0 | 0 * +------------------------------------+-----------------------------------+ */ /* - * ¥¦è¦ô»¡¬¡¦¥Ì¡¦¡×¡¦¥±¡¦¥Ã¡¦ô§¥é¡¦¥½ + * é饤åâ´ê¥»¥·å᥽åã®å¤¥å⥱åâ¥Ãå㥧åãÎå¤Á *-- - * ¡¦¥½¡¦¥±¡¦¥Ã¡£¥ò¡¦¥¤¡£¥·¡¦¥Í + * å⥽å⥱åâ¥Ãå㥵å⥤å㥷å㊠* * 31 16 15 14 13 12 8 7 0 * +------------------------------------+--+-----+----------+---------------+ - * | Reserved | P| DPL | 0 D 1 0 1| Reserved | 4 + * | Reserved | P| DPL | 0 0 1 0 1| Reserved | 4 * +------------------------------------+--+-----+----------+---------------+ * 31 16 15 0 * +------------------------------------+-----------------------------------+ - * | TSS ¥»¥°¥á¥ó¥È¡¦¥»¥ì¥¯¥¿ | Reserved | 0 + * | TSS å⥵å⡼åã¡£å㥦åã°å¦½å⥵åã¥ãåâ¥Ãå⥽ | Reserved | 0 * +------------------------------------+-----------------------------------+ *-- - * ¥¦è¦ô»+ * ³ä¤ê¹þ¤ß¡¦¥²¡¼¥È * * 31 16 15 14 13 12 8 7 5 4 0 * +------------------------------------+--+-----+----------+-----+---------+ - * | ¡¦¥§¡¦¥æ¡¦¥µ¡¦¥Æ¡¦¥Í 31..16 | P| DPL | 0 D 1 1 0|0 0 0|Reserved | 4 + * | å⥧åãÊ夽åã¦å¥è 31..16 | P| DPL | 0 D 1 1 0|0 0 0|Reserved | 4 * +------------------------------------+--+-----+----------+-----+---------+ * 31 16 15 0 * +------------------------------------+-----------------------------------+ - * | ¡¦¥µ¡¦¡¼¡¦â§ó¥È¡¦¥»¥ì¥¯¥ | ¥ª¥Õ¥»¥Ã¥È 15..0 | 0 + * | å⥵å⡼åã¡£å㥦åã°å¦½å⥵åã¥ãåâ¥Ãå⥽ | å⥧åãÊ夽åã¦å¥è 15..0 | 0 * +------------------------------------+-----------------------------------+ *-- - * ¡¦¥Í¡¦ò§¥Æ¡¦¥é¡£¥ò¡¦¥¤¡£¥·¡¦¥Í + * åã°å¦«åã¦å¥÷å㥵å⥤å㥷å㊠* * 31 16 15 14 13 12 8 7 5 4 0 * +------------------------------------+--+-----+----------+-----+---------+ - * | ¥ª¥Õ¥»¥Ã¥È 31..16 | P| DPL | 0 D 1 1 1|0 0 0|Reserved | 4 + * | å⥧åãÊ夽åã¦å¥è 31..16 | P| DPL | 0 D 1 1 1|0 0 0|Reserved | 4 * +------------------------------------+--+-----+----------+-----+---------+ * 31 16 15 0 * +------------------------------------+-----------------------------------+ - * | ¡¦¥µ¡¦¡¼¡¦â§ü§¥Í¡£¥ò¡¦¥µ¡¦ø§¥Ã¡¦¥½ | ¡¦¥§¡¦¥æ¡¦¥µ¡¦¥Æ¡¦¥Í 15..0 | 0 + * | å⥵å⡼åã¡£å㥦åã°å¦½å⥵åã¥ãåâ¥Ãå⥽ | å⥧åãÊ夽åã¦å¥è 15..0 | 0 * +------------------------------------+-----------------------------------+ *-- - * DPL : ¡¦¥Ì¡¦¡×¡¦¥±¡¦¥Ã¡¦ô§¥é¡¦¥½¥Ë¥Æ¥¯¡Ö¡¦ø§¥ë¡¦- * ¥ª¥Õ¥»¥Ã¥È : ¥×¥í¥·¡¼¥¸¥ã¡¦¥¨¥ó¥È¥ê¡¦¥Ý¥¤¥ó¥È¤Þ¤Ç¤Î¥ª¥Õ¥»¥Ã¥È - * P : ¥»¥°¥á¥ó¥È¸ºß¥Õ¥é¥° - * ¥»¥ì¥¯¥¿ : ¥Ç¥£¥¹¥Æ¥£¥Í¡¼¥·¥ç¥ó¡¦¥³¡¼¥É¡¦¥»¥°¥á¥ó¥È¤Î¥»¥°¥á¥ó¥È¡¦¥»¥ì¥¯¥¿ - * D : ¥²¡¼¥È¤Î¥µ¥¤¥º¡¥0 = 16 bit, 1 = 32 bit + * DPL : ¥Ç¥£¥¹¥¯¥ê¥×¥¿ÆÃ¸¢¥ì¥Ù¥ë + * ¥ª¥Õ¥»¥Ã¥È : ¥×¥í¥·¡¼¥¸¥ã¡¦¥¨¥ó¥È¥ê¡¦¥Ý¥¤¥ó¥È¤Þ¤Ç¤Î¥ª¥Õ¥»¥Ã¥È + * P : ¥»¥°¥á¥ó¥È¸ºß¥Õ¥é¥° + * ¥»¥ì¥¯¥¿ : ¥Ç¥£¥¹¥Æ¥£¥Í¡¼¥·¥ç¥ó¡¦¥³¡¼¥É¡¦¥»¥°¥á¥ó¥È¤Î¥»¥°¥á¥ó¥È¡¦¥»¥ì¥¯¥¿ + * D : ¥²¡¼¥È¤Î¥µ¥¤¥º¡¥0 = 16 bit, 1 = 32 bit */ +static void interrupt_task_gate(const descriptor_t *gsdp, int intrtype, int errorp, int error_code); +static void interrupt_intr_or_trap(const descriptor_t *gsdp, int intrtype, int errorp, int error_code); + void -interrupt(int num, int softintp, int errorp, int error_code) +interrupt(int num, int intrtype, int errorp, int error_code) { - DWORD idt_idx; + descriptor_t gsd; + UINT idt_idx; + UINT32 new_ip; + 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) { /* real mode */ + CPU_WORKCLOCK(20); + idt_idx = num * 4; if (idt_idx + 3 > CPU_IDTR_LIMIT) { - EXCEPTION(GP_EXCEPTION, num * 4 | 2); + VERBOSE(("interrupt: real-mode IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT)); + EXCEPTION(GP_EXCEPTION, idt_idx + 2); } - if (!softintp) { - BYTE op = cpu_codefetch(CPU_EIP); - if (op == 0xf4) { /* hlt */ - CPU_EIP++; - } + if ((intrtype == INTR_TYPE_EXTINTR) && CPU_STAT_HLT) { + VERBOSE(("interrupt: reset HTL in real mode")); + CPU_EIP++; + CPU_STAT_HLT = 0; } REGPUSH0(REAL_FLAGREG); REGPUSH0(CPU_CS); REGPUSH0(CPU_IP); - if (softintp) { - CPU_EFLAG &= ~(T_FLAG | I_FLAG | AC_FLAG); - CPU_TRAP = 0; - } + CPU_EFLAG &= ~(T_FLAG | I_FLAG | AC_FLAG | RF_FLAG); + CPU_TRAP = 0; - CPU_EIP = cpu_memoryread_w(CPU_IDTR_BASE + num * 4); - CPU_CS = cpu_memoryread_w(CPU_IDTR_BASE + num * 4 + 2); - CPU_SET_SEGREG(CPU_CS_INDEX, CPU_CS); - CPU_WORKCLOCK(20); + new_ip = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx); + new_cs = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx + 2); + LOAD_SEGREG(CPU_CS_INDEX, new_cs); + CPU_EIP = new_ip; } else { /* protected mode */ - selector_t task_sel, intr_sel, ss_sel; - descriptor_t gd; - int rv; - DWORD flags = REAL_EFLAGREG; - DWORD mask = 0; - DWORD new_ip, new_sp; - DWORD old_ip, old_sp; - WORD new_ss; - WORD old_cs, old_ss; + CPU_WORKCLOCK(200); - /* VM86 && IOPL < 3 && interrupt cause == INTn */ - if (CPU_STAT_VM86 && (CPU_STAT_IOPL < CPU_IOPL3) && (softintp == -1)) { - EXCEPTION(GP_EXCEPTION, 0); + VERBOSE(("interrupt: -------------------------------------------------------------- start")); + VERBOSE(("interrupt: old EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_EIP, CPU_SS, CPU_ESP)); + +#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; + exc_errcode = idt_idx + 2; + if (intrtype == INTR_TYPE_EXTINTR) + exc_errcode++; + if (idt_idx + 7 > CPU_IDTR_LIMIT) { - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + VERBOSE(("interrupt: IDTR limit check failure (idx = 0x%04x, limit = 0x%08x", idt_idx, CPU_IDTR_LIMIT)); + EXCEPTION(GP_EXCEPTION, exc_errcode); } - CPU_SET_GATEDESC(&gd, CPU_IDTR_BASE + idt_idx); - if (!gd.valid || !gd.p) { - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + /* load a gate descriptor from interrupt descriptor table */ + memset(&gsd, 0, sizeof(gsd)); + load_descriptor(&gsd, CPU_IDTR_BASE + idt_idx); + if (!SEG_IS_VALID(&gsd)) { + VERBOSE(("interrupt: gate descripter is invalid.")); + 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_INTR_16: case CPU_SYSDESC_TYPE_INTR_32: @@ -282,258 +292,343 @@ interrupt(int num, int softintp, int err break; default: - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + VERBOSE(("interrupt: invalid gate type (%d)", gsd.type)); + EXCEPTION(GP_EXCEPTION, exc_errcode); break; } - /* 5.10.1.1. Îã³°¡¿³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¡¦¥×¥í¥·¡¼¥¸¥ã¤ÎÊݸî */ - if (softintp && (gd.dpl < CPU_STAT_CPL)) { - EXCEPTION(GP_EXCEPTION, num * 8 | 2); + /* 5.10.1.1. 価¿³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¡¦¥×¥í¥·¡¼¥¸¥ã¤ÎÊݸî */ + if ((intrtype != INTR_TYPE_EXTINTR) && (gsd.dpl < CPU_STAT_CPL)) { + VERBOSE(("interrupt: intrtype(%d) && DPL(%d) < CPL(%d)", intrtype, gsd.dpl, CPU_STAT_CPL)); + EXCEPTION(GP_EXCEPTION, exc_errcode); } - switch (gd.type) { - case CPU_SYSDESC_TYPE_TASK: - rv = parse_selector(&task_sel, gd.u.gate.selector); - if (rv < 0 || task_sel.ldt) { - EXCEPTION(TS_EXCEPTION, task_sel.idx); - } - - /* check gate type */ - switch (task_sel.desc.type) { - case CPU_SYSDESC_TYPE_TSS_16: - case CPU_SYSDESC_TYPE_TSS_32: - break; - - case CPU_SYSDESC_TYPE_TSS_BUSY_16: - case CPU_SYSDESC_TYPE_TSS_BUSY_32: - VERBOSE(("interrupt: task is busy.")); - /*FALLTHROUGH*/ - default: - EXCEPTION(TS_EXCEPTION, task_sel.idx); - break; - } - - /* not present */ - if (selector_is_not_present(&task_sel)) { - EXCEPTION(NP_EXCEPTION, task_sel.idx); - } - - task_switch(&task_sel, TASK_SWITCH_INTR); + if (!SEG_IS_PRESENT(&gsd)) { + VERBOSE(("interrupt: gate descriptor is not present.")); + EXCEPTION(NP_EXCEPTION, exc_errcode); + } - if (errorp) { - if (task_sel.desc.type == CPU_SYSDESC_TYPE_TSS_32) { - CHECK_STACK_PUSH(&CPU_STAT_SREG(CPU_SS_INDEX), CPU_ESP, 4); - PUSH0_32(error_code); - } else { - CHECK_STACK_PUSH(&CPU_STAT_SREG(CPU_SS_INDEX), CPU_ESP, 2); - PUSH0_16(error_code); - } - } + if ((intrtype == INTR_TYPE_EXTINTR) && CPU_STAT_HLT) { + VERBOSE(("interrupt: reset HTL in protected mode")); + CPU_EIP++; + CPU_STAT_HLT = 0; + } - /* out of range */ - if (CPU_EIP > CPU_STAT_CS_LIMIT) { - EXCEPTION(GP_EXCEPTION, 0); - } + switch (gsd.type) { + case CPU_SYSDESC_TYPE_TASK: + interrupt_task_gate(&gsd, intrtype, errorp, error_code); break; case CPU_SYSDESC_TYPE_INTR_16: case CPU_SYSDESC_TYPE_INTR_32: - flags &= I_FLAG; - mask |= I_FLAG; - /*FALLTHROUGH*/ case CPU_SYSDESC_TYPE_TRAP_16: case CPU_SYSDESC_TYPE_TRAP_32: - flags &= ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG); - mask |= T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG; + interrupt_intr_or_trap(&gsd, intrtype, errorp, error_code); + break; - new_ip = intr_sel.desc.u.seg.segbase; - old_ss = CPU_SS; - old_cs = CPU_CS; - old_ip = CPU_EIP; - old_sp = CPU_ESP; - - VERBOSE(("TRAP-OR-INTERRUPT-GATE")); - - rv = parse_selector(&intr_sel, gd.u.gate.selector); - if (rv < 0) { - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); - } + default: + EXCEPTION(GP_EXCEPTION, exc_errcode); + break; + } - if (!intr_sel.desc.s - || !intr_sel.desc.u.seg.c - || (intr_sel.desc.dpl > CPU_STAT_CPL)) { - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); - } + VERBOSE(("interrupt: ---------------------------------------------------------------- end")); + } + + CPU_CLEAR_PREV_ESP(); +} + +static void +interrupt_task_gate(const descriptor_t *gsdp, int intrtype, int errorp, int error_code) +{ + selector_t task_sel; + int rv; + + VERBOSE(("interrupt: TASK-GATE")); + + rv = parse_selector(&task_sel, gsdp->u.gate.selector); + if (rv < 0 || task_sel.ldt || !SEG_IS_SYSTEM(&task_sel.desc)) { + 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")); + EXCEPTION(TS_EXCEPTION, task_sel.idx); + } + + /* check gate type */ + switch (task_sel.desc.type) { + case CPU_SYSDESC_TYPE_TSS_16: + case CPU_SYSDESC_TYPE_TSS_32: + break; + + case CPU_SYSDESC_TYPE_TSS_BUSY_16: + case CPU_SYSDESC_TYPE_TSS_BUSY_32: + VERBOSE(("interrupt: task is busy.")); + /*FALLTHROUGH*/ + default: + VERBOSE(("interrupt: invalid gate type (%d)", task_sel.desc.type)); + EXCEPTION(TS_EXCEPTION, task_sel.idx); + break; + } + + /* not present */ + if (selector_is_not_present(&task_sel)) { + VERBOSE(("interrupt: selector is not present")); + EXCEPTION(NP_EXCEPTION, task_sel.idx); + } + + task_switch(&task_sel, TASK_SWITCH_INTR); - /* not present */ - if (selector_is_not_present(&intr_sel)) { - EXCEPTION(NP_EXCEPTION, intr_sel.idx | !softintp); + CPU_SET_PREV_ESP(); + + if (errorp) { + 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 +interrupt_intr_or_trap(const descriptor_t *gsdp, int intrtype, int errorp, int error_code) +{ + selector_t cs_sel, ss_sel; + UINT stacksize; + UINT32 old_flags; + UINT32 new_flags; + UINT32 mask; + UINT32 sp; + UINT32 new_ip, new_sp; + UINT32 old_ip, old_sp; + UINT16 old_cs, old_ss, new_ss; + int exc_errcode; + int rv; + + new_ip = gsdp->u.gate.offset; + old_ss = CPU_SS; + old_cs = CPU_CS; + old_ip = CPU_EIP; + 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 (gsdp->type) { + case CPU_SYSDESC_TYPE_INTR_16: + case CPU_SYSDESC_TYPE_INTR_32: + VERBOSE(("interrupt: INTERRUPT-GATE")); + new_flags &= ~I_FLAG; + mask |= I_FLAG; + break; + + case CPU_SYSDESC_TYPE_TRAP_16: + case CPU_SYSDESC_TYPE_TRAP_32: + VERBOSE(("interrupt: TRAP-GATE")); + break; + + default: + ia32_panic("interrupt: gate descriptor type is invalid (type = %d)", gsdp->type); + break; + } + + 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) { + VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", gsdp->u.gate.selector, rv)); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } + + /* check segment type */ + if (SEG_IS_SYSTEM(&cs_sel.desc)) { + VERBOSE(("interrupt: code segment is system segment")); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } + if (SEG_IS_DATA(&cs_sel.desc)) { + VERBOSE(("interrupt: code segment is data segment")); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } + + /* check privilege level */ + if (cs_sel.desc.dpl > CPU_STAT_CPL) { + VERBOSE(("interrupt: DPL(%d) > CPL(%d)", cs_sel.desc.dpl, CPU_STAT_CPL)); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } + + /* not present */ + if (selector_is_not_present(&cs_sel)) { + VERBOSE(("interrupt: selector is not present")); + EXCEPTION(NP_EXCEPTION, exc_errcode); + } + + if (!SEG_IS_CONFORMING_CODE(&cs_sel.desc) && (cs_sel.desc.dpl < CPU_STAT_CPL)) { + stacksize = errorp ? 12 : 10; + if (!CPU_STAT_VM86) { + VERBOSE(("interrupt: INTER-PRIVILEGE-LEVEL-INTERRUPT")); + } else { + /* VM86 */ + VERBOSE(("interrupt: INTERRUPT-FROM-VIRTUAL-8086-MODE")); + if (cs_sel.desc.dpl != 0) { + /* 16.3.1.1 */ + VERBOSE(("interrupt: DPL[CS](%d) != 0", cs_sel.desc.dpl)); + EXCEPTION(GP_EXCEPTION, exc_errcode); } + stacksize += 8; + } + if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) { + stacksize *= 2; + } + + /* get stack pointer from TSS */ + get_stack_pointer_from_tss(cs_sel.desc.dpl, &new_ss, &new_sp); - if (!intr_sel.desc.u.seg.ec && (intr_sel.desc.dpl < CPU_STAT_CPL)) { - DWORD stacksize; + /* parse stack segment descriptor */ + rv = parse_selector(&ss_sel, new_ss); - if (CPU_STAT_VM86) { - VERBOSE(("INTER-PRIVILEGE-LEVEL-INTERRUPT")); - stacksize = errorp ? 12 : 10; - } else { - if (intr_sel.desc.dpl != 0) { - EXCEPTION(GP_EXCEPTION, intr_sel.idx); - } - VERBOSE(("INTERRUPT-FROM-VIRTUAL-8086-MODE")); - stacksize = errorp ? 20 : 18; - } - - get_stack_from_tss(intr_sel.desc.dpl, &new_ss, &new_sp); - rv = parse_selector(&ss_sel, new_ss); - if (rv < 0) { - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); - } - - if ((ss_sel.rpl != intr_sel.desc.dpl) - || (ss_sel.desc.dpl != intr_sel.desc.dpl) - || !ss_sel.desc.s - || ss_sel.desc.u.seg.c - || !ss_sel.desc.u.seg.wr) { - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); - } - - /* not present */ - if (selector_is_not_present(&ss_sel)) { - EXCEPTION(SS_EXCEPTION, ss_sel.idx | !softintp); - } - - switch (gd.type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: - CHECK_STACK_PUSH(&ss_sel.desc, new_sp, stacksize * 2); - break; - - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - CHECK_STACK_PUSH(&ss_sel.desc, new_sp, stacksize); - new_ip &= 0xffff; - break; - } - - /* out of range */ - if (new_ip > intr_sel.desc.u.seg.limit) { - EXCEPTION(GP_EXCEPTION, 0); - } - - load_ss(new_ss, &ss_sel.desc, intr_sel.desc.dpl); - CPU_ESP = new_sp; - - if (!CPU_STAT_VM86) { - switch (gd.type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: - PUSH0_32(CPU_GS); - PUSH0_32(CPU_FS); - PUSH0_32(CPU_DS); - PUSH0_32(CPU_ES); - break; - - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - PUSH0_16(CPU_GS); - PUSH0_16(CPU_FS); - PUSH0_16(CPU_DS); - PUSH0_16(CPU_ES); - break; - } - - 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 (gd.type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: - PUSH0_32(old_ss); - PUSH0_32(old_sp); - PUSH0_32(REAL_EFLAGREG); - PUSH0_32(old_cs); - PUSH0_32(old_ip); - if (errorp) { - PUSH0_32(error_code); - } - break; - - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - PUSH0_16(old_ss); - PUSH0_16(old_sp); - PUSH0_16(REAL_FLAGREG); - PUSH0_16(old_cs); - PUSH0_16(old_ip); - if (errorp) { - PUSH0_16(error_code); - } - break; - } - - load_cs(intr_sel.selector, &intr_sel.desc, intr_sel.desc.dpl); - SET_EIP(new_ip); - set_eflags(flags, mask); - } else { - if (CPU_STAT_VM86 - || (!intr_sel.desc.u.seg.ec && (intr_sel.desc.dpl != CPU_STAT_CPL))) { - EXCEPTION(GP_EXCEPTION, intr_sel.idx); - } - VERBOSE(("INTRA-PRIVILEGE-LEVEL-INTERRUPT")); - - switch (gd.type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: - CHECK_STACK_PUSH(&ss_sel.desc, new_sp, errorp ? 16 : 12); - break; - - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - CHECK_STACK_PUSH(&ss_sel.desc, new_sp, errorp ? 8 : 6); - new_ip &= 0xffff; - break; - } - - /* out of range */ - if (new_ip > intr_sel.desc.u.seg.limit) { - EXCEPTION(GP_EXCEPTION, 0); - } - - switch (gd.type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: - PUSH0_32(REAL_EFLAGREG); - PUSH0_32(CPU_CS); - PUSH0_32(CPU_EIP); - if (errorp) { - PUSH0_32(error_code); - } - break; - - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - PUSH0_16(REAL_FLAGREG); - PUSH0_16(CPU_CS); - PUSH0_16(CPU_IP); - if (errorp) { - PUSH0_16(error_code); - } - break; - } - - load_cs(intr_sel.selector, &intr_sel.desc, CPU_STAT_CPL); - SET_EIP(new_ip); - set_eflags(flags, mask); + /* update exception error code */ + exc_errcode = ss_sel.idx; + if (intrtype == INTR_TYPE_EXTINTR) + exc_errcode++; + + if (rv < 0) { + VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", new_ss, rv)); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + + /* check privilege level */ + if (ss_sel.rpl != cs_sel.desc.dpl) { + VERBOSE(("interrupt: selector RPL[SS](%d) != DPL[CS](%d)", ss_sel.rpl, cs_sel.desc.dpl)); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + if (ss_sel.desc.dpl != cs_sel.desc.dpl) { + VERBOSE(("interrupt: descriptor DPL[SS](%d) != DPL[CS](%d)", ss_sel.desc.dpl, cs_sel.desc.dpl)); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + + /* stack segment must be writable data segment. */ + if (SEG_IS_SYSTEM(&ss_sel.desc)) { + VERBOSE(("interrupt: stack segment is system segment")); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + if (SEG_IS_CODE(&ss_sel.desc)) { + VERBOSE(("interrupt: stack segment is code segment")); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + if (!SEG_IS_WRITABLE_DATA(&ss_sel.desc)) { + VERBOSE(("interrupt: stack segment is read-only data segment")); + EXCEPTION(TS_EXCEPTION, exc_errcode); + } + + /* not present */ + if (selector_is_not_present(&ss_sel)) { + VERBOSE(("interrupt: selector is not present")); + EXCEPTION(SS_EXCEPTION, exc_errcode); + } + + /* check stack room size */ + cpu_stack_push_check(ss_sel.idx, &ss_sel.desc, new_sp, stacksize); + + /* out of range */ + if (new_ip > cs_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); + } + + load_ss(ss_sel.selector, &ss_sel.desc, cs_sel.desc.dpl); + CPU_ESP = new_sp; + + load_cs(cs_sel.selector, &cs_sel.desc, cs_sel.desc.dpl); + CPU_EIP = new_ip; + + if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) { + if (CPU_STAT_VM86) { + PUSH0_32(CPU_GS); + PUSH0_32(CPU_FS); + PUSH0_32(CPU_DS); + PUSH0_32(CPU_ES); + + LOAD_SEGREG(CPU_GS_INDEX, 0); + CPU_STAT_SREG(CPU_GS_INDEX).valid = 0; + LOAD_SEGREG(CPU_FS_INDEX, 0); + 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; } - break; + PUSH0_32(old_ss); + PUSH0_32(old_sp); + PUSH0_32(old_flags); + PUSH0_32(old_cs); + PUSH0_32(old_ip); + if (errorp) { + PUSH0_32(error_code); + } + } else { + if (CPU_STAT_VM86) { + ia32_panic("interrupt: 16bit gate && VM86"); + } + PUSH0_16(old_ss); + PUSH0_16(old_sp); + PUSH0_16(old_flags); + PUSH0_16(old_cs); + PUSH0_16(old_ip); + if (errorp) { + PUSH0_16(error_code); + } + } + } else { + if (CPU_STAT_VM86) { + VERBOSE(("interrupt: VM86")); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } + if (!SEG_IS_CONFORMING_CODE(&cs_sel.desc) && (cs_sel.desc.dpl != CPU_STAT_CPL)) { + VERBOSE(("interrupt: %sCONFORMING-CODE-SEGMENT(%d) && DPL[CS](%d) != CPL", SEG_IS_CONFORMING_CODE(&cs_sel.desc) ? "" : "NON-", cs_sel.desc.dpl, CPU_STAT_CPL)); + EXCEPTION(GP_EXCEPTION, exc_errcode); + } - default: - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); - break; + VERBOSE(("interrupt: INTRA-PRIVILEGE-LEVEL-INTERRUPT")); + + stacksize = errorp ? 8 : 6; + if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) { + stacksize *= 2; + } + + /* check stack room size */ + if (CPU_STAT_SS32) { + sp = CPU_ESP; + } else { + sp = CPU_SP; + } + SS_PUSH_CHECK(sp, stacksize); + + /* out of range */ + if (new_ip > cs_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); + } + + load_cs(cs_sel.selector, &cs_sel.desc, CPU_STAT_CPL); + CPU_EIP = new_ip; + + if (gsdp->type & CPU_SYSDESC_TYPE_32BIT) { + PUSH0_32(old_flags); + PUSH0_32(old_cs); + PUSH0_32(old_ip); + if (errorp) { + PUSH0_32(error_code); + } + } else { + PUSH0_16(old_flags); + PUSH0_16(old_cs); + PUSH0_16(old_ip); + if (errorp) { + PUSH0_16(error_code); + } } } + set_eflags(new_flags, mask); + + VERBOSE(("interrupt: new EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_EIP, CPU_SS, CPU_ESP)); }