--- np2/i386c/ia32/exception.c 2004/02/05 16:43:44 1.8 +++ np2/i386c/ia32/exception.c 2004/03/08 12:56:22 1.14 @@ -1,4 +1,4 @@ -/* $Id: exception.c,v 1.8 2004/02/05 16:43:44 monaka Exp $ */ +/* $Id: exception.c,v 1.14 2004/03/08 12:56:22 monaka Exp $ */ /* * Copyright (c) 2003 NONAKA Kimihiro @@ -76,21 +76,28 @@ exception(int num, int error_code) 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) - || (CPU_STAT_NERROR == 2 && CPU_STAT_PREV_EXCEPTION == DF_EXCEPTION)) { + 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 DB_EXCEPTION: /* (F/T) デバッグ */ case BR_EXCEPTION: /* (F) BOUND の範囲外 */ case UD_EXCEPTION: /* (F) 無効オペコード */ case NM_EXCEPTION: /* (F) デバイス使用不可 (FPU が無い) */ + case MF_EXCEPTION: /* (F) 浮動小数点エラー */ +#if CPU_FAMILY >= 5 + case MC_EXCEPTION: /* (A) マシンチェック */ +#endif +#if CPU_FAMILY >= 6 + case XF_EXCEPTION: /* (F) ストリーミング SIMD 拡張命令 */ +#endif CPU_EIP = CPU_PREV_EIP; /*FALLTHROUGH*/ - case DB_EXCEPTION: /* (F/T) デバッグ */ case NMI_EXCEPTION: /* (I) NMI 割り込み */ case BP_EXCEPTION: /* (T) ブレークポイント */ case OF_EXCEPTION: /* (T) オーバーフロー */ @@ -116,33 +123,16 @@ exception(int num, int error_code) errorp = 1; 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: 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; @@ -150,6 +140,14 @@ exception(int num, int error_code) VERBOSE(("exception: ---------------------------------------------------------------- end")); INTERRUPT(num, 0, 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); } @@ -209,16 +207,16 @@ exception(int num, int error_code) * D : ゲートのサイズ.0 = 16 bit, 1 = 32 bit */ -static void interrupt_task(descriptor_t *gdp, int softintp, int errorp, int error_code); -static void interrupt_intr_or_trap(descriptor_t *gdp, int softintp, int errorp, int error_code); +static void interrupt_task_gate(descriptor_t *gd, int softintp, int errorp, int error_code); +static void interrupt_intr_or_trap(descriptor_t *gd, int softintp, int errorp, int error_code); void interrupt(int num, int softintp, int errorp, int error_code) { descriptor_t gd; - DWORD idt_idx; - DWORD new_ip; - WORD new_cs; + UINT idt_idx; + UINT32 new_ip; + UINT16 new_cs; VERBOSE(("interrupt: num = 0x%02x, softintp = %s, errorp = %s, error_code = %08x", num, softintp ? "on" : "off", errorp ? "on" : "off", error_code)); @@ -230,11 +228,9 @@ interrupt(int num, int softintp, int err EXCEPTION(GP_EXCEPTION, idt_idx + 2); } - if (!softintp) { - BYTE op = cpu_codefetch(CPU_IP); - if (op == 0xf4) { /* hlt */ - CPU_EIP++; - } + if (!softintp && CPU_STAT_HLT) { + CPU_EIP++; + CPU_STAT_HLT = FALSE; } REGPUSH0(REAL_FLAGREG); @@ -253,6 +249,15 @@ interrupt(int num, int softintp, int err /* protected mode */ 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 no = %d\n%s", CPU_EAX, cpu_reg2str())); + } +#endif + /* VM86 && IOPL < 3 && interrupt cause == INTn */ if (CPU_STAT_VM86 && (CPU_STAT_IOPL < CPU_IOPL3) && (softintp == -1)) { VERBOSE(("interrupt: VM86 && IOPL < 3 && INTn")); @@ -292,9 +297,14 @@ interrupt(int num, int softintp, int err EXCEPTION(GP_EXCEPTION, idt_idx + 2); } + if (!softintp && CPU_STAT_HLT) { + CPU_EIP++; + CPU_STAT_HLT = FALSE; + } + switch (gd.type) { case CPU_SYSDESC_TYPE_TASK: - interrupt_task(&gd, softintp, errorp, error_code); + interrupt_task_gate(&gd, softintp, errorp, error_code); break; case CPU_SYSDESC_TYPE_INTR_16: @@ -314,7 +324,7 @@ interrupt(int num, int softintp, int err } static void -interrupt_task(descriptor_t *gdp, int softintp, int errorp, int error_code) +interrupt_task_gate(descriptor_t *gd, int softintp, int errorp, int error_code) { selector_t task_sel; int rv; @@ -323,9 +333,9 @@ interrupt_task(descriptor_t *gdp, int so (void)softintp; - rv = parse_selector(&task_sel, gdp->u.gate.selector); + rv = parse_selector(&task_sel, gd->u.gate.selector); 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')); + VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d, %cDT)", gd->u.gate.selector, rv, task_sel.ldt ? 'L' : 'G')); EXCEPTION(TS_EXCEPTION, task_sel.idx); } @@ -359,99 +369,96 @@ interrupt_task(descriptor_t *gdp, int so } static void -interrupt_intr_or_trap(descriptor_t *gdp, int softintp, int errorp, int error_code) +interrupt_intr_or_trap(descriptor_t *gd, 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; - DWORD new_ip, new_sp; - DWORD old_ip, old_sp; - WORD old_cs, old_ss, new_ss; + 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 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; + new_ip = gd->u.gate.offset; old_ss = CPU_SS; old_cs = CPU_CS; old_ip = CPU_EIP; old_sp = CPU_ESP; + new_flags = old_flags = REAL_EFLAGREG; - switch (gdp->type) { + switch (gd->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; - mask |= I_FLAG; + new_flags &= ~I_FLAG; + mask = I_FLAG; break; case CPU_SYSDESC_TYPE_TRAP_16: - old_ip &= 0xffff; - old_sp &= 0xffff; - /*FALLTHROUGH*/ case CPU_SYSDESC_TYPE_TRAP_32: VERBOSE(("interrupt: TRAP-GATE")); + + default: + mask = 0; break; } - flags &= ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG); + new_flags &= ~(T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG); mask |= T_FLAG|RF_FLAG|NT_FLAG|VM_FLAG; - rv = parse_selector(&intr_sel, gdp->u.gate.selector); + rv = parse_selector(&cs_sel, gd->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); + VERBOSE(("interrupt: parse_selector (selector = %04x, rv = %d)", gd->u.gate.selector, rv)); + EXCEPTION(GP_EXCEPTION, cs_sel.idx + !softintp); } /* check segment type */ - if (!intr_sel.desc.s) { + if (!cs_sel.desc.s) { VERBOSE(("interrupt: code segment is system segment")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); + EXCEPTION(GP_EXCEPTION, cs_sel.idx + !softintp); } - if (!intr_sel.desc.u.seg.c) { + if (!cs_sel.desc.u.seg.c) { VERBOSE(("interrupt: code segment is data segment")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); + EXCEPTION(GP_EXCEPTION, cs_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); + if (cs_sel.desc.dpl > CPU_STAT_CPL) { + VERBOSE(("interrupt: DPL(%d) > CPL(%d)", cs_sel.desc.dpl, CPU_STAT_CPL)); + EXCEPTION(GP_EXCEPTION, cs_sel.idx + !softintp); } /* not present */ - if (selector_is_not_present(&intr_sel)) { + if (selector_is_not_present(&cs_sel)) { VERBOSE(("interrupt: selector is not present")); - EXCEPTION(NP_EXCEPTION, intr_sel.idx + !softintp); + EXCEPTION(NP_EXCEPTION, cs_sel.idx + !softintp); } - if (!intr_sel.desc.u.seg.ec - && (intr_sel.desc.dpl < CPU_STAT_CPL)) { + if (!cs_sel.desc.u.seg.ec + && (cs_sel.desc.dpl < CPU_STAT_CPL)) { if (!CPU_STAT_VM86) { VERBOSE(("interrupt: INTER-PRIVILEGE-LEVEL-INTERRUPT")); stacksize = errorp ? 12 : 10; } else { /* VM86 */ - if (intr_sel.desc.dpl != 0) { + if (cs_sel.desc.dpl != 0) { /* 16.3.1.1 */ - VERBOSE(("interrupt: DPL[CS](%d) != 0", intr_sel.desc.dpl)); - EXCEPTION(GP_EXCEPTION, intr_sel.idx); + VERBOSE(("interrupt: DPL[CS](%d) != 0", cs_sel.desc.dpl)); + EXCEPTION(GP_EXCEPTION, cs_sel.idx); } VERBOSE(("interrupt: INTERRUPT-FROM-VIRTUAL-8086-MODE")); stacksize = errorp ? 20 : 18; } - switch (gdp->type) { + switch (gd->type) { case CPU_SYSDESC_TYPE_INTR_32: case CPU_SYSDESC_TYPE_TRAP_32: stacksize *= 2; break; } - get_stack_from_tss(intr_sel.desc.dpl, &new_ss, &new_sp); + get_stack_pointer_from_tss(cs_sel.desc.dpl, &new_ss, &new_sp); rv = parse_selector(&ss_sel, new_ss); if (rv < 0) { @@ -460,12 +467,12 @@ interrupt_intr_or_trap(descriptor_t *gdp } /* 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)); + if (ss_sel.rpl != cs_sel.desc.dpl) { + VERBOSE(("interrupt: RPL[SS](%d) != DPL[CS](%d)", ss_sel.rpl, cs_sel.desc.dpl)); 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)); + if (ss_sel.desc.dpl != cs_sel.desc.dpl) { + VERBOSE(("interrupt: DPL[SS](%d) != DPL[CS](%d)", ss_sel.desc.dpl, cs_sel.desc.dpl)); EXCEPTION(TS_EXCEPTION, ss_sel.idx + !softintp); } @@ -493,42 +500,35 @@ interrupt_intr_or_trap(descriptor_t *gdp CHECK_STACK_PUSH(&ss_sel.desc, new_sp, stacksize); /* out of range */ - if (new_ip > intr_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)); + 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, intr_sel.desc.dpl); + load_ss(ss_sel.selector, &ss_sel.desc, cs_sel.desc.dpl); 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); - if (CPU_STAT_VM86) { - switch (gdp->type) { - case CPU_SYSDESC_TYPE_INTR_32: - case CPU_SYSDESC_TYPE_TRAP_32: + switch (gd->type) { + case CPU_SYSDESC_TYPE_INTR_32: + case CPU_SYSDESC_TYPE_TRAP_32: + if (CPU_STAT_VM86) { 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: - ia32_panic("interrupt: 16bit gate"); - break; + CPU_SET_SEGREG(CPU_GS_INDEX, 0); + CPU_STAT_SREG(CPU_GS_INDEX).valid = 0; + CPU_SET_SEGREG(CPU_FS_INDEX, 0); + CPU_STAT_SREG(CPU_FS_INDEX).valid = 0; + CPU_SET_SEGREG(CPU_DS_INDEX, 0); + CPU_STAT_SREG(CPU_DS_INDEX).valid = 0; + CPU_SET_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_sp); PUSH0_32(old_flags); @@ -541,6 +541,9 @@ interrupt_intr_or_trap(descriptor_t *gdp case CPU_SYSDESC_TYPE_INTR_16: case CPU_SYSDESC_TYPE_TRAP_16: + if (CPU_STAT_VM86) { + ia32_panic("interrupt: 16bit gate && VM86"); + } PUSH0_16(old_ss); PUSH0_16(old_sp); PUSH0_16(old_flags); @@ -552,45 +555,48 @@ interrupt_intr_or_trap(descriptor_t *gdp break; } - set_eflags(flags, mask); + set_eflags(new_flags, mask); } else { if (CPU_STAT_VM86) { VERBOSE(("interrupt: VM86")); - EXCEPTION(GP_EXCEPTION, intr_sel.idx + !softintp); + EXCEPTION(GP_EXCEPTION, cs_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 + !softintp); + if (!cs_sel.desc.u.seg.ec && (cs_sel.desc.dpl != CPU_STAT_CPL)) { + VERBOSE(("interrupt: NON-CONFORMING-CODE-SEGMENT(%s) and DPL[CS](%d) != CPL", cs_sel.desc.u.seg.ec ? "false" : "true", cs_sel.desc.dpl, CPU_STAT_CPL)); + EXCEPTION(GP_EXCEPTION, cs_sel.idx + !softintp); } VERBOSE(("interrupt: INTRA-PRIVILEGE-LEVEL-INTERRUPT")); - if (CPU_STAT_SS32) { - new_sp = CPU_ESP; - } else { - new_sp = CPU_SP; - } - stacksize = errorp ? 8 : 6; - switch (gdp->type) { + switch (gd->type) { case CPU_SYSDESC_TYPE_INTR_32: case CPU_SYSDESC_TYPE_TRAP_32: stacksize *= 2; break; } - CHECK_STACK_PUSH(&CPU_STAT_SREG(CPU_SS_INDEX), new_sp, stacksize); + + if (CPU_STAT_SS32) { + sp = CPU_ESP; + } else { + sp = CPU_SP; + } + CHECK_STACK_PUSH(&CPU_STAT_SREG(CPU_SS_INDEX), sp, stacksize); /* out of range */ - if (new_ip > intr_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)); + 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); } - switch (gdp->type) { + load_cs(cs_sel.selector, &cs_sel.desc, CPU_STAT_CPL); + SET_EIP(new_ip); + + 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); + PUSH0_32(old_flags); + PUSH0_32(old_cs); + PUSH0_32(old_ip); if (errorp) { PUSH0_32(error_code); } @@ -598,20 +604,17 @@ interrupt_intr_or_trap(descriptor_t *gdp case CPU_SYSDESC_TYPE_INTR_16: case CPU_SYSDESC_TYPE_TRAP_16: - PUSH0_16(REAL_FLAGREG); - PUSH0_16(CPU_CS); - PUSH0_16(CPU_IP); + PUSH0_16(old_flags); + PUSH0_16(old_cs); + PUSH0_16(old_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); + 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)); }