Skip to content

Commit

Permalink
Removes gdbstub dispatcher (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 28, 2024
1 parent c39be35 commit 252047a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 190 deletions.
2 changes: 1 addition & 1 deletion gui/src/hv/linux/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub union Exit {
fail_entry: ManuallyDrop<FailEntry>,
ex: ManuallyDrop<Ex>,
pub io: Io,
pub debug: ManuallyDrop<Debug>,
pub debug: Debug,
pub mmio: Mmio,
iocsr_io: ManuallyDrop<IocsrIo>,
hypercall: ManuallyDrop<Hypercall>,
Expand Down
25 changes: 20 additions & 5 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async fn run(args: ProgramArgs, exe: PathBuf) -> Result<(), ProgramError> {
};

// Wait for debugger.
let mut debugger = if let Some(addr) = debug {
let mut gdb_con = if let Some(addr) = debug {
let v = wait_for_debugger(addr).await?;

if v.is_none() {
Expand Down Expand Up @@ -243,17 +243,17 @@ async fn run(args: ProgramArgs, exe: PathBuf) -> Result<(), ProgramError> {
.build(&profile, attrs, &shutdown)
.map_err(ProgramError::BuildGraphicsEngine)?;
let (mut vmm, main) = self::vmm::create_channel();
let mut buf = [0; 1024];
let mut gdb_in = [0; 1024];

loop {
// Prepare futures to poll.
let mut vmm = pin!(vmm.recv());
let mut debugger = debugger.as_mut().map(|v| v.read(&mut buf));
let mut debug = gdb_con.as_mut().map(|v| v.read(&mut gdb_in));

// Poll all futures.
let (vmm, debug) = std::future::poll_fn(move |cx| {
let vmm = vmm.as_mut().poll(cx);
let debug = match &mut debugger {
let debug = match &mut debug {
Some(v) => v.poll_unpin(cx),
None => Poll::Pending,
};
Expand All @@ -267,8 +267,23 @@ async fn run(args: ProgramArgs, exe: PathBuf) -> Result<(), ProgramError> {
})
.await;

todo!()
// Process VMM event.
if let Some(vmm) = vmm {
let vmm = match vmm {
Some(v) => v,
None => break,
};

todo!()
}

// Process debugger requests.
if let Some(debug) = debug {
todo!()
}
}

Ok(())
}

async fn run_launcher(
Expand Down
95 changes: 0 additions & 95 deletions gui/src/vmm/debug/mod.rs

This file was deleted.

92 changes: 3 additions & 89 deletions gui/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use self::ram::RamBuilder;
use crate::gdb::DebugClient;
use crate::hv::{Hypervisor, Ram};
use crate::profile::Profile;
use cpu::GdbError;
use gdbstub::common::Signal;
use gdbstub::stub::state_machine::GdbStubStateMachine;
use gdbstub::stub::{GdbStubError, MultiThreadStopReason};
use gdbstub::stub::MultiThreadStopReason;
use kernel::{KernelError, ProgramHeaderError};
use obconf::{BootEnv, ConsoleType, Vm};
use std::cmp::max;
Expand All @@ -32,7 +29,6 @@ use winit::event_loop::EventLoopProxy;
mod arch;
mod channel;
mod cpu;
mod debug;
mod hw;
mod kernel;
mod ram;
Expand Down Expand Up @@ -62,7 +58,6 @@ fn get_page_size() -> Result<NonZero<usize>, std::io::Error> {
/// Manage a virtual machine that run the kernel.
pub struct Vmm<'a, 'b> {
cpu: CpuManager<'a, 'b, crate::hv::Default>, // Drop first.
gdb: Option<GdbStubStateMachine<'static, CpuManager<'a, 'b, crate::hv::Default>, DebugClient>>,
shutdown: Arc<AtomicBool>,
}

Expand Down Expand Up @@ -296,74 +291,15 @@ impl<'a, 'b> Vmm<'a, 'b> {
let shutdown = Arc::new(AtomicBool::new(false));
let mut cpu = CpuManager::new(Arc::new(hv), args.el, scope, devices, shutdown.clone());

// Setup GDB stub.
let gdb = debugger
.map(|client| {
gdbstub::stub::GdbStub::new(client)
.run_state_machine(&mut cpu)
.map_err(VmmError::SetupGdbStub)
})
.transpose()?;

// Spawn main CPU.
cpu.spawn(
map.kern_vaddr + kernel_img.entry(),
Some(map),
gdb.is_some(),
debugger.is_some(),
);

// Create VMM.
Ok(Self { cpu, gdb, shutdown })
}

fn dispatch_debug(
&mut self,
mut stop: Option<MultiThreadStopReason<u64>>,
) -> Result<DispatchDebugResult, DispatchDebugError> {
loop {
// Check current state.
let r = match self.gdb.take().unwrap() {
GdbStubStateMachine::Idle(s) => {
match self.cpu.dispatch_gdb_idle(s) {
Ok(Ok(v)) => Ok(v),
Ok(Err(v)) => {
// No pending data from the debugger.
self.gdb = Some(v.into());
return Ok(DispatchDebugResult::Ok);
}
Err(e) => Err(DispatchDebugError::DispatchIdle(e)),
}
}
GdbStubStateMachine::Running(s) => {
match self.cpu.dispatch_gdb_running(s, stop.take()) {
Ok(Ok(v)) => Ok(v),
Ok(Err(v)) => {
// No pending data from the debugger.
self.gdb = Some(v.into());
return Ok(DispatchDebugResult::Ok);
}
Err(e) => Err(DispatchDebugError::DispatchRunning(e)),
}
}
GdbStubStateMachine::CtrlCInterrupt(s) => {
self.cpu.lock();

s.interrupt_handled(
&mut self.cpu,
Some(MultiThreadStopReason::Signal(Signal::SIGINT)),
)
.map_err(DispatchDebugError::HandleInterrupt)
}
GdbStubStateMachine::Disconnected(_) => {
return Ok(DispatchDebugResult::Disconnected)
}
};

match r {
Ok(v) => self.gdb = Some(v),
Err(e) => return Err(e),
}
}
Ok(Self { cpu, shutdown })
}
}

Expand Down Expand Up @@ -406,23 +342,6 @@ impl VmmEvent {
}
}

pub enum DispatchDebugResult {
Ok,
Disconnected,
}

#[derive(Debug, Error)]
enum DispatchDebugError {
#[error("couldn't dispatch idle state")]
DispatchIdle(#[source] debug::DispatchGdbIdleError),

#[error("couldn't dispatch running state")]
DispatchRunning(#[source] debug::DispatchGdbRunningError),

#[error("couldn't handle CTRL+C interrupt")]
HandleInterrupt(#[source] gdbstub::stub::GdbStubError<GdbError, std::io::Error>),
}

#[derive(Debug, Error)]
pub enum VmmError {
#[error("couldn't open kernel path {1}")]
Expand Down Expand Up @@ -523,11 +442,6 @@ pub enum VmmError {

#[error("couldn't build RAM")]
BuildRam(#[source] ram::RamBuilderError),

#[error("couldn't setup a GDB stub")]
SetupGdbStub(
#[source] GdbStubError<GdbError, <DebugClient as gdbstub::conn::Connection>::Error>,
),
}

/// Represents an error when [`main_cpu()`] fails to reach event loop.
Expand Down

0 comments on commit 252047a

Please sign in to comment.