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