Skip to content

Commit

Permalink
Removes kvm.cpp (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Nov 20, 2024
1 parent 89dfe18 commit 1005d74
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 47 deletions.
3 changes: 1 addition & 2 deletions gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ endif()
# Setup GUI.
add_executable(obliteration WIN32 MACOSX_BUNDLE
app_data.cpp
core.cpp
cpu_settings.cpp
display_settings.cpp
game_models.cpp
Expand All @@ -37,7 +36,7 @@ if(WIN32)
elseif(APPLE)
target_sources(obliteration PRIVATE resources/obliteration.icns)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_sources(obliteration PRIVATE kvm.cpp vulkan.cpp)
target_sources(obliteration PRIVATE vulkan.cpp)
endif()

set_target_properties(obliteration PROPERTIES AUTOMOC ON AUTORCC ON)
Expand Down
1 change: 0 additions & 1 deletion gui/core.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions gui/kvm.cpp

This file was deleted.

20 changes: 3 additions & 17 deletions gui/src/hv/linux/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ impl<'a> Cpu for KvmCpu<'a> {

#[cfg(target_arch = "x86_64")]
fn translate(&self, vaddr: usize) -> Result<usize, std::io::Error> {
use super::ffi::{KvmTranslation, KVM_TRANSLATE};

let mut data = KvmTranslation {
linear_address: vaddr,
physical_address: 0,
Expand All @@ -77,7 +79,7 @@ impl<'a> Cpu for KvmCpu<'a> {
pad: [0; 5],
};

match unsafe { kvm_translate(self.fd.as_raw_fd(), &mut data) } {
match unsafe { ioctl(self.fd.as_raw_fd(), KVM_TRANSLATE, &mut data) } {
0 => Ok(data.physical_address),
_ => return Err(std::io::Error::last_os_error()),
}
Expand Down Expand Up @@ -183,19 +185,3 @@ impl<'a, 'b> CpuDebug for KvmDebug<'a, 'b> {
self.0
}
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
struct KvmTranslation {
linear_address: usize,
physical_address: usize,
valid: u8,
writeable: u8,
usermode: u8,
pad: [u8; 5],
}

extern "C" {
#[cfg(target_arch = "x86_64")]
fn kvm_translate(vcpu: std::ffi::c_int, arg: *mut KvmTranslation) -> std::ffi::c_int;
}
20 changes: 20 additions & 0 deletions gui/src/hv/linux/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub const KVM_SET_REGS: c_ulong = _IOW::<KvmRegs>(KVMIO, 0x82);
#[cfg(target_arch = "x86_64")]
pub const KVM_GET_SREGS: c_ulong = _IOR::<KvmSregs>(KVMIO, 0x83);
#[cfg(target_arch = "x86_64")]
pub const KVM_SET_SREGS: c_ulong = _IOW::<KvmSregs>(KVMIO, 0x84);
#[cfg(target_arch = "x86_64")]
pub const KVM_TRANSLATE: c_ulong = _IOWR::<KvmTranslation>(KVMIO, 0x85);
#[cfg(target_arch = "x86_64")]
pub const KVM_GET_FPU: c_ulong = _IOR::<KvmFpu>(KVMIO, 0x8c);
#[cfg(target_arch = "x86_64")]
pub const KVM_SET_CPUID2: c_ulong = _IOC(_IOC_WRITE, KVMIO, 0x90, 8);
Expand Down Expand Up @@ -96,6 +100,11 @@ const fn _IOW<T>(ty: c_ulong, nr: c_ulong) -> c_ulong {
_IOC(_IOC_WRITE, ty, nr, size_of::<T>() as _)
}

#[allow(non_snake_case)]
const fn _IOWR<T>(ty: c_ulong, nr: c_ulong) -> c_ulong {
_IOC(_IOC_READ | _IOC_WRITE, ty, nr, size_of::<T>() as _)
}

#[allow(non_snake_case)]
const fn _IOC(dir: c_ulong, ty: c_ulong, nr: c_ulong, size: c_ulong) -> c_ulong {
(dir << _IOC_DIRSHIFT)
Expand Down Expand Up @@ -180,6 +189,17 @@ pub struct KvmSregs {
pub interrupt_bitmap: [u64; (KVM_NR_INTERRUPTS + 63) / 64],
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
pub struct KvmTranslation {
pub linear_address: usize,
pub physical_address: usize,
pub valid: u8,
pub writeable: u8,
pub usermode: u8,
pub pad: [u8; 5],
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
pub struct KvmSegment {
Expand Down
8 changes: 2 additions & 6 deletions gui/src/hv/linux/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::ffi::{
KvmFpu, KvmRegs, KvmSregs, KVM_GET_FPU, KVM_GET_REGS, KVM_GET_SREGS, KVM_SET_REGS,
KVM_SET_SREGS,
};
use crate::hv::{CpuCommit, CpuStates};
use libc::ioctl;
use std::ffi::c_int;
use std::mem::MaybeUninit;
use std::os::fd::{AsRawFd, OwnedFd};
use thiserror::Error;
Expand Down Expand Up @@ -375,7 +375,7 @@ impl<'a> CpuCommit for KvmStates<'a> {
}

// Set special registers.
if unsafe { self.sdirty && kvm_set_sregs(self.cpu.as_raw_fd(), &self.sregs) != 0 } {
if unsafe { self.sdirty && ioctl(self.cpu.as_raw_fd(), KVM_SET_SREGS, &self.sregs) < 0 } {
return Err(StatesError::SetSRegsFailed(Error::last_os_error()));
}

Expand All @@ -401,7 +401,3 @@ pub enum StatesError {
#[error("couldn't set special registers")]
SetSRegsFailed(#[source] std::io::Error),
}

extern "C" {
fn kvm_set_sregs(vcpu: c_int, regs: *const KvmSregs) -> c_int;
}

0 comments on commit 1005d74

Please sign in to comment.