140314b30SXiaoye Zheng use crate::filesystem::devfs::DevFS; 240314b30SXiaoye Zheng use crate::filesystem::vfs::{ 340314b30SXiaoye Zheng core::generate_inode_id, 440314b30SXiaoye Zheng file::{File, FileMode}, 540609970SGnoCiYeH make_rawdev, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 640314b30SXiaoye Zheng }; 740314b30SXiaoye Zheng use crate::mm::VirtAddr; 840314b30SXiaoye Zheng use crate::process::ProcessManager; 940314b30SXiaoye Zheng use crate::syscall::user_access::copy_from_user; 1040314b30SXiaoye Zheng use crate::virt::kvm::host_mem::KvmUserspaceMemoryRegion; 1140314b30SXiaoye Zheng use crate::virt::kvm::update_vm; 1240314b30SXiaoye Zheng use crate::virt::kvm::vcpu_dev::LockedVcpuInode; 1340314b30SXiaoye Zheng use crate::virt::kvm::vm; 14*91e9d4abSLoGin use crate::{arch::KVMArch, libs::spinlock::SpinLock, time::TimeSpec}; 1540314b30SXiaoye Zheng use crate::{filesystem, kdebug}; 1640314b30SXiaoye Zheng use alloc::{ 1740314b30SXiaoye Zheng string::String, 1840314b30SXiaoye Zheng sync::{Arc, Weak}, 1940314b30SXiaoye Zheng vec::Vec, 2040314b30SXiaoye Zheng }; 21*91e9d4abSLoGin use system_error::SystemError; 2240314b30SXiaoye Zheng 2340314b30SXiaoye Zheng // pub const KVM_API_VERSION:u32 = 12; 2440314b30SXiaoye Zheng // pub const GUEST_STACK_SIZE:usize = 1024; 2540314b30SXiaoye Zheng // pub const HOST_STACK_SIZE:usize = 0x1000 * 6; 2640314b30SXiaoye Zheng 2740314b30SXiaoye Zheng /* 2840314b30SXiaoye Zheng * ioctls for /dev/vm fds: 2940314b30SXiaoye Zheng */ 3040314b30SXiaoye Zheng pub const KVM_CREATE_VCPU: u32 = 0x00; 3140314b30SXiaoye Zheng pub const KVM_SET_USER_MEMORY_REGION: u32 = 0x01; 3240314b30SXiaoye Zheng pub const KVM_GET_DIRTY_LOG: u32 = 0x02; 3340314b30SXiaoye Zheng pub const KVM_IRQFD: u32 = 0x03; 3440314b30SXiaoye Zheng pub const KVM_IOEVENTFD: u32 = 0x04; 3540314b30SXiaoye Zheng pub const KVM_IRQ_LINE_STATUS: u32 = 0x05; 3640314b30SXiaoye Zheng 3740314b30SXiaoye Zheng // #[derive(Debug)] 3840314b30SXiaoye Zheng // pub struct InodeInfo { 3940314b30SXiaoye Zheng // kvm: Arc<Hypervisor>, 4040314b30SXiaoye Zheng // } 4140314b30SXiaoye Zheng 4240314b30SXiaoye Zheng #[derive(Debug)] 4340314b30SXiaoye Zheng pub struct VmInode { 4440314b30SXiaoye Zheng /// uuid 暂时不知道有什么用(x 4540314b30SXiaoye Zheng // uuid: Uuid, 4640314b30SXiaoye Zheng /// 指向自身的弱引用 4740314b30SXiaoye Zheng self_ref: Weak<LockedVmInode>, 4840314b30SXiaoye Zheng /// 指向inode所在的文件系统对象的指针 4940314b30SXiaoye Zheng fs: Weak<DevFS>, 5040314b30SXiaoye Zheng /// INode 元数据 5140314b30SXiaoye Zheng metadata: Metadata, 5240314b30SXiaoye Zheng // fdata: InodeInfo, 5340314b30SXiaoye Zheng } 5440314b30SXiaoye Zheng 5540314b30SXiaoye Zheng #[derive(Debug)] 5640314b30SXiaoye Zheng pub struct LockedVmInode(SpinLock<VmInode>); 5740314b30SXiaoye Zheng 5840314b30SXiaoye Zheng impl LockedVmInode { 5940314b30SXiaoye Zheng pub fn new() -> Arc<Self> { 6040314b30SXiaoye Zheng let inode = VmInode { 6140314b30SXiaoye Zheng self_ref: Weak::default(), 6240314b30SXiaoye Zheng fs: Weak::default(), 6340314b30SXiaoye Zheng metadata: Metadata { 6440314b30SXiaoye Zheng dev_id: 1, 6540314b30SXiaoye Zheng inode_id: generate_inode_id(), 6640314b30SXiaoye Zheng size: 0, 6740314b30SXiaoye Zheng blk_size: 0, 6840314b30SXiaoye Zheng blocks: 0, 6940314b30SXiaoye Zheng atime: TimeSpec::default(), 7040314b30SXiaoye Zheng mtime: TimeSpec::default(), 7140314b30SXiaoye Zheng ctime: TimeSpec::default(), 7240314b30SXiaoye Zheng file_type: FileType::KvmDevice, // 文件夹,block设备,char设备 7340314b30SXiaoye Zheng mode: filesystem::vfs::syscall::ModeType::S_IALLUGO, 7440314b30SXiaoye Zheng nlinks: 1, 7540314b30SXiaoye Zheng uid: 0, 7640314b30SXiaoye Zheng gid: 0, 7740314b30SXiaoye Zheng raw_dev: make_rawdev(1, 4), // 这里用来作为device number 7840314b30SXiaoye Zheng }, 7940314b30SXiaoye Zheng // fdata: InodeInfo { 8040314b30SXiaoye Zheng // kvm: kvm, 8140314b30SXiaoye Zheng // }, 8240314b30SXiaoye Zheng }; 8340314b30SXiaoye Zheng 8440314b30SXiaoye Zheng let result = Arc::new(LockedVmInode(SpinLock::new(inode))); 8540314b30SXiaoye Zheng result.0.lock().self_ref = Arc::downgrade(&result); 8640314b30SXiaoye Zheng 8740314b30SXiaoye Zheng return result; 8840314b30SXiaoye Zheng } 8940314b30SXiaoye Zheng } 9040314b30SXiaoye Zheng 9140314b30SXiaoye Zheng impl IndexNode for LockedVmInode { 9240314b30SXiaoye Zheng fn as_any_ref(&self) -> &dyn core::any::Any { 9340314b30SXiaoye Zheng self 9440314b30SXiaoye Zheng } 9540314b30SXiaoye Zheng 9640314b30SXiaoye Zheng fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 9740314b30SXiaoye Zheng kdebug!("file private data:{:?}", _data); 9840314b30SXiaoye Zheng return Ok(()); 9940314b30SXiaoye Zheng } 10040314b30SXiaoye Zheng 10140314b30SXiaoye Zheng fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 10240314b30SXiaoye Zheng return Ok(()); 10340314b30SXiaoye Zheng } 10440314b30SXiaoye Zheng 10540314b30SXiaoye Zheng fn metadata(&self) -> Result<Metadata, SystemError> { 10640314b30SXiaoye Zheng return Ok(self.0.lock().metadata.clone()); 10740314b30SXiaoye Zheng } 10840314b30SXiaoye Zheng 10940314b30SXiaoye Zheng fn fs(&self) -> Arc<dyn FileSystem> { 11040314b30SXiaoye Zheng return self.0.lock().fs.upgrade().unwrap(); 11140314b30SXiaoye Zheng } 11240314b30SXiaoye Zheng 11340314b30SXiaoye Zheng fn list(&self) -> Result<Vec<String>, SystemError> { 11440314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 11540314b30SXiaoye Zheng } 11640314b30SXiaoye Zheng 11740314b30SXiaoye Zheng fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 11840314b30SXiaoye Zheng let mut inode = self.0.lock(); 11940314b30SXiaoye Zheng inode.metadata.atime = metadata.atime; 12040314b30SXiaoye Zheng inode.metadata.mtime = metadata.mtime; 12140314b30SXiaoye Zheng inode.metadata.ctime = metadata.ctime; 12240314b30SXiaoye Zheng inode.metadata.mode = metadata.mode; 12340314b30SXiaoye Zheng inode.metadata.uid = metadata.uid; 12440314b30SXiaoye Zheng inode.metadata.gid = metadata.gid; 12540314b30SXiaoye Zheng 12640314b30SXiaoye Zheng return Ok(()); 12740314b30SXiaoye Zheng } 12840314b30SXiaoye Zheng 12940314b30SXiaoye Zheng /// @brief io control接口 13040314b30SXiaoye Zheng /// 13140314b30SXiaoye Zheng /// @param cmd 命令 13240314b30SXiaoye Zheng /// @param data 数据 13340314b30SXiaoye Zheng /// 13440314b30SXiaoye Zheng /// @return 成功:Ok() 13540314b30SXiaoye Zheng /// 失败:Err(错误码) 13640314b30SXiaoye Zheng fn ioctl(&self, cmd: u32, data: usize) -> Result<usize, SystemError> { 13740314b30SXiaoye Zheng match cmd { 13840314b30SXiaoye Zheng 0xdeadbeef => { 13940314b30SXiaoye Zheng kdebug!("kvm_vm ioctl"); 14040314b30SXiaoye Zheng Ok(0) 14140314b30SXiaoye Zheng } 14240314b30SXiaoye Zheng KVM_CREATE_VCPU => { 14340314b30SXiaoye Zheng kdebug!("kvm_vcpu ioctl KVM_CREATE_VCPU"); 14440314b30SXiaoye Zheng kvm_vm_ioctl_create_vcpu(data as u32) 14540314b30SXiaoye Zheng } 14640314b30SXiaoye Zheng KVM_SET_USER_MEMORY_REGION => { 14740314b30SXiaoye Zheng kdebug!("kvm_vcpu ioctl KVM_SET_USER_MEMORY_REGION data={:x}", data); 14840314b30SXiaoye Zheng let mut kvm_userspace_mem = KvmUserspaceMemoryRegion::default(); // = unsafe { (data as *const KvmUserspaceMemoryRegion).as_ref().unwrap() }; 14940314b30SXiaoye Zheng unsafe { 15040314b30SXiaoye Zheng copy_from_user( 15140314b30SXiaoye Zheng core::slice::from_raw_parts_mut( 15240314b30SXiaoye Zheng (&mut kvm_userspace_mem as *mut _) as *mut u8, 15340314b30SXiaoye Zheng core::mem::size_of::<KvmUserspaceMemoryRegion>(), 15440314b30SXiaoye Zheng ), 15540314b30SXiaoye Zheng VirtAddr::new(data), 15640314b30SXiaoye Zheng )?; 15740314b30SXiaoye Zheng } 15840314b30SXiaoye Zheng kdebug!( 15940314b30SXiaoye Zheng "slot={}, flag={}, memory_size={:x}, guest_phys_addr={}, userspace_addr={}", 16040314b30SXiaoye Zheng kvm_userspace_mem.slot, 16140314b30SXiaoye Zheng kvm_userspace_mem.flags, 16240314b30SXiaoye Zheng kvm_userspace_mem.memory_size, 16340314b30SXiaoye Zheng kvm_userspace_mem.guest_phys_addr, // starting at physical address guest_phys_addr (from the guest’s perspective) 16440314b30SXiaoye Zheng kvm_userspace_mem.userspace_addr // using memory at linear address userspace_addr (from the host’s perspective) 16540314b30SXiaoye Zheng ); 16640314b30SXiaoye Zheng 16740314b30SXiaoye Zheng let mut current_vm = vm(0).unwrap(); 16840314b30SXiaoye Zheng current_vm.set_user_memory_region(&kvm_userspace_mem)?; 16940314b30SXiaoye Zheng update_vm(0, current_vm); 17040314b30SXiaoye Zheng Ok(0) 17140314b30SXiaoye Zheng } 17240314b30SXiaoye Zheng KVM_GET_DIRTY_LOG | KVM_IRQFD | KVM_IOEVENTFD | KVM_IRQ_LINE_STATUS => { 17340314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 17440314b30SXiaoye Zheng } 17540314b30SXiaoye Zheng _ => { 17640314b30SXiaoye Zheng kdebug!("kvm_vm ioctl"); 17740314b30SXiaoye Zheng Ok(usize::MAX) 17840314b30SXiaoye Zheng } 17940314b30SXiaoye Zheng } 18040314b30SXiaoye Zheng } 18140314b30SXiaoye Zheng /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 18240314b30SXiaoye Zheng fn read_at( 18340314b30SXiaoye Zheng &self, 18440314b30SXiaoye Zheng _offset: usize, 18540314b30SXiaoye Zheng _len: usize, 18640314b30SXiaoye Zheng _buf: &mut [u8], 18740314b30SXiaoye Zheng _data: &mut FilePrivateData, 18840314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 18940314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 19040314b30SXiaoye Zheng } 19140314b30SXiaoye Zheng 19240314b30SXiaoye Zheng /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 19340314b30SXiaoye Zheng fn write_at( 19440314b30SXiaoye Zheng &self, 19540314b30SXiaoye Zheng _offset: usize, 19640314b30SXiaoye Zheng _len: usize, 19740314b30SXiaoye Zheng _buf: &[u8], 19840314b30SXiaoye Zheng _data: &mut FilePrivateData, 19940314b30SXiaoye Zheng ) -> Result<usize, SystemError> { 20040314b30SXiaoye Zheng Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 20140314b30SXiaoye Zheng } 20240314b30SXiaoye Zheng } 20340314b30SXiaoye Zheng 20440314b30SXiaoye Zheng fn kvm_vm_ioctl_create_vcpu(id: u32) -> Result<usize, SystemError> { 20540314b30SXiaoye Zheng let vcpu = KVMArch::kvm_arch_vcpu_create(id).unwrap(); 20640314b30SXiaoye Zheng KVMArch::kvm_arch_vcpu_setup(vcpu.as_ref())?; 20740314b30SXiaoye Zheng 20840314b30SXiaoye Zheng let mut current_vm = vm(0).unwrap(); 20940314b30SXiaoye Zheng current_vm.vcpu.push(vcpu); 21040314b30SXiaoye Zheng current_vm.nr_vcpus += 1; 21140314b30SXiaoye Zheng update_vm(0, current_vm); 21240314b30SXiaoye Zheng 21340314b30SXiaoye Zheng let vcpu_inode = LockedVcpuInode::new(); 21440314b30SXiaoye Zheng let file: File = File::new(vcpu_inode, FileMode::O_RDWR)?; 21540314b30SXiaoye Zheng let r = ProcessManager::current_pcb() 21640314b30SXiaoye Zheng .fd_table() 21740314b30SXiaoye Zheng .write() 21840314b30SXiaoye Zheng .alloc_fd(file, None) 21940314b30SXiaoye Zheng .map(|fd| fd as usize); 22040314b30SXiaoye Zheng return r; 22140314b30SXiaoye Zheng } 222