1 use crate::filesystem::devfs::{DevFS, DeviceINode}; 2 use crate::filesystem::vfs::{ 3 core::generate_inode_id, 4 file::{File, FileMode}, 5 make_rawdev, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, PollStatus, 6 }; 7 use crate::process::ProcessManager; 8 use crate::{arch::KVMArch, libs::spinlock::SpinLock, syscall::SystemError, time::TimeSpec}; 9 use crate::{filesystem, kdebug}; 10 // use crate::virt::kvm::{host_stack}; 11 use super::push_vm; 12 use crate::virt::kvm::vm_dev::LockedVmInode; 13 use alloc::{ 14 string::String, 15 sync::{Arc, Weak}, 16 vec::Vec, 17 }; 18 19 pub const KVM_API_VERSION: u32 = 12; 20 21 // use crate::virt::kvm::kvm_dev_ioctl_create_vm; 22 /* 23 * ioctls for /dev/kvm fds: 24 */ 25 pub const KVM_GET_API_VERSION: u32 = 0x00; 26 pub const KVM_CREATE_VM: u32 = 0x01; 27 pub const KVM_CHECK_EXTENSION: u32 = 0x03; 28 pub const KVM_GET_VCPU_MMAP_SIZE: u32 = 0x04; // Get size for mmap(vcpu_fd) in bytes 29 pub const KVM_TRACE_ENABLE: u32 = 0x05; 30 pub const KVM_TRACE_PAUSE: u32 = 0x06; 31 pub const KVM_TRACE_DISABLE: u32 = 0x07; 32 33 #[derive(Debug)] 34 pub struct KvmInode { 35 /// uuid 暂时不知道有什么用(x 36 // uuid: Uuid, 37 /// 指向自身的弱引用 38 self_ref: Weak<LockedKvmInode>, 39 /// 指向inode所在的文件系统对象的指针 40 fs: Weak<DevFS>, 41 /// INode 元数据 42 metadata: Metadata, 43 } 44 45 #[derive(Debug)] 46 pub struct LockedKvmInode(SpinLock<KvmInode>); 47 48 impl LockedKvmInode { 49 pub fn new() -> Arc<Self> { 50 let inode = KvmInode { 51 self_ref: Weak::default(), 52 fs: Weak::default(), 53 metadata: Metadata { 54 dev_id: 1, 55 inode_id: generate_inode_id(), 56 size: 0, 57 blk_size: 0, 58 blocks: 0, 59 atime: TimeSpec::default(), 60 mtime: TimeSpec::default(), 61 ctime: TimeSpec::default(), 62 file_type: FileType::KvmDevice, // 文件夹,block设备,char设备 63 mode: filesystem::vfs::syscall::ModeType::S_IALLUGO, 64 nlinks: 1, 65 uid: 0, 66 gid: 0, 67 raw_dev: make_rawdev(1, 4), // 这里用来作为device number 68 }, 69 }; 70 71 let result = Arc::new(LockedKvmInode(SpinLock::new(inode))); 72 result.0.lock().self_ref = Arc::downgrade(&result); 73 74 return result; 75 } 76 } 77 78 impl DeviceINode for LockedKvmInode { 79 fn set_fs(&self, fs: Weak<DevFS>) { 80 self.0.lock().fs = fs; 81 } 82 } 83 84 impl IndexNode for LockedKvmInode { 85 fn as_any_ref(&self) -> &dyn core::any::Any { 86 self 87 } 88 89 fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 90 kdebug!("file private data:{:?}", _data); 91 return Ok(()); 92 } 93 94 fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 95 return Ok(()); 96 } 97 98 fn metadata(&self) -> Result<Metadata, SystemError> { 99 return Ok(self.0.lock().metadata.clone()); 100 } 101 102 fn fs(&self) -> Arc<dyn FileSystem> { 103 return self.0.lock().fs.upgrade().unwrap(); 104 } 105 106 fn list(&self) -> Result<Vec<String>, SystemError> { 107 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 108 } 109 110 fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 111 let mut inode = self.0.lock(); 112 inode.metadata.atime = metadata.atime; 113 inode.metadata.mtime = metadata.mtime; 114 inode.metadata.ctime = metadata.ctime; 115 inode.metadata.mode = metadata.mode; 116 inode.metadata.uid = metadata.uid; 117 inode.metadata.gid = metadata.gid; 118 119 return Ok(()); 120 } 121 122 fn poll(&self) -> Result<PollStatus, SystemError> { 123 return Ok(PollStatus::READ | PollStatus::WRITE); 124 } 125 126 /// @brief io control接口 127 /// 128 /// @param cmd 命令 129 /// @param data 数据 130 /// 131 /// @return 成功:Ok() 132 /// 失败:Err(错误码) 133 fn ioctl(&self, cmd: u32, data: usize) -> Result<usize, SystemError> { 134 match cmd { 135 0xdeadbeef => { 136 kdebug!("kvm ioctl"); 137 Ok(0) 138 } 139 KVM_GET_API_VERSION => Ok(KVM_API_VERSION as usize), 140 KVM_CREATE_VM => { 141 kdebug!("kvm KVM_CREATE_VM"); 142 kvm_dev_ioctl_create_vm(data) 143 } 144 KVM_CHECK_EXTENSION 145 | KVM_GET_VCPU_MMAP_SIZE 146 | KVM_TRACE_ENABLE 147 | KVM_TRACE_PAUSE 148 | KVM_TRACE_DISABLE => Err(SystemError::EOPNOTSUPP_OR_ENOTSUP), 149 _ => KVMArch::kvm_arch_dev_ioctl(cmd, data), 150 } 151 } 152 /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 153 fn read_at( 154 &self, 155 _offset: usize, 156 _len: usize, 157 _buf: &mut [u8], 158 _data: &mut FilePrivateData, 159 ) -> Result<usize, SystemError> { 160 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 161 } 162 163 /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 164 fn write_at( 165 &self, 166 _offset: usize, 167 _len: usize, 168 _buf: &[u8], 169 _data: &mut FilePrivateData, 170 ) -> Result<usize, SystemError> { 171 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 172 } 173 } 174 175 #[no_mangle] 176 pub fn kvm_dev_ioctl_create_vm(_vmtype: usize) -> Result<usize, SystemError> { 177 push_vm(0).expect("need a valid vm!"); 178 179 // 创建vm文件,返回文件描述符 180 let vm_inode = LockedVmInode::new(); 181 let file: File = File::new(vm_inode, FileMode::O_RDWR)?; 182 let r = ProcessManager::current_pcb() 183 .fd_table() 184 .write() 185 .alloc_fd(file, None) 186 .map(|fd| fd as usize); 187 return r; 188 } 189