Skip to content

Commit

Permalink
Check for interrupts after the execution of every instruction, instea…
Browse files Browse the repository at this point in the history
…d of only at the end of a code block
  • Loading branch information
ergo720 committed Jun 22, 2024
1 parent a27ad12 commit 59cd3a3
Show file tree
Hide file tree
Showing 22 changed files with 594 additions and 717 deletions.
24 changes: 14 additions & 10 deletions lib86cpu/core/breakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ cpu_check_watchpoint_overlap(cpu_t *cpu, addr_t addr, size_t size, int idx)
}

static void
cpu_check_watchpoints(cpu_t *cpu, addr_t addr, int dr_idx, int type, uint32_t eip)
cpu_check_watchpoints(cpu_t *cpu, addr_t addr, int dr_idx, int type)
{
bool match = false;
int dr7_type = cpu_get_watchpoint_type(cpu, dr_idx);
if (type == DR7_TYPE_DATA_W) {
if (((dr7_type == DR7_TYPE_DATA_W) || (dr7_type == DR7_TYPE_DATA_RW)) && !(cpu->cpu_flags & CPU_INHIBIT_DBG_TRAP)) {
if (((dr7_type == DR7_TYPE_DATA_W) || (dr7_type == DR7_TYPE_DATA_RW))) {
match = true;
}
}
Expand All @@ -54,36 +54,40 @@ cpu_check_watchpoints(cpu_t *cpu, addr_t addr, int dr_idx, int type, uint32_t ei
match = true;
}
}
else if ((type == dr7_type) && !(cpu->cpu_flags & CPU_INHIBIT_DBG_TRAP)) { // either DR7_TYPE_IO_RW or DR7_TYPE_DATA_RW
else if (type == dr7_type) { // either DR7_TYPE_IO_RW or DR7_TYPE_DATA_RW
match = true;
}

if (match) {
cpu->cpu_ctx.regs.dr[6] |= (1 << dr_idx);
cpu->cpu_ctx.exp_info.exp_data.fault_addr = addr;
cpu->cpu_ctx.exp_info.exp_data.fault_addr = 0;
cpu->cpu_ctx.exp_info.exp_data.code = 0;
cpu->cpu_ctx.exp_info.exp_data.idx = EXP_DB;
cpu->cpu_ctx.exp_info.exp_data.eip = eip;
throw host_exp_t::db_exp;
if (type == DR7_TYPE_INSTR) {
throw host_exp_t::db_exp;
}
else {
cpu->raise_int_fn(&cpu->cpu_ctx, CPU_DBG_TRAP_INT);
}
}
}

void
cpu_check_data_watchpoints(cpu_t *cpu, addr_t addr, size_t size, int type, uint32_t eip)
cpu_check_data_watchpoints(cpu_t *cpu, addr_t addr, size_t size, int type)
{
for (const auto &wp : cpu->wp_data) {
if ((wp.watch_addr <= (addr + size - 1)) && (addr <= wp.watch_end)) [[unlikely]] {
cpu_check_watchpoints(cpu, addr, wp.dr_idx, type, eip);
cpu_check_watchpoints(cpu, addr, wp.dr_idx, type);
}
}
}

void
cpu_check_io_watchpoints(cpu_t *cpu, port_t port, size_t size, int type, uint32_t eip)
cpu_check_io_watchpoints(cpu_t *cpu, port_t port, size_t size, int type)
{
for (const auto &wp : cpu->wp_io) {
if ((wp.watch_addr <= (port + size - 1)) && (port <= wp.watch_end)) [[unlikely]] {
cpu_check_watchpoints(cpu, port, wp.dr_idx, type, eip);
cpu_check_watchpoints(cpu, port, wp.dr_idx, type);
}
}
}
4 changes: 2 additions & 2 deletions lib86cpu/core/breakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#pragma once


void cpu_check_data_watchpoints(cpu_t *cpu, addr_t addr, size_t size, int type, uint32_t eip);
void cpu_check_io_watchpoints(cpu_t *cpu, port_t port, size_t size, int type, uint32_t eip);
void cpu_check_data_watchpoints(cpu_t *cpu, addr_t addr, size_t size, int type);
void cpu_check_io_watchpoints(cpu_t *cpu, port_t port, size_t size, int type);
bool cpu_check_watchpoint_enabled(cpu_t *cpu, int idx);
int cpu_get_watchpoint_type(cpu_t *cpu, int idx);
size_t cpu_get_watchpoint_length(cpu_t *cpu, int idx);
6 changes: 2 additions & 4 deletions lib86cpu/core/emitter/emitter_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#define CPU_CTX_HFLG offsetof(cpu_ctx_t, hflags)
#define CPU_CTX_EXP offsetof(cpu_ctx_t, exp_info)
#define CPU_CTX_INT offsetof(cpu_ctx_t, int_pending)
#define CPU_CTX_EXIT offsetof(cpu_ctx_t, exit_requested)
#define CPU_CTX_HALTED offsetof(cpu_ctx_t, is_halted)

#define CPU_CTX_EAX offsetof(cpu_ctx_t, regs.eax)
#define CPU_CTX_ECX offsetof(cpu_ctx_t, regs.ecx)
Expand Down Expand Up @@ -122,7 +120,6 @@
#define CPU_EXP_ADDR offsetof(cpu_ctx_t, exp_info.exp_data.fault_addr)
#define CPU_EXP_CODE offsetof(cpu_ctx_t, exp_info.exp_data.code)
#define CPU_EXP_IDX offsetof(cpu_ctx_t, exp_info.exp_data.idx)
#define CPU_EXP_EIP offsetof(cpu_ctx_t, exp_info.exp_data.eip)

#define REG_off(reg) get_reg_offset(reg)
#define REG_idx(reg) get_reg_idx(reg)
Expand Down Expand Up @@ -202,7 +199,8 @@ inline constexpr auto all_callable_funcs = std::make_tuple(
idivw_helper,
idivb_helper,
cpuid_helper,
hlt_helper,
hlt_helper<true>,
hlt_helper<false>,
fxsave_helper,
fxrstor_helper,
fpu_update_tag<true>,
Expand Down
Loading

0 comments on commit 59cd3a3

Please sign in to comment.