demon/linux: x64 trap writing

This commit is contained in:
Ryan Fleury
2025-07-31 15:10:25 -07:00
parent 7c99e79981
commit a323aec6d0
+72
View File
@@ -1195,6 +1195,29 @@ dmn_ctrl_run(Arena *arena, DMN_CtrlCtx *ctx, DMN_RunCtrls *ctrls)
//
B32 need_wait_on_events = (evts.count == 0);
////////////////////////////
//- rjf: write all traps into memory
//
U8 *trap_swap_bytes = push_array_no_zero(scratch.arena, U8, ctrls->traps.trap_count);
ProfScope("write all traps into memory")
{
U64 trap_idx = 0;
for(DMN_TrapChunkNode *n = ctrls->traps.first; n != 0; n = n->next)
{
for(U64 n_idx = 0; n_idx < n->count; n_idx += 1, trap_idx += 1)
{
DMN_Trap *trap = n->v+n_idx;
if(trap->flags == 0)
{
trap_swap_bytes[trap_idx] = 0xCC;
dmn_process_read(trap->process, r1u64(trap->vaddr, trap->vaddr+1), trap_swap_bytes+trap_idx);
U8 int3 = 0xCC;
dmn_process_write(trap->process, r1u64(trap->vaddr, trap->vaddr+1), &int3);
}
}
}
}
////////////////////////////
//- rjf: gather all threads which we should run
//
@@ -1306,6 +1329,32 @@ dmn_ctrl_run(Arena *arena, DMN_CtrlCtx *ctx, DMN_RunCtrls *ctrls)
final_wait_pid = wait_id;
done = 1;
// NOTE(rjf): siginfo hint from old code:
#if 0
{
switch(siginfo.si_code)
{
// SI_KERNEL (hit int3; 0xCC)
case 0x80:
{
// TODO(rjf): breakpoint event
}break;
// +----------------------"breakpoint"
// |
// v----------v----------------------"hardware breakpoint"
// TRAP_UNK, TRAP_HWBKPT, TRAP_BRKPT, TRAP_TRACE
case 0x5: case 0x4: case 0x1: case 0x2:
{
// TODO(rjf): breakpoint event (?)
}break;
case 0x3: case 0x0:
{
// TODO(rjf): do nothing(?)
}break;
}
}
#endif
//- rjf: unpack event
int wifexited = WIFEXITED(status);
int wifsignaled = WIFSIGNALED(status);
@@ -1501,6 +1550,29 @@ dmn_ctrl_run(Arena *arena, DMN_CtrlCtx *ctx, DMN_RunCtrls *ctrls)
}
}
//////////////////////////
//- rjf: restore original memory at trap locations
//
ProfScope("restore original memory at trap locations")
{
U64 trap_idx = 0;
for(DMN_TrapChunkNode *n = ctrls->traps.first; n != 0; n = n->next)
{
for(U64 n_idx = 0; n_idx < n->count; n_idx += 1, trap_idx += 1)
{
DMN_Trap *trap = n->v+n_idx;
if(trap->flags == 0)
{
U8 og_byte = trap_swap_bytes[trap_idx];
if(og_byte != 0xCC)
{
dmn_process_write(trap->process, r1u64(trap->vaddr, trap->vaddr+1), &og_byte);
}
}
}
}
}
scratch_end(scratch);
}
return evts;