diff --git a/src/arch/x86_64/macros.rs b/src/arch/x86_64/macros.rs index 22fe5a3cf649ade69d1cb04180acdb1d5738cd5d..5e9002fb62942d959021e0062a3ec7a81d8bea26 100644 --- a/src/arch/x86_64/macros.rs +++ b/src/arch/x86_64/macros.rs @@ -323,6 +323,10 @@ impl InterruptStack { self.iret.rflags &= !(1 << 8); } } + /// Checks if the trap flag is enabled, see `set_singlestep` + pub fn is_singlestep(&self) -> bool { + self.iret.rflags & 1 << 8 == 1 << 8 + } } macro_rules! interrupt_push { diff --git a/src/context/switch.rs b/src/context/switch.rs index 6e10825fcab4326d10a02bbf88d758b35740b773..851655757f6df75bd36cc870d719e107f1768bcc 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -1,10 +1,11 @@ use core::sync::atomic::Ordering; -use crate::context::{arch, contexts, Context, Status, CONTEXT_ID}; use crate::context::signal::signal_handler; +use crate::context::{arch, contexts, Context, Status, CONTEXT_ID}; use crate::gdt; -use crate::interrupt; use crate::interrupt::irq::PIT_TICKS; +use crate::interrupt; +use crate::ptrace; use crate::time; unsafe fn update(context: &mut Context, cpu_id: usize) { @@ -16,6 +17,8 @@ unsafe fn update(context: &mut Context, cpu_id: usize) { // Restore from signal, must only be done from another context to avoid overwriting the stack! if context.ksig_restore && ! context.running { + let was_singlestep = ptrace::regs_for(context).map(|s| s.is_singlestep()).unwrap_or(false); + let ksig = context.ksig.take().expect("context::switch: ksig not set with ksig_restore"); context.arch = ksig.0; @@ -33,6 +36,11 @@ unsafe fn update(context: &mut Context, cpu_id: usize) { context.ksig_restore = false; + // Keep singlestep flag across jumps + if let Some(regs) = ptrace::regs_for_mut(context) { + regs.set_singlestep(was_singlestep); + } + context.unblock(); }