// vector: 0..255: vector in IDT // error_code: if exception generates and error, push this error code // trap: override exception class to TRAP void BX_CPU_C::exception(unsigned vector, Bit16u error_code) { BX_INSTR_EXCEPTION(BX_CPU_ID, vector, error_code); #if BX_DEBUGGER bx_dbg_exception(BX_CPU_ID, vector, error_code); #endif BX_DEBUG(("exception(0x%02x): error_code=%04x", vector, error_code)); unsigned exception_type = 0; unsigned exception_class = BX_EXCEPTION_CLASS_FAULT; bx_bool push_error = 0; if (vector < BX_CPU_HANDLED_EXCEPTIONS) { push_error = exceptions_info[vector].push_error; exception_class = exceptions_info[vector].exception_class; exception_type = exceptions_info[vector].exception_type; } else { BX_PANIC(("exception(%u): bad vector", vector)); } if (vector != BX_PF_EXCEPTION && vector != BX_DF_EXCEPTION) { // Page faults have different format error_code = (error_code & 0xfffe) | BX_CPU_THIS_PTR EXT; } #if BX_SUPPORT_VMX VMexit_Event(0, BX_HARDWARE_EXCEPTION, vector, error_code, push_error); #endif if (BX_CPU_THIS_PTR errorno > 0) { if (BX_CPU_THIS_PTR errorno > 2 || BX_CPU_THIS_PTR curr_exception == BX_ET_DOUBLE_FAULT) { // restore RIP/RSP to value before error occurred RIP = BX_CPU_THIS_PTR prev_rip; if (BX_CPU_THIS_PTR speculative_rsp) RSP = BX_CPU_THIS_PTR prev_rsp; debug(BX_CPU_THIS_PTR prev_rip); // print debug information to the log #if BX_SUPPORT_VMX VMexit_TripleFault(); #endif #if BX_DEBUGGER // trap into debugger (similar as done when PANIC occured) bx_debug_break(); #endif if (SIM->get_param_bool(BXPN_RESET_ON_TRIPLE_FAULT)->get()) { BX_ERROR(("exception(): 3rd (%d) exception with no resolution, shutdown status is %02xh, resetting", vector, DEV_cmos_get_reg(0x0f))); bx_pc_system.Reset(BX_RESET_HARDWARE); } else { BX_PANIC(("exception(): 3rd (%d) exception with no resolution", vector)); BX_ERROR(("WARNING: Any simulation after this point is completely bogus !")); shutdown(); } longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop } } // note: fault-class exceptions _except_ #DB set RF in // eflags image. if (exception_class == BX_EXCEPTION_CLASS_FAULT) { // restore RIP/RSP to value before error occurred RIP = BX_CPU_THIS_PTR prev_rip; if (BX_CPU_THIS_PTR speculative_rsp) RSP = BX_CPU_THIS_PTR prev_rsp; if (vector != BX_DB_EXCEPTION) BX_CPU_THIS_PTR assert_RF(); } if (vector == BX_DB_EXCEPTION) { // Commit debug events to DR6 #if BX_CPU_LEVEL <= 4 // On 386/486 bit12 is settable BX_CPU_THIS_PTR dr6.val32 = (BX_CPU_THIS_PTR dr6.val32 & 0xffff0ff0) | (BX_CPU_THIS_PTR debug_trap & 0x0000f00f); #else // On Pentium+, bit12 is always zero BX_CPU_THIS_PTR dr6.val32 = (BX_CPU_THIS_PTR dr6.val32 & 0xffff0ff0) | (BX_CPU_THIS_PTR debug_trap & 0x0000e00f); #endif // clear GD flag in the DR7 prior entering debug exception handler BX_CPU_THIS_PTR dr7.set_GD(0); } BX_CPU_THIS_PTR EXT = 1; /* if we've already had 1st exception, see if 2nd causes a * Double Fault instead. Otherwise, just record 1st exception */ if (BX_CPU_THIS_PTR errorno > 0 && exception_type != BX_ET_DOUBLE_FAULT) { if (! is_exception_OK[BX_CPU_THIS_PTR curr_exception][exception_type]) { exception(BX_DF_EXCEPTION, 0); } } BX_CPU_THIS_PTR curr_exception = exception_type; BX_CPU_THIS_PTR errorno++; if (real_mode()) { push_error = 0; // not INT, no error code pushed error_code = 0; } interrupt(vector, BX_HARDWARE_EXCEPTION, push_error, error_code); BX_CPU_THIS_PTR errorno = 0; // error resolved longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop }
// vector: 0..255: vector in IDT // error_code: if exception generates and error, push this error code // trap: override exception class to TRAP void BX_CPU_C::exception(unsigned vector, Bit16u error_code, bx_bool trap) { unsigned exception_type = 0, exception_class = BX_EXCEPTION_CLASS_FAULT; bx_bool push_error = 0; invalidate_prefetch_q(); BX_INSTR_EXCEPTION(BX_CPU_ID, vector); #if BX_DEBUGGER bx_dbg_exception(BX_CPU_ID, vector, error_code); #endif BX_DEBUG(("exception(0x%02x): error_code=%04x", vector, error_code)); // if not initial error, restore previous register values from // previous attempt to handle exception if (BX_CPU_THIS_PTR errorno) { BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS] = BX_CPU_THIS_PTR save_cs; BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS] = BX_CPU_THIS_PTR save_ss; RIP = BX_CPU_THIS_PTR save_eip; RSP = BX_CPU_THIS_PTR save_esp; } if (BX_CPU_THIS_PTR errorno > 0) { if (errno > 2 || BX_CPU_THIS_PTR curr_exception == BX_ET_DOUBLE_FAULT) { debug(BX_CPU_THIS_PTR prev_rip); // print debug information to the log #if BX_DEBUGGER // trap into debugger (similar as done when PANIC occured) bx_debug_break(); #endif if (SIM->get_param_bool(BXPN_RESET_ON_TRIPLE_FAULT)->get()) { BX_ERROR(("exception(): 3rd (%d) exception with no resolution, shutdown status is %02xh, resetting", vector, DEV_cmos_get_reg(0x0f))); bx_pc_system.Reset(BX_RESET_SOFTWARE); } else { BX_PANIC(("exception(): 3rd (%d) exception with no resolution", vector)); BX_ERROR(("WARNING: Any simulation after this point is completely bogus !")); shutdown(); } longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop } } // note: fault-class exceptions _except_ #DB set RF in // eflags image. switch (vector) { case BX_DE_EXCEPTION: // DIV by 0 push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_CONTRIBUTORY; break; case BX_DB_EXCEPTION: // debug exceptions push_error = 0; // Instruction fetch breakpoint - FAULT // Data read or write breakpoint - TRAP // I/O read or write breakpoint - TRAP // General detect condition - FAULT // Single-step - TRAP // Task-switch - TRAP exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; case 2: // NMI push_error = 0; exception_type = BX_ET_BENIGN; break; case BX_BP_EXCEPTION: // breakpoint push_error = 0; exception_class = BX_EXCEPTION_CLASS_TRAP; exception_type = BX_ET_BENIGN; break; case BX_OF_EXCEPTION: // overflow push_error = 0; exception_class = BX_EXCEPTION_CLASS_TRAP; exception_type = BX_ET_BENIGN; break; case BX_BR_EXCEPTION: // bounds check push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; case BX_UD_EXCEPTION: // invalid opcode push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; case BX_NM_EXCEPTION: // device not available push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; case BX_DF_EXCEPTION: // double fault push_error = 1; error_code = 0; exception_class = BX_EXCEPTION_CLASS_ABORT; exception_type = BX_ET_DOUBLE_FAULT; break; case 9: // coprocessor segment overrun (286,386 only) push_error = 0; exception_class = BX_EXCEPTION_CLASS_ABORT; exception_type = BX_ET_BENIGN; BX_PANIC(("exception(9): unfinished")); break; case BX_TS_EXCEPTION: // invalid TSS push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_CONTRIBUTORY; break; case BX_NP_EXCEPTION: // segment not present push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_CONTRIBUTORY; break; case BX_SS_EXCEPTION: // stack fault push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_CONTRIBUTORY; break; case BX_GP_EXCEPTION: // general protection push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_CONTRIBUTORY; break; case BX_PF_EXCEPTION: // page fault push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_PAGE_FAULT; break; case 15: // reserved BX_PANIC(("exception(15): reserved")); push_error = 0; exception_type = 0; break; case BX_MF_EXCEPTION: // floating-point error push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; #if BX_CPU_LEVEL >= 4 case BX_AC_EXCEPTION: // alignment check push_error = 1; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; #endif #if BX_CPU_LEVEL >= 5 case BX_MC_EXCEPTION: // machine check BX_PANIC(("exception(): machine-check, vector 18 not implemented")); push_error = 0; exception_class = BX_EXCEPTION_CLASS_ABORT; exception_type = BX_ET_BENIGN; break; #if BX_SUPPORT_SSE case BX_XM_EXCEPTION: // SIMD Floating-Point exception push_error = 0; exception_class = BX_EXCEPTION_CLASS_FAULT; exception_type = BX_ET_BENIGN; break; #endif #endif default: BX_PANIC(("exception(%u): bad vector", (unsigned) vector)); exception_type = BX_ET_BENIGN; push_error = 0; // keep compiler happy for now break; } if (trap) { exception_class = BX_EXCEPTION_CLASS_TRAP; } else { if (exception_class == BX_EXCEPTION_CLASS_FAULT) { // restore RIP/RSP to value before error occurred RIP = BX_CPU_THIS_PTR prev_rip; if (BX_CPU_THIS_PTR speculative_rsp) RSP = BX_CPU_THIS_PTR prev_rsp; if (vector != BX_DB_EXCEPTION) BX_CPU_THIS_PTR assert_RF(); } } // clear GD flag in the DR7 prior entering debug exception handler if (vector == BX_DB_EXCEPTION) BX_CPU_THIS_PTR dr7 &= ~0x00002000; if (exception_type != BX_ET_PAGE_FAULT) { // Page faults have different format error_code = (error_code & 0xfffe) | BX_CPU_THIS_PTR EXT; } else { // FIXME: special format error returned for page faults ? } BX_CPU_THIS_PTR EXT = 1; /* if we've already had 1st exception, see if 2nd causes a * Double Fault instead. Otherwise, just record 1st exception */ if (BX_CPU_THIS_PTR errorno > 0) { if (is_exception_OK[BX_CPU_THIS_PTR curr_exception][exception_type]) { BX_CPU_THIS_PTR curr_exception = exception_type; } else { exception(BX_DF_EXCEPTION, 0, 0); } } else { BX_CPU_THIS_PTR curr_exception = exception_type; } BX_CPU_THIS_PTR errorno++; if (real_mode()) { // not INT, no error code pushed BX_CPU_THIS_PTR interrupt(vector, 0, 0, 0); BX_CPU_THIS_PTR errorno = 0; // error resolved longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop } else { BX_CPU_THIS_PTR interrupt(vector, 0, push_error, error_code); BX_CPU_THIS_PTR errorno = 0; // error resolved longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop } }