140314b30SXiaoye Zheng use crate::arch::kvm::vmx::vcpu::VcpuContextFrame; 240314b30SXiaoye Zheng use crate::arch::KVMArch; 302343d0bSLoGin use crate::driver::base::device::device_number::DeviceNumber; 440314b30SXiaoye Zheng use crate::filesystem::devfs::DevFS; 540314b30SXiaoye Zheng use crate::filesystem::vfs::{ 602343d0bSLoGin core::generate_inode_id, file::FileMode, FilePrivateData, FileSystem, FileType, IndexNode, 702343d0bSLoGin Metadata, 840314b30SXiaoye Zheng }; 9dfe53cf0SGnoCiYeH use crate::libs::spinlock::SpinLockGuard; 1040314b30SXiaoye Zheng use crate::mm::VirtAddr; 1140314b30SXiaoye Zheng use crate::syscall::user_access::copy_from_user; 1240314b30SXiaoye Zheng use crate::virt::kvm::vcpu::Vcpu; 1340314b30SXiaoye Zheng use crate::virt::kvm::vm; 1440314b30SXiaoye Zheng use crate::{filesystem, kdebug}; 15*6fc066acSJomo use crate::{libs::spinlock::SpinLock, time::PosixTimeSpec}; 1640314b30SXiaoye Zheng use alloc::{ 1740314b30SXiaoye Zheng string::String, 1840314b30SXiaoye Zheng sync::{Arc, Weak}, 1940314b30SXiaoye Zheng vec::Vec, 2040314b30SXiaoye Zheng }; 2191e9d4abSLoGin use system_error::SystemError; 2240314b30SXiaoye Zheng 2340314b30SXiaoye Zheng // pub const KVM_API_VERSION:u32 = 12; 2440314b30SXiaoye Zheng pub const KVM_RUN: u32 = 0x00; 2540314b30SXiaoye Zheng // pub const KVM_GET_REGS: u32 = 0x01; 2640314b30SXiaoye Zheng pub const KVM_SET_REGS: u32 = 0x02; 2740314b30SXiaoye Zheng 2840314b30SXiaoye Zheng // pub const GUEST_STACK_SIZE:usize = 1024; 2940314b30SXiaoye Zheng // pub const HOST_STACK_SIZE:usize = 0x1000 * 6; 3040314b30SXiaoye Zheng 3140314b30SXiaoye Zheng /* 3240314b30SXiaoye Zheng * ioctls for /dev/vm fds: 3340314b30SXiaoye Zheng */ 3440314b30SXiaoye Zheng // pub const KVM_CREATE_VCPU: u32 = 0x00; 3540314b30SXiaoye Zheng // pub const KVM_SET_USER_MEMORY_REGION: u32 = 0x01; 3640314b30SXiaoye Zheng // pub const KVM_GET_DIRTY_LOG: u32 = 0x02; 3740314b30SXiaoye Zheng // pub const KVM_IRQFD: u32 = 0x03; 3840314b30SXiaoye Zheng // pub const KVM_IOEVENTFD: u32 = 0x04; 3940314b30SXiaoye Zheng // pub const KVM_IRQ_LINE_STATUS: u32 = 0x05; 4040314b30SXiaoye Zheng 4140314b30SXiaoye Zheng // #[derive(Debug)] 4240314b30SXiaoye Zheng // pub struct InodeInfo { 4340314b30SXiaoye Zheng // kvm: Arc<Hypervisor>, 4440314b30SXiaoye Zheng // } 4540314b30SXiaoye Zheng 4640314b30SXiaoye Zheng #[derive(Debug)] 4740314b30SXiaoye Zheng pub struct VcpuInode { 4840314b30SXiaoye Zheng /// uuid 暂时不知道有什么用(x 4940314b30SXiaoye Zheng // uuid: Uuid, 5040314b30SXiaoye Zheng /// 指向自身的弱引用 5140314b30SXiaoye Zheng self_ref: Weak<LockedVcpuInode>, 5240314b30SXiaoye Zheng /// 指向inode所在的文件系统对象的指针 5340314b30SXiaoye Zheng fs: Weak<DevFS>, 5440314b30SXiaoye Zheng /// INode 元数据 5540314b30SXiaoye Zheng metadata: Metadata, 5640314b30SXiaoye Zheng // fdata: InodeInfo, 5740314b30SXiaoye Zheng } 5840314b30SXiaoye Zheng 5940314b30SXiaoye Zheng #[derive(Debug)] 6040314b30SXiaoye Zheng pub struct LockedVcpuInode(SpinLock<VcpuInode>); 6140314b30SXiaoye Zheng 6240314b30SXiaoye Zheng impl LockedVcpuInode { 6340314b30SXiaoye Zheng pub fn new() -> Arc<Self> { 6440314b30SXiaoye Zheng let inode = VcpuInode { 6540314b30SXiaoye Zheng self_ref: Weak::default(), 6640314b30SXiaoye Zheng fs: Weak::default(), 6740314b30SXiaoye Zheng metadata: Metadata { 6840314b30SXiaoye Zheng dev_id: 1, 6940314b30SXiaoye Zheng inode_id: generate_inode_id(), 7040314b30SXiaoye Zheng size: 0, 7140314b30SXiaoye Zheng blk_size: 0, 7240314b30SXiaoye Zheng blocks: 0, 73*6fc066acSJomo atime: PosixTimeSpec::default(), 74*6fc066acSJomo mtime: PosixTimeSpec::default(), 75*6fc066acSJomo ctime: PosixTimeSpec::default(), 7640314b30SXiaoye Zheng file_type: FileType::KvmDevice, // 文件夹,block设备,char设备 7740314b30SXiaoye Zheng mode: filesystem::vfs::syscall::ModeType::S_IALLUGO, 7840314b30SXiaoye Zheng nlinks: 1, 7940314b30SXiaoye Zheng uid: 0, 8040314b30SXiaoye Zheng gid: 0, 8102343d0bSLoGin raw_dev: DeviceNumber::default(), // 这里用来作为device number 8240314b30SXiaoye Zheng }, 8340314b30SXiaoye Zheng // fdata: InodeInfo { 8440314b30SXiaoye Zheng // kvm: kvm, 8540314b30SXiaoye Zheng // }, 8640314b30SXiaoye Zheng }; 8740314b30SXiaoye Zheng 8840314b30SXiaoye Zheng let result = Arc::new(LockedVcpuInode(SpinLock::new(inode))); 8940314b30SXiaoye Zheng result.0.lock().self_ref = Arc::downgrade(&result); 9040314b30SXiaoye Zheng 9140314b30SXiaoye Zheng return result; 9240314b30SXiaoye Zheng } 9340314b30SXiaoye Zheng } 9440314b30SXiaoye Zheng 9540314b30SXiaoye Zheng impl IndexNode for LockedVcpuInode { 9640314b30SXiaoye Zheng fn as_any_ref(&self) -> &dyn core::any::Any { 9740314b30SXiaoye Zheng self 9840314b30SXiaoye Zheng } 9940314b30SXiaoye Zheng 100dfe53cf0SGnoCiYeH fn open( 101dfe53cf0SGnoCiYeH &self, 102dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 103dfe53cf0SGnoCiYeH _mode: &FileMode, 104dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 10540314b30SXiaoye Zheng kdebug!("file private data:{:?}", _data); 10640314b30SXiaoye Zheng return Ok(()); 10740314b30SXiaoye Zheng } 10840314b30SXiaoye Zheng 109dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 11040314b30SXiaoye Zheng return Ok(()); 11140314b30SXiaoye Zheng } 11240314b30SXiaoye Zheng 11340314b30SXiaoye Zheng fn metadata(&self) -> Result<Metadata, SystemError> { 11440314b30SXiaoye Zheng return Ok(self.0.lock().metadata.clone()); 11540314b30SXiaoye Zheng } 11640314b30SXiaoye Zheng 11740314b30SXiaoye Zheng fn fs(&self) -> Arc<dyn FileSystem> { 11840314b30SXiaoye Zheng return self.0.lock().fs.upgrade().unwrap(); 11940314b30SXiaoye Zheng } 12040314b30SXiaoye Zheng 12140314b30SXiaoye Zheng fn list(&self) -> Result<Vec<String>, SystemError> { 12240314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 12340314b30SXiaoye Zheng } 12440314b30SXiaoye Zheng 12540314b30SXiaoye Zheng fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 12640314b30SXiaoye Zheng let mut inode = self.0.lock(); 12740314b30SXiaoye Zheng inode.metadata.atime = metadata.atime; 12840314b30SXiaoye Zheng inode.metadata.mtime = metadata.mtime; 12940314b30SXiaoye Zheng inode.metadata.ctime = metadata.ctime; 13040314b30SXiaoye Zheng inode.metadata.mode = metadata.mode; 13140314b30SXiaoye Zheng inode.metadata.uid = metadata.uid; 13240314b30SXiaoye Zheng inode.metadata.gid = metadata.gid; 13340314b30SXiaoye Zheng 13440314b30SXiaoye Zheng return Ok(()); 13540314b30SXiaoye Zheng } 13640314b30SXiaoye Zheng 13740314b30SXiaoye Zheng /// @brief io control接口 13840314b30SXiaoye Zheng /// 13940314b30SXiaoye Zheng /// @param cmd 命令 14040314b30SXiaoye Zheng /// @param data 数据 14140314b30SXiaoye Zheng /// 14240314b30SXiaoye Zheng /// @return 成功:Ok() 14340314b30SXiaoye Zheng /// 失败:Err(错误码) 14452da9a59SGnoCiYeH fn ioctl( 14552da9a59SGnoCiYeH &self, 14652da9a59SGnoCiYeH cmd: u32, 14752da9a59SGnoCiYeH data: usize, 14852da9a59SGnoCiYeH _private_data: &FilePrivateData, 14952da9a59SGnoCiYeH ) -> Result<usize, SystemError> { 15040314b30SXiaoye Zheng match cmd { 15140314b30SXiaoye Zheng 0xdeadbeef => { 15240314b30SXiaoye Zheng kdebug!("kvm_cpu ioctl"); 15340314b30SXiaoye Zheng Ok(0) 15440314b30SXiaoye Zheng } 15540314b30SXiaoye Zheng KVM_RUN => { 15640314b30SXiaoye Zheng kdebug!("kvm_cpu ioctl"); 15740314b30SXiaoye Zheng // let guest_stack = vec![0xCC; GUEST_STACK_SIZE]; 15840314b30SXiaoye Zheng // let host_stack = vec![0xCC; HOST_STACK_SIZE]; 15940314b30SXiaoye Zheng // let guest_rsp = guest_stack.as_ptr() as u64 + GUEST_STACK_SIZE as u64; 16040314b30SXiaoye Zheng // let host_rsp = (host_stack.as_ptr() as u64) + HOST_STACK_SIZE as u64; 16140314b30SXiaoye Zheng // let hypervisor = Hypervisor::new(1, host_rsp, 0).expect("Cannot create hypervisor"); 16240314b30SXiaoye Zheng // let vcpu = VmxVcpu::new(1, Arc::new(Mutex::new(hypervisor)), host_rsp, guest_rsp, guest_code as *const () as u64).expect("Cannot create VcpuData"); 16340314b30SXiaoye Zheng // vcpu.virtualize_cpu().expect("Cannot virtualize cpu"); 16440314b30SXiaoye Zheng let vcpu = vm(0).unwrap().vcpu[0].clone(); 16540314b30SXiaoye Zheng vcpu.lock().virtualize_cpu()?; 16640314b30SXiaoye Zheng KVMArch::kvm_arch_vcpu_ioctl_run(vcpu.as_ref())?; 16740314b30SXiaoye Zheng Ok(0) 16840314b30SXiaoye Zheng } 16940314b30SXiaoye Zheng KVM_SET_REGS => { 17040314b30SXiaoye Zheng let mut kvm_regs = VcpuContextFrame::default(); 17140314b30SXiaoye Zheng unsafe { 17240314b30SXiaoye Zheng copy_from_user( 17340314b30SXiaoye Zheng core::slice::from_raw_parts_mut( 17440314b30SXiaoye Zheng (&mut kvm_regs as *mut _) as *mut u8, 17540314b30SXiaoye Zheng core::mem::size_of::<VcpuContextFrame>(), 17640314b30SXiaoye Zheng ), 17740314b30SXiaoye Zheng VirtAddr::new(data), 17840314b30SXiaoye Zheng )?; 17940314b30SXiaoye Zheng } 18040314b30SXiaoye Zheng kdebug!( 18140314b30SXiaoye Zheng "rip={:x}, rflags={:x}, rsp={:x}, rax={:x}", 18240314b30SXiaoye Zheng kvm_regs.rip, 18340314b30SXiaoye Zheng kvm_regs.rflags, 18440314b30SXiaoye Zheng kvm_regs.regs[6], 18540314b30SXiaoye Zheng kvm_regs.regs[0], 18640314b30SXiaoye Zheng ); 18740314b30SXiaoye Zheng 18840314b30SXiaoye Zheng let vcpu = vm(0).unwrap().vcpu[0].clone(); 18940314b30SXiaoye Zheng vcpu.lock().set_regs(kvm_regs)?; 19040314b30SXiaoye Zheng 19140314b30SXiaoye Zheng Ok(0) 19240314b30SXiaoye Zheng } 19340314b30SXiaoye Zheng _ => { 19440314b30SXiaoye Zheng kdebug!("kvm_cpu ioctl"); 19540314b30SXiaoye Zheng Ok(usize::MAX) 19640314b30SXiaoye Zheng } 19740314b30SXiaoye Zheng } 19840314b30SXiaoye Zheng } 19940314b30SXiaoye Zheng /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 20040314b30SXiaoye Zheng fn read_at( 20140314b30SXiaoye Zheng &self, 20240314b30SXiaoye Zheng _offset: usize, 20340314b30SXiaoye Zheng _len: usize, 20440314b30SXiaoye Zheng _buf: &mut [u8], 205dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 20640314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 20740314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 20840314b30SXiaoye Zheng } 20940314b30SXiaoye Zheng 21040314b30SXiaoye Zheng /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 21140314b30SXiaoye Zheng fn write_at( 21240314b30SXiaoye Zheng &self, 21340314b30SXiaoye Zheng _offset: usize, 21440314b30SXiaoye Zheng _len: usize, 21540314b30SXiaoye Zheng _buf: &[u8], 216dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 21740314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 21840314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 21940314b30SXiaoye Zheng } 22040314b30SXiaoye Zheng } 221