--- np2/i386c/ia32/exception.c 2004/01/23 14:33:26 1.4 +++ np2/i386c/ia32/exception.c 2004/02/05 16:43:44 1.8 @@ -1,4 +1,4 @@ -/* $Id: exception.c,v 1.4 2004/01/23 14:33:26 monaka Exp $ */ +/* $Id: exception.c,v 1.8 2004/02/05 16:43:44 monaka Exp $ */ /* * Copyright (c) 2003 NONAKA Kimihiro @@ -72,7 +72,9 @@ exception(int num, int error_code) __ASSERT((unsigned int)num < EXCEPTION_NUM); + 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_NERROR++; if ((CPU_STAT_NERROR >= 3) @@ -100,9 +102,11 @@ exception(int num, int error_code) error_code = 0; break; +#if CPU_FAMILY >= 4 case AC_EXCEPTION: /* (F) アラインメントチェック (errcode: 0) */ error_code = 0; /*FALLTHROUGH*/ +#endif case TS_EXCEPTION: /* (F) 無効 TSS (errcode) */ case NP_EXCEPTION: /* (F) セグメント不在 (errcode) */ case SS_EXCEPTION: /* (F) スタックセグメントフォルト (errcode) */ @@ -117,27 +121,25 @@ exception(int num, int error_code) 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: ia32_panic("exception: unknown exception (%d)", num); break; } -#if defined(DEBUG) - if (num == PF_EXCEPTION) { - VERBOSE(("exception: CPU_CR2 = %08x", CPU_CR2)); - } -#endif - if (CPU_STAT_NERROR >= 2) { if (dftable[exctype[CPU_STAT_PREV_EXCEPTION]][exctype[num]]) { num = DF_EXCEPTION; @@ -145,8 +147,9 @@ exception(int num, int error_code) } CPU_STAT_PREV_EXCEPTION = num; - INTERRUPT(num, FALSE, errorp, error_code); - CPU_STAT_NERROR = 0; + VERBOSE(("exception: ---------------------------------------------------------------- end")); + + INTERRUPT(num, 0, errorp, error_code); siglongjmp(exec_1step_jmpbuf, 1); } @@ -217,14 +220,14 @@ interrupt(int num, int softintp, int err DWORD new_ip; WORD new_cs; - 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, softintp = %s, errorp = %s, error_code = %08x", num, softintp ? "on" : "off", errorp ? "on" : "off", error_code)); if (!CPU_STAT_PM) { /* real mode */ idt_idx = num * 4; 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)); - EXCEPTION(GP_EXCEPTION, num * 4 | 2); + EXCEPTION(GP_EXCEPTION, idt_idx + 2); } if (!softintp) { @@ -241,13 +244,14 @@ interrupt(int num, int softintp, int err CPU_EFLAG &= ~(T_FLAG | I_FLAG | AC_FLAG | RF_FLAG); CPU_TRAP = 0; - new_ip = cpu_memoryread_w(CPU_IDTR_BASE + num * 4); - new_cs = cpu_memoryread_w(CPU_IDTR_BASE + num * 4 + 2); + new_ip = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx); + new_cs = cpu_memoryread_w(CPU_IDTR_BASE + idt_idx + 2); CPU_SET_SEGREG(CPU_CS_INDEX, new_cs); SET_EIP(new_ip); CPU_WORKCLOCK(20); } else { /* protected mode */ + VERBOSE(("interrupt: -------------------------------------------------------------- start")); /* VM86 && IOPL < 3 && interrupt cause == INTn */ if (CPU_STAT_VM86 && (CPU_STAT_IOPL < CPU_IOPL3) && (softintp == -1)) { @@ -258,14 +262,14 @@ interrupt(int num, int softintp, int err idt_idx = num * 8; if (idt_idx + 7 > 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, idt_idx + 2 + !softintp); } memset(&gd, 0, sizeof(gd)); - CPU_SET_GATEDESC(&gd, CPU_IDTR_BASE + idt_idx); + load_descriptor(&gd, CPU_IDTR_BASE + idt_idx); if (!gd.valid || !gd.p) { VERBOSE(("interrupt: gate descripter is invalid.")); - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + EXCEPTION(GP_EXCEPTION, idt_idx + 2 + !softintp); } switch (gd.type) { @@ -278,14 +282,14 @@ interrupt(int num, int softintp, int err default: VERBOSE(("interrupt: invalid gate type (%d)", gd.type)); - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + EXCEPTION(GP_EXCEPTION, idt_idx + 2 + !softintp); break; } /* 5.10.1.1. 例外/割り込みハンドラ・プロシージャの保護 */ if (softintp && (gd.dpl < CPU_STAT_CPL)) { VERBOSE(("interrupt: softintp && DPL(%d) < CPL(%d)", gd.dpl, CPU_STAT_CPL)); - EXCEPTION(GP_EXCEPTION, num * 8 | 2); + EXCEPTION(GP_EXCEPTION, idt_idx + 2); } switch (gd.type) { @@ -301,9 +305,11 @@ interrupt(int num, int softintp, int err break; default: - EXCEPTION(GP_EXCEPTION, num * 8 | 2 | !softintp); + EXCEPTION(GP_EXCEPTION, idt_idx + 2 + !softintp); break; } + + VERBOSE(("interrupt: ---------------------------------------------------------------- end")); } } @@ -356,6 +362,7 @@ static void interrupt_intr_or_trap(descriptor_t *gdp, int softintp, int errorp, int error_code) { selector_t intr_sel, ss_sel; + DWORD old_flags = REAL_EFLAGREG; DWORD flags = REAL_EFLAGREG; DWORD mask = 0; DWORD stacksize; @@ -364,8 +371,19 @@ interrupt_intr_or_trap(descriptor_t *gdp WORD old_cs, old_ss, new_ss; int rv; + VERBOSE(("interrupt: old EIP = %04x:%08x, ESP = %04x:%08x", CPU_CS, CPU_PREV_EIP, CPU_SS, CPU_ESP)); + + new_ip = gdp->u.gate.offset; + old_ss = CPU_SS; + old_cs = CPU_CS; + old_ip = CPU_EIP; + old_sp = CPU_ESP; + switch (gdp->type) { case CPU_SYSDESC_TYPE_INTR_16: + old_ip &= 0xffff; + old_sp &= 0xffff; + /*FALLTHROUGH*/ case CPU_SYSDESC_TYPE_INTR_32: VERBOSE(("interrupt: INTERRUPT-GATE")); flags &= ~I_FLAG; @@ -373,54 +391,42 @@ interrupt_intr_or_trap(descriptor_t *gdp break; case CPU_SYSDESC_TYPE_TRAP_16: + old_ip &= 0xffff; + old_sp &= 0xffff; + /*FALLTHROUGH*/ case CPU_SYSDESC_TYPE_TRAP_32: VERBOSE(("interrupt: TRAP-GATE")); break; } - flags &= ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG); mask |= T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG; - new_ip = gdp->u.gate.offset; - old_ss = CPU_SS; - old_cs = CPU_CS; - old_ip = CPU_EIP; - old_sp = CPU_ESP; - switch (gdp->type) { - case CPU_SYSDESC_TYPE_INTR_16: - case CPU_SYSDESC_TYPE_TRAP_16: - old_ip &= 0xffff; - old_sp &= 0xffff; - break; - } - VERBOSE(("interrupt: old EIP = %04x:%08x, old ESP = %04x:%08x", old_cs, old_ip, old_ss, old_sp)); - rv = parse_selector(&intr_sel, gdp->u.gate.selector); if (rv < 0) { VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", gdp->u.gate.selector, rv)); - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } /* check segment type */ if (!intr_sel.desc.s) { VERBOSE(("interrupt: code segment is system segment")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } if (!intr_sel.desc.u.seg.c) { VERBOSE(("interrupt: code segment is data segment")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } /* check privilege level */ if (intr_sel.desc.dpl > CPU_STAT_CPL) { VERBOSE(("interrupt: DPL(%d) > CPL(%d)", intr_sel.desc.dpl, CPU_STAT_CPL)); - EXCEPTION(GP_EXCEPTION, intr_sel.idx | !softintp); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } /* not present */ if (selector_is_not_present(&intr_sel)) { VERBOSE(("interrupt: selector is not present")); - EXCEPTION(NP_EXCEPTION, intr_sel.idx | !softintp); + EXCEPTION(NP_EXCEPTION, intr_sel.idx + !softintp); } if (!intr_sel.desc.u.seg.ec @@ -450,37 +456,37 @@ interrupt_intr_or_trap(descriptor_t *gdp rv = parse_selector(&ss_sel, new_ss); if (rv < 0) { VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", new_ss, rv)); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } /* check privilege level */ if (ss_sel.rpl != intr_sel.desc.dpl) { VERBOSE(("interrupt: RPL[SS](%d) != DPL[CS](%d)", ss_sel.rpl, intr_sel.desc.dpl)); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } if (ss_sel.desc.dpl != intr_sel.desc.dpl) { VERBOSE(("interrupt: DPL[SS](%d) != DPL[CS](%d)", ss_sel.desc.dpl, intr_sel.desc.dpl)); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } /* check segment type */ if (!ss_sel.desc.s) { VERBOSE(("interrupt: stack segment is system segment")); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } if (ss_sel.desc.u.seg.c) { VERBOSE(("interrupt: stack segment is code segment")); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } if (!ss_sel.desc.u.seg.wr) { VERBOSE(("interrupt: stack segment is read-only data segment")); - EXCEPTION(TS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } /* not present */ if (selector_is_not_present(&ss_sel)) { VERBOSE(("interrupt: selector is not present")); - EXCEPTION(SS_EXCEPTION, ss_sel.idx | !softintp); + EXCEPTION(SS_EXCEPTION, ss_sel.idx + !softintp); } /* check stack room size */ @@ -492,12 +498,8 @@ interrupt_intr_or_trap(descriptor_t *gdp EXCEPTION(GP_EXCEPTION, 0); } - load_ss(new_ss, &ss_sel.desc, intr_sel.desc.dpl); - if (CPU_STAT_SS32) { - CPU_ESP = new_sp; - } else { - CPU_SP = new_sp; - } + load_ss(ss_sel.selector, &ss_sel.desc, intr_sel.desc.dpl); + CPU_ESP = new_sp; load_cs(intr_sel.selector, &intr_sel.desc, intr_sel.desc.dpl); SET_EIP(new_ip); @@ -529,7 +531,7 @@ interrupt_intr_or_trap(descriptor_t *gdp case CPU_SYSDESC_TYPE_TRAP_32: PUSH0_32(old_ss); PUSH0_32(old_sp); - PUSH0_32(REAL_EFLAGREG); + PUSH0_32(old_flags); PUSH0_32(old_cs); PUSH0_32(old_ip); if (errorp) { @@ -541,7 +543,7 @@ interrupt_intr_or_trap(descriptor_t *gdp case CPU_SYSDESC_TYPE_TRAP_16: PUSH0_16(old_ss); PUSH0_16(old_sp); - PUSH0_16(REAL_FLAGREG); + PUSH0_16(old_flags); PUSH0_16(old_cs); PUSH0_16(old_ip); if (errorp) { @@ -554,11 +556,11 @@ interrupt_intr_or_trap(descriptor_t *gdp } else { if (CPU_STAT_VM86) { VERBOSE(("interrupt: VM86")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } if (!intr_sel.desc.u.seg.ec && (intr_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)); - EXCEPTION(GP_EXCEPTION, intr_sel.idx); + EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); } VERBOSE(("interrupt: INTRA-PRIVILEGE-LEVEL-INTERRUPT"));