Skip to content

Commit

Permalink
Introduce KvmRunWrapper::as_ref()
Browse files Browse the repository at this point in the history
Introduce a new method to get an immutable reference to the kvm_run
struct. Replace uses of `as_mut_ref()` with `as_ref()` where possible.

Signed-off-by: Carlos López <[email protected]>
  • Loading branch information
00xc committed Nov 23, 2023
1 parent a569412 commit e44fcd4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/ioctls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl KvmRunWrapper {
}
}

impl AsRef<kvm_run> for KvmRunWrapper {
fn as_ref(&self) -> &kvm_run {
// SAFETY: Safe because we know we mapped enough memory to hold the kvm_run struct because
// the kernel told us how large it was.
unsafe { &*(self.kvm_run_ptr as *const kvm_run) }
}
}

impl Drop for KvmRunWrapper {
fn drop(&mut self) {
// SAFETY: This is safe because we mmap the area at kvm_run_ptr ourselves,
Expand Down
18 changes: 9 additions & 9 deletions src/ioctls/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ impl VcpuFd {
/// ```
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn sync_regs(&self) -> kvm_sync_regs {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
let kvm_run = self.kvm_run_ptr.as_ref();

// SAFETY: Accessing this union field could be out of bounds if the `kvm_run`
// allocation isn't large enough. The `kvm_run` region is set using
Expand Down Expand Up @@ -2663,9 +2663,9 @@ mod tests {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().immediate_exit, 0);
assert_eq!(vcpu.kvm_run_ptr.as_ref().immediate_exit, 0);
vcpu.set_kvm_immediate_exit(1);
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().immediate_exit, 1);
assert_eq!(vcpu.kvm_run_ptr.as_ref().immediate_exit, 1);
}

#[test]
Expand Down Expand Up @@ -2737,17 +2737,17 @@ mod tests {
];
for reg in &sync_regs {
vcpu.set_sync_valid_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().kvm_valid_regs, *reg as u64);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_valid_regs, *reg as u64);
vcpu.clear_sync_valid_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().kvm_valid_regs, 0);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_valid_regs, 0);
}

// Test that multiple valid SyncRegs can be set at the same time
vcpu.set_sync_valid_reg(SyncReg::Register);
vcpu.set_sync_valid_reg(SyncReg::SystemRegister);
vcpu.set_sync_valid_reg(SyncReg::VcpuEvents);
assert_eq!(
vcpu.kvm_run_ptr.as_mut_ref().kvm_valid_regs,
vcpu.kvm_run_ptr.as_ref().kvm_valid_regs,
SyncReg::Register as u64 | SyncReg::SystemRegister as u64 | SyncReg::VcpuEvents as u64
);

Expand All @@ -2760,17 +2760,17 @@ mod tests {

for reg in &sync_regs {
vcpu.set_sync_dirty_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().kvm_dirty_regs, *reg as u64);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs, *reg as u64);
vcpu.clear_sync_dirty_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_mut_ref().kvm_dirty_regs, 0);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs, 0);
}

// Test that multiple dirty SyncRegs can be set at the same time
vcpu.set_sync_dirty_reg(SyncReg::Register);
vcpu.set_sync_dirty_reg(SyncReg::SystemRegister);
vcpu.set_sync_dirty_reg(SyncReg::VcpuEvents);
assert_eq!(
vcpu.kvm_run_ptr.as_mut_ref().kvm_dirty_regs,
vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs,
SyncReg::Register as u64 | SyncReg::SystemRegister as u64 | SyncReg::VcpuEvents as u64
);
}
Expand Down

0 comments on commit e44fcd4

Please sign in to comment.