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