102343d0bSLoGin use crate::driver::base::device::device_number::DeviceNumber; 240314b30SXiaoye Zheng use crate::filesystem::devfs::{DevFS, DeviceINode}; 340314b30SXiaoye Zheng use crate::filesystem::vfs::{ 440314b30SXiaoye Zheng core::generate_inode_id, 540314b30SXiaoye Zheng file::{File, FileMode}, 602343d0bSLoGin FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 740314b30SXiaoye Zheng }; 8dfe53cf0SGnoCiYeH use crate::libs::spinlock::SpinLockGuard; 940314b30SXiaoye Zheng use crate::process::ProcessManager; 106fc066acSJomo use crate::{arch::KVMArch, libs::spinlock::SpinLock, time::PosixTimeSpec}; 1140314b30SXiaoye Zheng use crate::{filesystem, kdebug}; 1240314b30SXiaoye Zheng // use crate::virt::kvm::{host_stack}; 1340314b30SXiaoye Zheng use super::push_vm; 1440314b30SXiaoye Zheng use crate::virt::kvm::vm_dev::LockedVmInode; 1540314b30SXiaoye Zheng use alloc::{ 1640314b30SXiaoye Zheng string::String, 1740314b30SXiaoye Zheng sync::{Arc, Weak}, 1840314b30SXiaoye Zheng vec::Vec, 1940314b30SXiaoye Zheng }; 2091e9d4abSLoGin use system_error::SystemError; 2140314b30SXiaoye Zheng 2240314b30SXiaoye Zheng pub const KVM_API_VERSION: u32 = 12; 2340314b30SXiaoye Zheng 2440314b30SXiaoye Zheng // use crate::virt::kvm::kvm_dev_ioctl_create_vm; 2540314b30SXiaoye Zheng /* 2640314b30SXiaoye Zheng * ioctls for /dev/kvm fds: 2740314b30SXiaoye Zheng */ 2840314b30SXiaoye Zheng pub const KVM_GET_API_VERSION: u32 = 0x00; 2940314b30SXiaoye Zheng pub const KVM_CREATE_VM: u32 = 0x01; 3040314b30SXiaoye Zheng pub const KVM_CHECK_EXTENSION: u32 = 0x03; 3140314b30SXiaoye Zheng pub const KVM_GET_VCPU_MMAP_SIZE: u32 = 0x04; // Get size for mmap(vcpu_fd) in bytes 3240314b30SXiaoye Zheng pub const KVM_TRACE_ENABLE: u32 = 0x05; 3340314b30SXiaoye Zheng pub const KVM_TRACE_PAUSE: u32 = 0x06; 3440314b30SXiaoye Zheng pub const KVM_TRACE_DISABLE: u32 = 0x07; 3540314b30SXiaoye Zheng 3640314b30SXiaoye Zheng #[derive(Debug)] 3740314b30SXiaoye Zheng pub struct KvmInode { 3840314b30SXiaoye Zheng /// uuid 暂时不知道有什么用(x 3940314b30SXiaoye Zheng // uuid: Uuid, 4040314b30SXiaoye Zheng /// 指向自身的弱引用 4140314b30SXiaoye Zheng self_ref: Weak<LockedKvmInode>, 4240314b30SXiaoye Zheng /// 指向inode所在的文件系统对象的指针 4340314b30SXiaoye Zheng fs: Weak<DevFS>, 4440314b30SXiaoye Zheng /// INode 元数据 4540314b30SXiaoye Zheng metadata: Metadata, 4640314b30SXiaoye Zheng } 4740314b30SXiaoye Zheng 4840314b30SXiaoye Zheng #[derive(Debug)] 4940314b30SXiaoye Zheng pub struct LockedKvmInode(SpinLock<KvmInode>); 5040314b30SXiaoye Zheng 5140314b30SXiaoye Zheng impl LockedKvmInode { 5240314b30SXiaoye Zheng pub fn new() -> Arc<Self> { 5340314b30SXiaoye Zheng let inode = KvmInode { 5440314b30SXiaoye Zheng self_ref: Weak::default(), 5540314b30SXiaoye Zheng fs: Weak::default(), 5640314b30SXiaoye Zheng metadata: Metadata { 5740314b30SXiaoye Zheng dev_id: 1, 5840314b30SXiaoye Zheng inode_id: generate_inode_id(), 5940314b30SXiaoye Zheng size: 0, 6040314b30SXiaoye Zheng blk_size: 0, 6140314b30SXiaoye Zheng blocks: 0, 626fc066acSJomo atime: PosixTimeSpec::default(), 636fc066acSJomo mtime: PosixTimeSpec::default(), 646fc066acSJomo ctime: PosixTimeSpec::default(), 6540314b30SXiaoye Zheng file_type: FileType::KvmDevice, // 文件夹,block设备,char设备 6640314b30SXiaoye Zheng mode: filesystem::vfs::syscall::ModeType::S_IALLUGO, 6740314b30SXiaoye Zheng nlinks: 1, 6840314b30SXiaoye Zheng uid: 0, 6940314b30SXiaoye Zheng gid: 0, 7002343d0bSLoGin raw_dev: DeviceNumber::default(), // 这里用来作为device number 7140314b30SXiaoye Zheng }, 7240314b30SXiaoye Zheng }; 7340314b30SXiaoye Zheng 7440314b30SXiaoye Zheng let result = Arc::new(LockedKvmInode(SpinLock::new(inode))); 7540314b30SXiaoye Zheng result.0.lock().self_ref = Arc::downgrade(&result); 7640314b30SXiaoye Zheng 7740314b30SXiaoye Zheng return result; 7840314b30SXiaoye Zheng } 7940314b30SXiaoye Zheng } 8040314b30SXiaoye Zheng 8140314b30SXiaoye Zheng impl DeviceINode for LockedKvmInode { 8240314b30SXiaoye Zheng fn set_fs(&self, fs: Weak<DevFS>) { 8340314b30SXiaoye Zheng self.0.lock().fs = fs; 8440314b30SXiaoye Zheng } 8540314b30SXiaoye Zheng } 8640314b30SXiaoye Zheng 8740314b30SXiaoye Zheng impl IndexNode for LockedKvmInode { 8840314b30SXiaoye Zheng fn as_any_ref(&self) -> &dyn core::any::Any { 8940314b30SXiaoye Zheng self 9040314b30SXiaoye Zheng } 9140314b30SXiaoye Zheng 92dfe53cf0SGnoCiYeH fn open( 93dfe53cf0SGnoCiYeH &self, 94dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 95dfe53cf0SGnoCiYeH _mode: &FileMode, 96dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 9740314b30SXiaoye Zheng kdebug!("file private data:{:?}", _data); 9840314b30SXiaoye Zheng return Ok(()); 9940314b30SXiaoye Zheng } 10040314b30SXiaoye Zheng 101dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 10240314b30SXiaoye Zheng return Ok(()); 10340314b30SXiaoye Zheng } 10440314b30SXiaoye Zheng 10540314b30SXiaoye Zheng fn metadata(&self) -> Result<Metadata, SystemError> { 10640314b30SXiaoye Zheng return Ok(self.0.lock().metadata.clone()); 10740314b30SXiaoye Zheng } 10840314b30SXiaoye Zheng 10940314b30SXiaoye Zheng fn fs(&self) -> Arc<dyn FileSystem> { 11040314b30SXiaoye Zheng return self.0.lock().fs.upgrade().unwrap(); 11140314b30SXiaoye Zheng } 11240314b30SXiaoye Zheng 11340314b30SXiaoye Zheng fn list(&self) -> Result<Vec<String>, SystemError> { 114*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 11540314b30SXiaoye Zheng } 11640314b30SXiaoye Zheng 11740314b30SXiaoye Zheng fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 11840314b30SXiaoye Zheng let mut inode = self.0.lock(); 11940314b30SXiaoye Zheng inode.metadata.atime = metadata.atime; 12040314b30SXiaoye Zheng inode.metadata.mtime = metadata.mtime; 12140314b30SXiaoye Zheng inode.metadata.ctime = metadata.ctime; 12240314b30SXiaoye Zheng inode.metadata.mode = metadata.mode; 12340314b30SXiaoye Zheng inode.metadata.uid = metadata.uid; 12440314b30SXiaoye Zheng inode.metadata.gid = metadata.gid; 12540314b30SXiaoye Zheng 12640314b30SXiaoye Zheng return Ok(()); 12740314b30SXiaoye Zheng } 12840314b30SXiaoye Zheng 12940314b30SXiaoye Zheng /// @brief io control接口 13040314b30SXiaoye Zheng /// 13140314b30SXiaoye Zheng /// @param cmd 命令 13240314b30SXiaoye Zheng /// @param data 数据 13340314b30SXiaoye Zheng /// 13440314b30SXiaoye Zheng /// @return 成功:Ok() 13540314b30SXiaoye Zheng /// 失败:Err(错误码) 13652da9a59SGnoCiYeH fn ioctl( 13752da9a59SGnoCiYeH &self, 13852da9a59SGnoCiYeH cmd: u32, 13952da9a59SGnoCiYeH data: usize, 14052da9a59SGnoCiYeH _private_data: &FilePrivateData, 14152da9a59SGnoCiYeH ) -> Result<usize, SystemError> { 14240314b30SXiaoye Zheng match cmd { 14340314b30SXiaoye Zheng 0xdeadbeef => { 14440314b30SXiaoye Zheng kdebug!("kvm ioctl"); 14540314b30SXiaoye Zheng Ok(0) 14640314b30SXiaoye Zheng } 14740314b30SXiaoye Zheng KVM_GET_API_VERSION => Ok(KVM_API_VERSION as usize), 14840314b30SXiaoye Zheng KVM_CREATE_VM => { 14940314b30SXiaoye Zheng kdebug!("kvm KVM_CREATE_VM"); 15040314b30SXiaoye Zheng kvm_dev_ioctl_create_vm(data) 15140314b30SXiaoye Zheng } 15240314b30SXiaoye Zheng KVM_CHECK_EXTENSION 15340314b30SXiaoye Zheng | KVM_GET_VCPU_MMAP_SIZE 15440314b30SXiaoye Zheng | KVM_TRACE_ENABLE 15540314b30SXiaoye Zheng | KVM_TRACE_PAUSE 156*1074eb34SSamuel Dai | KVM_TRACE_DISABLE => Err(SystemError::ENOSYS), 15740314b30SXiaoye Zheng _ => KVMArch::kvm_arch_dev_ioctl(cmd, data), 15840314b30SXiaoye Zheng } 15940314b30SXiaoye Zheng } 16040314b30SXiaoye Zheng /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 16140314b30SXiaoye Zheng fn read_at( 16240314b30SXiaoye Zheng &self, 16340314b30SXiaoye Zheng _offset: usize, 16440314b30SXiaoye Zheng _len: usize, 16540314b30SXiaoye Zheng _buf: &mut [u8], 166dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 16740314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 168*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 16940314b30SXiaoye Zheng } 17040314b30SXiaoye Zheng 17140314b30SXiaoye Zheng /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 17240314b30SXiaoye Zheng fn write_at( 17340314b30SXiaoye Zheng &self, 17440314b30SXiaoye Zheng _offset: usize, 17540314b30SXiaoye Zheng _len: usize, 17640314b30SXiaoye Zheng _buf: &[u8], 177dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 17840314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 179*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 18040314b30SXiaoye Zheng } 18140314b30SXiaoye Zheng } 18240314b30SXiaoye Zheng 18340314b30SXiaoye Zheng #[no_mangle] 18440314b30SXiaoye Zheng pub fn kvm_dev_ioctl_create_vm(_vmtype: usize) -> Result<usize, SystemError> { 18540314b30SXiaoye Zheng push_vm(0).expect("need a valid vm!"); 18640314b30SXiaoye Zheng 18740314b30SXiaoye Zheng // 创建vm文件,返回文件描述符 18840314b30SXiaoye Zheng let vm_inode = LockedVmInode::new(); 18940314b30SXiaoye Zheng let file: File = File::new(vm_inode, FileMode::O_RDWR)?; 19040314b30SXiaoye Zheng let r = ProcessManager::current_pcb() 19140314b30SXiaoye Zheng .fd_table() 19240314b30SXiaoye Zheng .write() 19340314b30SXiaoye Zheng .alloc_fd(file, None) 19440314b30SXiaoye Zheng .map(|fd| fd as usize); 19540314b30SXiaoye Zheng return r; 19640314b30SXiaoye Zheng } 197