1 use crate::driver::base::device::device_number::DeviceNumber; 2 use crate::filesystem::devfs::DevFS; 3 use crate::filesystem::vfs::{ 4 core::generate_inode_id, 5 file::{File, FileMode}, 6 FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 7 }; 8 use crate::mm::VirtAddr; 9 use crate::process::ProcessManager; 10 use crate::syscall::user_access::copy_from_user; 11 use crate::virt::kvm::host_mem::KvmUserspaceMemoryRegion; 12 use crate::virt::kvm::update_vm; 13 use crate::virt::kvm::vcpu_dev::LockedVcpuInode; 14 use crate::virt::kvm::vm; 15 use crate::{arch::KVMArch, libs::spinlock::SpinLock, time::TimeSpec}; 16 use crate::{filesystem, kdebug}; 17 use alloc::{ 18 string::String, 19 sync::{Arc, Weak}, 20 vec::Vec, 21 }; 22 use system_error::SystemError; 23 24 // pub const KVM_API_VERSION:u32 = 12; 25 // pub const GUEST_STACK_SIZE:usize = 1024; 26 // pub const HOST_STACK_SIZE:usize = 0x1000 * 6; 27 28 /* 29 * ioctls for /dev/vm fds: 30 */ 31 pub const KVM_CREATE_VCPU: u32 = 0x00; 32 pub const KVM_SET_USER_MEMORY_REGION: u32 = 0x01; 33 pub const KVM_GET_DIRTY_LOG: u32 = 0x02; 34 pub const KVM_IRQFD: u32 = 0x03; 35 pub const KVM_IOEVENTFD: u32 = 0x04; 36 pub const KVM_IRQ_LINE_STATUS: u32 = 0x05; 37 38 // #[derive(Debug)] 39 // pub struct InodeInfo { 40 // kvm: Arc<Hypervisor>, 41 // } 42 43 #[derive(Debug)] 44 pub struct VmInode { 45 /// uuid 暂时不知道有什么用(x 46 // uuid: Uuid, 47 /// 指向自身的弱引用 48 self_ref: Weak<LockedVmInode>, 49 /// 指向inode所在的文件系统对象的指针 50 fs: Weak<DevFS>, 51 /// INode 元数据 52 metadata: Metadata, 53 // fdata: InodeInfo, 54 } 55 56 #[derive(Debug)] 57 pub struct LockedVmInode(SpinLock<VmInode>); 58 59 impl LockedVmInode { 60 pub fn new() -> Arc<Self> { 61 let inode = VmInode { 62 self_ref: Weak::default(), 63 fs: Weak::default(), 64 metadata: Metadata { 65 dev_id: 1, 66 inode_id: generate_inode_id(), 67 size: 0, 68 blk_size: 0, 69 blocks: 0, 70 atime: TimeSpec::default(), 71 mtime: TimeSpec::default(), 72 ctime: TimeSpec::default(), 73 file_type: FileType::KvmDevice, // 文件夹,block设备,char设备 74 mode: filesystem::vfs::syscall::ModeType::S_IALLUGO, 75 nlinks: 1, 76 uid: 0, 77 gid: 0, 78 raw_dev: DeviceNumber::default(), // 这里用来作为device number 79 }, 80 // fdata: InodeInfo { 81 // kvm: kvm, 82 // }, 83 }; 84 85 let result = Arc::new(LockedVmInode(SpinLock::new(inode))); 86 result.0.lock().self_ref = Arc::downgrade(&result); 87 88 return result; 89 } 90 } 91 92 impl IndexNode for LockedVmInode { 93 fn as_any_ref(&self) -> &dyn core::any::Any { 94 self 95 } 96 97 fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 98 kdebug!("file private data:{:?}", _data); 99 return Ok(()); 100 } 101 102 fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 103 return Ok(()); 104 } 105 106 fn metadata(&self) -> Result<Metadata, SystemError> { 107 return Ok(self.0.lock().metadata.clone()); 108 } 109 110 fn fs(&self) -> Arc<dyn FileSystem> { 111 return self.0.lock().fs.upgrade().unwrap(); 112 } 113 114 fn list(&self) -> Result<Vec<String>, SystemError> { 115 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 116 } 117 118 fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 119 let mut inode = self.0.lock(); 120 inode.metadata.atime = metadata.atime; 121 inode.metadata.mtime = metadata.mtime; 122 inode.metadata.ctime = metadata.ctime; 123 inode.metadata.mode = metadata.mode; 124 inode.metadata.uid = metadata.uid; 125 inode.metadata.gid = metadata.gid; 126 127 return Ok(()); 128 } 129 130 /// @brief io control接口 131 /// 132 /// @param cmd 命令 133 /// @param data 数据 134 /// 135 /// @return 成功:Ok() 136 /// 失败:Err(错误码) 137 fn ioctl(&self, cmd: u32, data: usize) -> Result<usize, SystemError> { 138 match cmd { 139 0xdeadbeef => { 140 kdebug!("kvm_vm ioctl"); 141 Ok(0) 142 } 143 KVM_CREATE_VCPU => { 144 kdebug!("kvm_vcpu ioctl KVM_CREATE_VCPU"); 145 kvm_vm_ioctl_create_vcpu(data as u32) 146 } 147 KVM_SET_USER_MEMORY_REGION => { 148 kdebug!("kvm_vcpu ioctl KVM_SET_USER_MEMORY_REGION data={:x}", data); 149 let mut kvm_userspace_mem = KvmUserspaceMemoryRegion::default(); // = unsafe { (data as *const KvmUserspaceMemoryRegion).as_ref().unwrap() }; 150 unsafe { 151 copy_from_user( 152 core::slice::from_raw_parts_mut( 153 (&mut kvm_userspace_mem as *mut _) as *mut u8, 154 core::mem::size_of::<KvmUserspaceMemoryRegion>(), 155 ), 156 VirtAddr::new(data), 157 )?; 158 } 159 kdebug!( 160 "slot={}, flag={}, memory_size={:x}, guest_phys_addr={}, userspace_addr={}", 161 kvm_userspace_mem.slot, 162 kvm_userspace_mem.flags, 163 kvm_userspace_mem.memory_size, 164 kvm_userspace_mem.guest_phys_addr, // starting at physical address guest_phys_addr (from the guest’s perspective) 165 kvm_userspace_mem.userspace_addr // using memory at linear address userspace_addr (from the host’s perspective) 166 ); 167 168 let mut current_vm = vm(0).unwrap(); 169 current_vm.set_user_memory_region(&kvm_userspace_mem)?; 170 update_vm(0, current_vm); 171 Ok(0) 172 } 173 KVM_GET_DIRTY_LOG | KVM_IRQFD | KVM_IOEVENTFD | KVM_IRQ_LINE_STATUS => { 174 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 175 } 176 _ => { 177 kdebug!("kvm_vm ioctl"); 178 Ok(usize::MAX) 179 } 180 } 181 } 182 /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 183 fn read_at( 184 &self, 185 _offset: usize, 186 _len: usize, 187 _buf: &mut [u8], 188 _data: &mut FilePrivateData, 189 ) -> Result<usize, SystemError> { 190 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 191 } 192 193 /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 194 fn write_at( 195 &self, 196 _offset: usize, 197 _len: usize, 198 _buf: &[u8], 199 _data: &mut FilePrivateData, 200 ) -> Result<usize, SystemError> { 201 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 202 } 203 } 204 205 fn kvm_vm_ioctl_create_vcpu(id: u32) -> Result<usize, SystemError> { 206 let vcpu = KVMArch::kvm_arch_vcpu_create(id).unwrap(); 207 KVMArch::kvm_arch_vcpu_setup(vcpu.as_ref())?; 208 209 let mut current_vm = vm(0).unwrap(); 210 current_vm.vcpu.push(vcpu); 211 current_vm.nr_vcpus += 1; 212 update_vm(0, current_vm); 213 214 let vcpu_inode = LockedVcpuInode::new(); 215 let file: File = File::new(vcpu_inode, FileMode::O_RDWR)?; 216 let r = ProcessManager::current_pcb() 217 .fd_table() 218 .write() 219 .alloc_fd(file, None) 220 .map(|fd| fd as usize); 221 return r; 222 } 223