From 1005d74f00bd24120ca11039377b0be901f06c75 Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Thu, 21 Nov 2024 02:16:35 +0700 Subject: [PATCH] Removes kvm.cpp (#1124) --- gui/CMakeLists.txt | 3 +-- gui/core.cpp | 1 - gui/kvm.cpp | 21 --------------------- gui/src/hv/linux/cpu.rs | 20 +++----------------- gui/src/hv/linux/ffi.rs | 20 ++++++++++++++++++++ gui/src/hv/linux/x86_64.rs | 8 ++------ 6 files changed, 26 insertions(+), 47 deletions(-) delete mode 100644 gui/core.cpp delete mode 100644 gui/kvm.cpp diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 57422115a..4ac438912 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -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 @@ -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) diff --git a/gui/core.cpp b/gui/core.cpp deleted file mode 100644 index 1ecc69a37..000000000 --- a/gui/core.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "core.hpp" diff --git a/gui/kvm.cpp b/gui/kvm.cpp deleted file mode 100644 index aff5c2b1c..000000000 --- a/gui/kvm.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// This file contains KVM wrappers for Rust side. The reason we need these wrappers is because the -// KVM ioctls is not available in the libc binding. -#include "core.h" - -#include - -#include -#include -#include -#include - -#ifdef __x86_64__ -extern "C" int kvm_set_sregs(int vcpu, const kvm_sregs *regs) -{ - return ioctl(vcpu, KVM_SET_SREGS, regs); -} - -extern "C" int kvm_translate(int vcpu, kvm_translation *arg) { - return ioctl(vcpu, KVM_TRANSLATE, arg); -} -#endif diff --git a/gui/src/hv/linux/cpu.rs b/gui/src/hv/linux/cpu.rs index c116190cc..44bf833ad 100644 --- a/gui/src/hv/linux/cpu.rs +++ b/gui/src/hv/linux/cpu.rs @@ -68,6 +68,8 @@ impl<'a> Cpu for KvmCpu<'a> { #[cfg(target_arch = "x86_64")] fn translate(&self, vaddr: usize) -> Result { + use super::ffi::{KvmTranslation, KVM_TRANSLATE}; + let mut data = KvmTranslation { linear_address: vaddr, physical_address: 0, @@ -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()), } @@ -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; -} diff --git a/gui/src/hv/linux/ffi.rs b/gui/src/hv/linux/ffi.rs index 0acf3ccc2..df366e30b 100644 --- a/gui/src/hv/linux/ffi.rs +++ b/gui/src/hv/linux/ffi.rs @@ -17,6 +17,10 @@ pub const KVM_SET_REGS: c_ulong = _IOW::(KVMIO, 0x82); #[cfg(target_arch = "x86_64")] pub const KVM_GET_SREGS: c_ulong = _IOR::(KVMIO, 0x83); #[cfg(target_arch = "x86_64")] +pub const KVM_SET_SREGS: c_ulong = _IOW::(KVMIO, 0x84); +#[cfg(target_arch = "x86_64")] +pub const KVM_TRANSLATE: c_ulong = _IOWR::(KVMIO, 0x85); +#[cfg(target_arch = "x86_64")] pub const KVM_GET_FPU: c_ulong = _IOR::(KVMIO, 0x8c); #[cfg(target_arch = "x86_64")] pub const KVM_SET_CPUID2: c_ulong = _IOC(_IOC_WRITE, KVMIO, 0x90, 8); @@ -96,6 +100,11 @@ const fn _IOW(ty: c_ulong, nr: c_ulong) -> c_ulong { _IOC(_IOC_WRITE, ty, nr, size_of::() as _) } +#[allow(non_snake_case)] +const fn _IOWR(ty: c_ulong, nr: c_ulong) -> c_ulong { + _IOC(_IOC_READ | _IOC_WRITE, ty, nr, size_of::() 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) @@ -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 { diff --git a/gui/src/hv/linux/x86_64.rs b/gui/src/hv/linux/x86_64.rs index 5579b124c..bddd8a393 100644 --- a/gui/src/hv/linux/x86_64.rs +++ b/gui/src/hv/linux/x86_64.rs @@ -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; @@ -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())); } @@ -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; -}