Skip to content

Commit 2471eea

Browse files
committed
refactor: remove duplicate VcpuFds from GDB
Since we store VcpuFds in VcpuHandles inside Vmm, and GDB stub has access to Vmm, remove redundant duplication of VcpuFds. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent cd4564a commit 2471eea

File tree

6 files changed

+109
-107
lines changed

6 files changed

+109
-107
lines changed

src/vmm/src/builder.rs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,6 @@ pub fn build_microvm_for_boot(
182182
let entry_point = load_kernel(&boot_config.kernel_file, vm.guest_memory())?;
183183
let initrd = InitrdConfig::from_config(boot_config, vm.guest_memory())?;
184184

185-
#[cfg(feature = "gdb")]
186-
let (gdb_tx, gdb_rx) = mpsc::channel();
187-
#[cfg(feature = "gdb")]
188-
vcpus
189-
.iter_mut()
190-
.for_each(|vcpu| vcpu.attach_debug_info(gdb_tx.clone()));
191-
#[cfg(feature = "gdb")]
192-
let vcpu_fds = vcpus
193-
.iter()
194-
.map(|vcpu| vcpu.copy_kvm_vcpu_fd(&vm))
195-
.collect::<Result<Vec<_>, _>>()?;
196-
197185
if vm_resources.pci_enabled {
198186
device_manager.enable_pci(&vm)?;
199187
} else {
@@ -281,7 +269,7 @@ pub fn build_microvm_for_boot(
281269
boot_cmdline,
282270
)?;
283271

284-
let mut vmm = Vmm {
272+
let vmm = Vmm {
285273
instance_info: instance_info.clone(),
286274
shutdown_exit_code: None,
287275
kvm,
@@ -291,33 +279,35 @@ pub fn build_microvm_for_boot(
291279
vcpus_exit_evt,
292280
device_manager,
293281
};
294-
295-
// Move vcpus to their own threads and start their state machine in the 'Paused' state.
296-
vmm.start_vcpus(
297-
vcpus,
298-
seccomp_filters
299-
.get("vcpu")
300-
.ok_or_else(|| StartMicrovmError::MissingSeccompFilters("vcpu".to_string()))?
301-
.clone(),
302-
)
303-
.map_err(VmmError::VcpuStart)?;
304-
305282
let vmm = Arc::new(Mutex::new(vmm));
306283

307284
#[cfg(feature = "gdb")]
308-
if let Some(gdb_socket_path) = &vm_resources.machine_config.gdb_socket_path {
309-
gdb::gdb_thread(
310-
vmm.clone(),
311-
vcpu_fds,
312-
gdb_rx,
313-
entry_point.entry_addr,
314-
gdb_socket_path,
315-
)
316-
.map_err(StartMicrovmError::GdbServer)?;
317-
} else {
318-
debug!("No GDB socket provided not starting gdb server.");
285+
{
286+
let (gdb_tx, gdb_rx) = mpsc::channel();
287+
vcpus
288+
.iter_mut()
289+
.for_each(|vcpu| vcpu.attach_debug_info(gdb_tx.clone()));
290+
291+
if let Some(gdb_socket_path) = &vm_resources.machine_config.gdb_socket_path {
292+
gdb::gdb_thread(vmm.clone(), gdb_rx, entry_point.entry_addr, gdb_socket_path)
293+
.map_err(StartMicrovmError::GdbServer)?;
294+
} else {
295+
debug!("No GDB socket provided not starting gdb server.");
296+
}
319297
}
320298

299+
// Move vcpus to their own threads and start their state machine in the 'Paused' state.
300+
vmm.lock()
301+
.unwrap()
302+
.start_vcpus(
303+
vcpus,
304+
seccomp_filters
305+
.get("vcpu")
306+
.ok_or_else(|| StartMicrovmError::MissingSeccompFilters("vcpu".to_string()))?
307+
.clone(),
308+
)
309+
.map_err(VmmError::VcpuStart)?;
310+
321311
// Load seccomp filters for the VMM thread.
322312
// Execution panics if filters cannot be loaded, use --no-seccomp if skipping filters
323313
// altogether is the desired behaviour.

src/vmm/src/gdb/event_loop.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use gdbstub::conn::{Connection, ConnectionExt};
1111
use gdbstub::stub::run_blocking::{self, WaitForStopReasonError};
1212
use gdbstub::stub::{DisconnectReason, GdbStub, MultiThreadStopReason};
1313
use gdbstub::target::Target;
14-
use kvm_ioctls::VcpuFd;
1514
use vm_memory::GuestAddress;
1615

1716
use super::target::{FirecrackerTarget, GdbTargetError, vcpuid_to_tid};
@@ -22,11 +21,10 @@ use crate::logger::{error, trace};
2221
pub fn event_loop(
2322
connection: UnixStream,
2423
vmm: Arc<Mutex<Vmm>>,
25-
vcpu_fds: Vec<VcpuFd>,
2624
gdb_event_receiver: Receiver<usize>,
2725
entry_addr: GuestAddress,
2826
) {
29-
let target = FirecrackerTarget::new(vmm, vcpu_fds, gdb_event_receiver, entry_addr);
27+
let target = FirecrackerTarget::new(vmm, gdb_event_receiver, entry_addr);
3028
let connection: Box<dyn ConnectionExt<Error = std::io::Error>> = { Box::new(connection) };
3129
let debugger = GdbStub::new(connection);
3230

src/vmm/src/gdb/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::sync::{Arc, Mutex};
1515

1616
use arch::vcpu_set_debug;
1717
use event_loop::event_loop;
18-
use kvm_ioctls::VcpuFd;
1918
use target::GdbTargetError;
2019
use vm_memory::GuestAddress;
2120

@@ -35,7 +34,6 @@ use crate::logger::trace;
3534
/// communcation to the GDB server
3635
pub fn gdb_thread(
3736
vmm: Arc<Mutex<Vmm>>,
38-
vcpu_fds: Vec<VcpuFd>,
3937
gdb_event_receiver: Receiver<usize>,
4038
entry_addr: GuestAddress,
4139
socket_addr: &str,
@@ -44,10 +42,12 @@ pub fn gdb_thread(
4442
// to be stopped as it connects. This also allows us to set breakpoints before kernel starts.
4543
// This entry adddress is automatically used as it is not tracked inside the target state, so
4644
// when resumed will be removed
47-
vcpu_set_debug(&vcpu_fds[0], &[entry_addr], false)?;
48-
49-
for vcpu_fd in &vcpu_fds[1..] {
50-
vcpu_set_debug(vcpu_fd, &[], false)?;
45+
{
46+
let vmm = vmm.lock().unwrap();
47+
vcpu_set_debug(&vmm.vcpus_handles[0].vcpu_fd, &[entry_addr], false)?;
48+
for handle in &vmm.vcpus_handles[1..] {
49+
vcpu_set_debug(&handle.vcpu_fd, &[], false)?;
50+
}
5151
}
5252

5353
let path = Path::new(socket_addr);
@@ -59,7 +59,7 @@ pub fn gdb_thread(
5959

6060
std::thread::Builder::new()
6161
.name("gdb".into())
62-
.spawn(move || event_loop(connection, vmm, vcpu_fds, gdb_event_receiver, entry_addr))
62+
.spawn(move || event_loop(connection, vmm, gdb_event_receiver, entry_addr))
6363
.map_err(|_| GdbTargetError::GdbThreadError)?;
6464

6565
Ok(())

0 commit comments

Comments
 (0)