Example #1
0
// 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
  }
}
Example #2
0
// 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
}
Example #3
0
void logfunctions::ask(int level, const char *prefix, const char *fmt, va_list ap)
{
  // Guard against reentry on ask() function.  The danger is that some
  // function that's called within ask() could trigger another
  // BX_PANIC that could call ask() again, leading to infinite
  // recursion and infinite asks.
  static char in_ask_already = 0;
  char buf1[1024];
  if (in_ask_already) {
    fprintf(stderr, "logfunctions::ask() should not reenter!!\n");
    return;
  }
  in_ask_already = 1;
  vsnprintf(buf1, sizeof(buf1), fmt, ap);
  // FIXME: facility set to 0 because it's unknown.

  // update vga screen.  This is useful because sometimes useful messages
  // are printed on the screen just before a panic.  It's also potentially
  // dangerous if this function calls ask again...  That's why I added
  // the reentry check above.
  if (SIM->get_init_done()) DEV_vga_refresh();

#if !BX_EXTERNAL_DEBUGGER
  // ensure the text screen is showing
  SIM->set_display_mode(DISP_MODE_CONFIG);
  int val = SIM->log_msg(prefix, level, buf1);
  switch(val)
  {
    case BX_LOG_ASK_CHOICE_CONTINUE:
      break;
    case BX_LOG_ASK_CHOICE_CONTINUE_ALWAYS:
      // user said continue, and don't "ask" for this facility again.
      setonoff(level, ACT_REPORT);
      break;
    case BX_LOG_ASK_CHOICE_DIE:
    case BX_LOG_NOTIFY_FAILED:
      bx_user_quit = (val==BX_LOG_ASK_CHOICE_DIE)?1:0;
      in_ask_already = 0;  // because fatal will longjmp out
      fatal(prefix, buf1, ap, 1);
      // should never get here
      BX_PANIC(("in ask(), fatal() should never return!"));
      break;
    case BX_LOG_ASK_CHOICE_DUMP_CORE:
      fprintf(stderr, "User chose to dump core...\n");
#if BX_HAVE_ABORT
      abort();
#else
      // do something highly illegal that should kill the process.
      // Hey, this is fun!
      {
        char *crashptr = (char *)0; char c = *crashptr;
      }
      fprintf(stderr, "Sorry, I couldn't find your abort() function.  Exiting.");
      exit(0);
#endif
#if BX_DEBUGGER
    case BX_LOG_ASK_CHOICE_ENTER_DEBUG:
      // user chose debugger.  To "drop into the debugger" we just set the
      // interrupt_requested bit and continue execution.  Before the next
      // instruction, it should notice the user interrupt and return to
      // the debugger.
      bx_debug_break();
      break;
#elif BX_GDBSTUB
    case BX_LOG_ASK_CHOICE_ENTER_DEBUG:
      bx_gdbstub_break();
      break;
#endif
    default:
      // this happens if panics happen before the callback is initialized
      // in gui/control.cc.
      fprintf(stderr, "WARNING: log_msg returned unexpected value %d\n", val);
  }
#else
  // external debugger ask code goes here
#endif
  // return to simulation mode
  SIM->set_display_mode(DISP_MODE_SIM);
  in_ask_already = 0;
}