Boolean bx_cpu_c::can_pop(Bit32u bytes) { Bit32u temp_ESP, expand_down_limit; /* ??? */ if (real_mode()) BX_PANIC(("can_pop(): called in real mode?")); if (bx_cpu. sregs[BX_SEG_REG_SS].cache.u.segment.d_b) { /* Big bit set: use ESP */ temp_ESP = ESP; expand_down_limit = 0xFFFFFFFF; } else { /* Big bit clear: use SP */ temp_ESP = SP; expand_down_limit = 0xFFFF; } if (bx_cpu. sregs[BX_SEG_REG_SS].cache.valid==0) { BX_PANIC(("can_pop(): SS invalidated.")); return(0); /* never gets here */ } if (bx_cpu. sregs[BX_SEG_REG_SS].cache.p==0) { /* ??? */ BX_PANIC(("can_pop(): SS.p = 0")); return(0); } if (bx_cpu. sregs[BX_SEG_REG_SS].cache.u.segment.c_ed) { /* expand down segment */ if ( temp_ESP == expand_down_limit ) { BX_PANIC(("can_pop(): found SP=ffff")); return(0); } if ( ((expand_down_limit - temp_ESP) + 1) >= bytes ) return(1); return(0); } else { /* normal (expand-up) segment */ if (bx_cpu. sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled==0) { BX_PANIC(("can_pop(): SS.limit = 0")); } if ( temp_ESP == expand_down_limit ) { BX_PANIC(("can_pop(): found SP=ffff")); return(0); } if ( temp_ESP > bx_cpu. sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled ) { BX_PANIC(("can_pop(): eSP > SS.limit")); return(0); } if ( ((bx_cpu. sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled - temp_ESP) + 1) >= bytes ) return(1); return(0); } }
void BX_CPU_C::interrupt(Bit8u vector, bx_bool is_INT, bx_bool is_error_code, Bit16u error_code) { #if BX_DEBUGGER BX_CPU_THIS_PTR show_flag |= Flag_intsig; #if BX_DEBUG_LINUX if (bx_dbg.linux_syscall) { if (vector == 0x80) bx_dbg_linux_syscall(BX_CPU_ID); } #endif bx_dbg_interrupt(BX_CPU_ID, vector, error_code); #endif BX_DEBUG(("interrupt(): vector = %u, INT = %u, EXT = %u", (unsigned) vector, (unsigned) is_INT, (unsigned) BX_CPU_THIS_PTR EXT)); BX_INSTR_INTERRUPT(BX_CPU_ID, vector); invalidate_prefetch_q(); // Discard any traps and inhibits for new context; traps will // resume upon return. BX_CPU_THIS_PTR debug_trap = 0; BX_CPU_THIS_PTR inhibit_mask = 0; BX_CPU_THIS_PTR save_cs = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS]; BX_CPU_THIS_PTR save_ss = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS]; BX_CPU_THIS_PTR save_eip = RIP; BX_CPU_THIS_PTR save_esp = RSP; #if BX_SUPPORT_X86_64 if (long_mode()) { long_mode_int(vector, is_INT, is_error_code, error_code); return; } #endif if(real_mode()) { real_mode_int(vector, is_INT, is_error_code, error_code); } else { protected_mode_int(vector, is_INT, is_error_code, error_code); } }
Boolean bx_cpu_c::can_push(bx_descriptor_t *descriptor, Bit32u esp, Bit32u bytes) { if ( real_mode() ) { /* code not needed ??? */ BX_PANIC(("can_push(): called in real mode")); return(0); /* never gets here */ } // small stack compares against 16-bit SP if (!descriptor->u.segment.d_b) esp &= 0x0000ffff; if (descriptor->valid==0) { BX_PANIC(("can_push(): SS invalidated.")); return(0); } if (descriptor->p==0) { BX_PANIC(("can_push(): not present")); return(0); } if (descriptor->u.segment.c_ed) { /* expand down segment */ Bit32u expand_down_limit; if (descriptor->u.segment.d_b) expand_down_limit = 0xffffffff; else expand_down_limit = 0x0000ffff; if (esp==0) { BX_PANIC(("can_push(): esp=0, wraparound?")); return(0); } if (esp < bytes) { BX_PANIC(("can_push(): expand-down: esp < N")); return(0); } if ( (esp - bytes) <= descriptor->u.segment.limit_scaled ) { BX_PANIC(("can_push(): expand-down: esp-N < limit")); return(0); } if ( esp > expand_down_limit ) { BX_PANIC(("can_push(): esp > expand-down-limit")); return(0); } return(1); } else { /* normal (expand-up) segment */ if (descriptor->u.segment.limit_scaled==0) { BX_PANIC(("can_push(): found limit of 0")); return(0); } // Look at case where esp==0. Possibly, it's an intentional wraparound // If so, limit must be the maximum for the given stack size if (esp==0) { if (descriptor->u.segment.d_b && (descriptor->u.segment.limit_scaled==0xffffffff)) return(1); if ((descriptor->u.segment.d_b==0) && (descriptor->u.segment.limit_scaled>=0xffff)) return(1); BX_PANIC(("can_push(): esp=0, normal, wraparound? limit=%08x", descriptor->u.segment.limit_scaled)); return(0); } if (esp < bytes) { BX_INFO(("can_push(): expand-up: esp < N")); return(0); } if ((esp-1) > descriptor->u.segment.limit_scaled) { BX_INFO(("can_push(): expand-up: SP > limit")); return(0); } /* all checks pass */ return(1); } }
// 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 }
void BX_CPU_C::interrupt(Bit8u vector, unsigned type, bx_bool push_error, Bit16u error_code) { #if BX_DEBUGGER BX_CPU_THIS_PTR show_flag |= Flag_intsig; #if BX_DEBUG_LINUX if (bx_dbg.linux_syscall) { if (vector == 0x80) bx_dbg_linux_syscall(BX_CPU_ID); } #endif bx_dbg_interrupt(BX_CPU_ID, vector, error_code); #endif BX_INSTR_INTERRUPT(BX_CPU_ID, vector); invalidate_prefetch_q(); bx_bool soft_int = 0; switch(type) { case BX_SOFTWARE_INTERRUPT: case BX_SOFTWARE_EXCEPTION: soft_int = 1; break; case BX_PRIVILEGED_SOFTWARE_INTERRUPT: case BX_EXTERNAL_INTERRUPT: case BX_NMI: case BX_HARDWARE_EXCEPTION: break; default: BX_PANIC(("interrupt(): unknown exception type %d", type)); } BX_DEBUG(("interrupt(): vector = %02x, TYPE = %u, EXT = %u", vector, type, (unsigned) BX_CPU_THIS_PTR EXT)); // Discard any traps and inhibits for new context; traps will // resume upon return. BX_CPU_THIS_PTR debug_trap = 0; BX_CPU_THIS_PTR inhibit_mask = 0; #if BX_SUPPORT_VMX BX_CPU_THIS_PTR in_event = 1; #endif #if BX_SUPPORT_X86_64 if (long_mode()) { long_mode_int(vector, soft_int, push_error, error_code); } else #endif { RSP_SPECULATIVE; if(real_mode()) { real_mode_int(vector, push_error, error_code); } else { protected_mode_int(vector, soft_int, push_error, error_code); } RSP_COMMIT; } #if BX_X86_DEBUGGER BX_CPU_THIS_PTR in_repeat = 0; #endif #if BX_SUPPORT_VMX BX_CPU_THIS_PTR in_event = 0; #endif }
// 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 } }