102343d0bSLoGin use crate::driver::base::device::device_number::DeviceNumber;
2*2eab6dd7S曾俊 use crate::filesystem;
340314b30SXiaoye Zheng use crate::filesystem::devfs::{DevFS, DeviceINode};
440314b30SXiaoye Zheng use crate::filesystem::vfs::{
540314b30SXiaoye Zheng core::generate_inode_id,
640314b30SXiaoye Zheng file::{File, FileMode},
702343d0bSLoGin FilePrivateData, FileSystem, FileType, IndexNode, Metadata,
840314b30SXiaoye Zheng };
9dfe53cf0SGnoCiYeH use crate::libs::spinlock::SpinLockGuard;
1040314b30SXiaoye Zheng use crate::process::ProcessManager;
116fc066acSJomo use crate::{arch::KVMArch, libs::spinlock::SpinLock, time::PosixTimeSpec};
1240314b30SXiaoye Zheng // use crate::virt::kvm::{host_stack};
1340314b30SXiaoye Zheng use super::push_vm;
1440314b30SXiaoye Zheng use crate::virt::kvm::vm_dev::LockedVmInode;
1540314b30SXiaoye Zheng use alloc::{
1640314b30SXiaoye Zheng string::String,
1740314b30SXiaoye Zheng sync::{Arc, Weak},
1840314b30SXiaoye Zheng vec::Vec,
1940314b30SXiaoye Zheng };
20*2eab6dd7S曾俊 use log::debug;
2191e9d4abSLoGin use system_error::SystemError;
2240314b30SXiaoye Zheng
2340314b30SXiaoye Zheng pub const KVM_API_VERSION: u32 = 12;
2440314b30SXiaoye Zheng
2540314b30SXiaoye Zheng // use crate::virt::kvm::kvm_dev_ioctl_create_vm;
2640314b30SXiaoye Zheng /*
2740314b30SXiaoye Zheng * ioctls for /dev/kvm fds:
2840314b30SXiaoye Zheng */
2940314b30SXiaoye Zheng pub const KVM_GET_API_VERSION: u32 = 0x00;
3040314b30SXiaoye Zheng pub const KVM_CREATE_VM: u32 = 0x01;
3140314b30SXiaoye Zheng pub const KVM_CHECK_EXTENSION: u32 = 0x03;
3240314b30SXiaoye Zheng pub const KVM_GET_VCPU_MMAP_SIZE: u32 = 0x04; // Get size for mmap(vcpu_fd) in bytes
3340314b30SXiaoye Zheng pub const KVM_TRACE_ENABLE: u32 = 0x05;
3440314b30SXiaoye Zheng pub const KVM_TRACE_PAUSE: u32 = 0x06;
3540314b30SXiaoye Zheng pub const KVM_TRACE_DISABLE: u32 = 0x07;
3640314b30SXiaoye Zheng
3740314b30SXiaoye Zheng #[derive(Debug)]
3840314b30SXiaoye Zheng pub struct KvmInode {
3940314b30SXiaoye Zheng /// uuid 暂时不知道有什么用(x
4040314b30SXiaoye Zheng // uuid: Uuid,
4140314b30SXiaoye Zheng /// 指向自身的弱引用
4240314b30SXiaoye Zheng self_ref: Weak<LockedKvmInode>,
4340314b30SXiaoye Zheng /// 指向inode所在的文件系统对象的指针
4440314b30SXiaoye Zheng fs: Weak<DevFS>,
4540314b30SXiaoye Zheng /// INode 元数据
4640314b30SXiaoye Zheng metadata: Metadata,
4740314b30SXiaoye Zheng }
4840314b30SXiaoye Zheng
4940314b30SXiaoye Zheng #[derive(Debug)]
5040314b30SXiaoye Zheng pub struct LockedKvmInode(SpinLock<KvmInode>);
5140314b30SXiaoye Zheng
5240314b30SXiaoye Zheng impl LockedKvmInode {
new() -> Arc<Self>5340314b30SXiaoye Zheng pub fn new() -> Arc<Self> {
5440314b30SXiaoye Zheng let inode = KvmInode {
5540314b30SXiaoye Zheng self_ref: Weak::default(),
5640314b30SXiaoye Zheng fs: Weak::default(),
5740314b30SXiaoye Zheng metadata: Metadata {
5840314b30SXiaoye Zheng dev_id: 1,
5940314b30SXiaoye Zheng inode_id: generate_inode_id(),
6040314b30SXiaoye Zheng size: 0,
6140314b30SXiaoye Zheng blk_size: 0,
6240314b30SXiaoye Zheng blocks: 0,
636fc066acSJomo atime: PosixTimeSpec::default(),
646fc066acSJomo mtime: PosixTimeSpec::default(),
656fc066acSJomo ctime: PosixTimeSpec::default(),
6640314b30SXiaoye Zheng file_type: FileType::KvmDevice, // 文件夹,block设备,char设备
6740314b30SXiaoye Zheng mode: filesystem::vfs::syscall::ModeType::S_IALLUGO,
6840314b30SXiaoye Zheng nlinks: 1,
6940314b30SXiaoye Zheng uid: 0,
7040314b30SXiaoye Zheng gid: 0,
7102343d0bSLoGin raw_dev: DeviceNumber::default(), // 这里用来作为device number
7240314b30SXiaoye Zheng },
7340314b30SXiaoye Zheng };
7440314b30SXiaoye Zheng
7540314b30SXiaoye Zheng let result = Arc::new(LockedKvmInode(SpinLock::new(inode)));
7640314b30SXiaoye Zheng result.0.lock().self_ref = Arc::downgrade(&result);
7740314b30SXiaoye Zheng
7840314b30SXiaoye Zheng return result;
7940314b30SXiaoye Zheng }
8040314b30SXiaoye Zheng }
8140314b30SXiaoye Zheng
8240314b30SXiaoye Zheng impl DeviceINode for LockedKvmInode {
set_fs(&self, fs: Weak<DevFS>)8340314b30SXiaoye Zheng fn set_fs(&self, fs: Weak<DevFS>) {
8440314b30SXiaoye Zheng self.0.lock().fs = fs;
8540314b30SXiaoye Zheng }
8640314b30SXiaoye Zheng }
8740314b30SXiaoye Zheng
8840314b30SXiaoye Zheng impl IndexNode for LockedKvmInode {
as_any_ref(&self) -> &dyn core::any::Any8940314b30SXiaoye Zheng fn as_any_ref(&self) -> &dyn core::any::Any {
9040314b30SXiaoye Zheng self
9140314b30SXiaoye Zheng }
9240314b30SXiaoye Zheng
open( &self, _data: SpinLockGuard<FilePrivateData>, _mode: &FileMode, ) -> Result<(), SystemError>93dfe53cf0SGnoCiYeH fn open(
94dfe53cf0SGnoCiYeH &self,
95dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>,
96dfe53cf0SGnoCiYeH _mode: &FileMode,
97dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> {
98*2eab6dd7S曾俊 debug!("file private data:{:?}", _data);
9940314b30SXiaoye Zheng return Ok(());
10040314b30SXiaoye Zheng }
10140314b30SXiaoye Zheng
close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError>102dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> {
10340314b30SXiaoye Zheng return Ok(());
10440314b30SXiaoye Zheng }
10540314b30SXiaoye Zheng
metadata(&self) -> Result<Metadata, SystemError>10640314b30SXiaoye Zheng fn metadata(&self) -> Result<Metadata, SystemError> {
10740314b30SXiaoye Zheng return Ok(self.0.lock().metadata.clone());
10840314b30SXiaoye Zheng }
10940314b30SXiaoye Zheng
fs(&self) -> Arc<dyn FileSystem>11040314b30SXiaoye Zheng fn fs(&self) -> Arc<dyn FileSystem> {
11140314b30SXiaoye Zheng return self.0.lock().fs.upgrade().unwrap();
11240314b30SXiaoye Zheng }
11340314b30SXiaoye Zheng
list(&self) -> Result<Vec<String>, SystemError>11440314b30SXiaoye Zheng fn list(&self) -> Result<Vec<String>, SystemError> {
1151074eb34SSamuel Dai Err(SystemError::ENOSYS)
11640314b30SXiaoye Zheng }
11740314b30SXiaoye Zheng
set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError>11840314b30SXiaoye Zheng fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> {
11940314b30SXiaoye Zheng let mut inode = self.0.lock();
12040314b30SXiaoye Zheng inode.metadata.atime = metadata.atime;
12140314b30SXiaoye Zheng inode.metadata.mtime = metadata.mtime;
12240314b30SXiaoye Zheng inode.metadata.ctime = metadata.ctime;
12340314b30SXiaoye Zheng inode.metadata.mode = metadata.mode;
12440314b30SXiaoye Zheng inode.metadata.uid = metadata.uid;
12540314b30SXiaoye Zheng inode.metadata.gid = metadata.gid;
12640314b30SXiaoye Zheng
12740314b30SXiaoye Zheng return Ok(());
12840314b30SXiaoye Zheng }
12940314b30SXiaoye Zheng
13040314b30SXiaoye Zheng /// @brief io control接口
13140314b30SXiaoye Zheng ///
13240314b30SXiaoye Zheng /// @param cmd 命令
13340314b30SXiaoye Zheng /// @param data 数据
13440314b30SXiaoye Zheng ///
13540314b30SXiaoye Zheng /// @return 成功:Ok()
13640314b30SXiaoye Zheng /// 失败:Err(错误码)
ioctl( &self, cmd: u32, data: usize, _private_data: &FilePrivateData, ) -> Result<usize, SystemError>13752da9a59SGnoCiYeH fn ioctl(
13852da9a59SGnoCiYeH &self,
13952da9a59SGnoCiYeH cmd: u32,
14052da9a59SGnoCiYeH data: usize,
14152da9a59SGnoCiYeH _private_data: &FilePrivateData,
14252da9a59SGnoCiYeH ) -> Result<usize, SystemError> {
14340314b30SXiaoye Zheng match cmd {
14440314b30SXiaoye Zheng 0xdeadbeef => {
145*2eab6dd7S曾俊 debug!("kvm ioctl");
14640314b30SXiaoye Zheng Ok(0)
14740314b30SXiaoye Zheng }
14840314b30SXiaoye Zheng KVM_GET_API_VERSION => Ok(KVM_API_VERSION as usize),
14940314b30SXiaoye Zheng KVM_CREATE_VM => {
150*2eab6dd7S曾俊 debug!("kvm KVM_CREATE_VM");
15140314b30SXiaoye Zheng kvm_dev_ioctl_create_vm(data)
15240314b30SXiaoye Zheng }
15340314b30SXiaoye Zheng KVM_CHECK_EXTENSION
15440314b30SXiaoye Zheng | KVM_GET_VCPU_MMAP_SIZE
15540314b30SXiaoye Zheng | KVM_TRACE_ENABLE
15640314b30SXiaoye Zheng | KVM_TRACE_PAUSE
1571074eb34SSamuel Dai | KVM_TRACE_DISABLE => Err(SystemError::ENOSYS),
15840314b30SXiaoye Zheng _ => KVMArch::kvm_arch_dev_ioctl(cmd, data),
15940314b30SXiaoye Zheng }
16040314b30SXiaoye Zheng }
16140314b30SXiaoye Zheng /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写
read_at( &self, _offset: usize, _len: usize, _buf: &mut [u8], _data: SpinLockGuard<FilePrivateData>, ) -> Result<usize, SystemError>16240314b30SXiaoye Zheng fn read_at(
16340314b30SXiaoye Zheng &self,
16440314b30SXiaoye Zheng _offset: usize,
16540314b30SXiaoye Zheng _len: usize,
16640314b30SXiaoye Zheng _buf: &mut [u8],
167dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>,
16840314b30SXiaoye Zheng ) -> Result<usize, SystemError> {
1691074eb34SSamuel Dai Err(SystemError::ENOSYS)
17040314b30SXiaoye Zheng }
17140314b30SXiaoye Zheng
17240314b30SXiaoye Zheng /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写
write_at( &self, _offset: usize, _len: usize, _buf: &[u8], _data: SpinLockGuard<FilePrivateData>, ) -> Result<usize, SystemError>17340314b30SXiaoye Zheng fn write_at(
17440314b30SXiaoye Zheng &self,
17540314b30SXiaoye Zheng _offset: usize,
17640314b30SXiaoye Zheng _len: usize,
17740314b30SXiaoye Zheng _buf: &[u8],
178dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>,
17940314b30SXiaoye Zheng ) -> Result<usize, SystemError> {
1801074eb34SSamuel Dai Err(SystemError::ENOSYS)
18140314b30SXiaoye Zheng }
18240314b30SXiaoye Zheng }
18340314b30SXiaoye Zheng
18440314b30SXiaoye Zheng #[no_mangle]
kvm_dev_ioctl_create_vm(_vmtype: usize) -> Result<usize, SystemError>18540314b30SXiaoye Zheng pub fn kvm_dev_ioctl_create_vm(_vmtype: usize) -> Result<usize, SystemError> {
18640314b30SXiaoye Zheng push_vm(0).expect("need a valid vm!");
18740314b30SXiaoye Zheng
18840314b30SXiaoye Zheng // 创建vm文件,返回文件描述符
18940314b30SXiaoye Zheng let vm_inode = LockedVmInode::new();
19040314b30SXiaoye Zheng let file: File = File::new(vm_inode, FileMode::O_RDWR)?;
19140314b30SXiaoye Zheng let r = ProcessManager::current_pcb()
19240314b30SXiaoye Zheng .fd_table()
19340314b30SXiaoye Zheng .write()
19440314b30SXiaoye Zheng .alloc_fd(file, None)
19540314b30SXiaoye Zheng .map(|fd| fd as usize);
19640314b30SXiaoye Zheng return r;
19740314b30SXiaoye Zheng }
198