xref: /DragonOS/kernel/src/virt/kvm/kvm_dev.rs (revision f110d330d5493f383067b4e82ebbfb72f40457b2)
1 use crate::filesystem::devfs::{DevFS, DeviceINode};
2 use crate::filesystem::vfs::{
3     core::generate_inode_id,
4     file::{File, FileMode},
5     make_rawdev, FilePrivateData, FileSystem, FileType, IndexNode, Metadata,
6 };
7 use crate::process::ProcessManager;
8 use crate::{arch::KVMArch, libs::spinlock::SpinLock, syscall::SystemError, time::TimeSpec};
9 use crate::{filesystem, kdebug};
10 // use crate::virt::kvm::{host_stack};
11 use super::push_vm;
12 use crate::virt::kvm::vm_dev::LockedVmInode;
13 use alloc::{
14     string::String,
15     sync::{Arc, Weak},
16     vec::Vec,
17 };
18 
19 pub const KVM_API_VERSION: u32 = 12;
20 
21 // use crate::virt::kvm::kvm_dev_ioctl_create_vm;
22 /*
23  * ioctls for /dev/kvm fds:
24  */
25 pub const KVM_GET_API_VERSION: u32 = 0x00;
26 pub const KVM_CREATE_VM: u32 = 0x01;
27 pub const KVM_CHECK_EXTENSION: u32 = 0x03;
28 pub const KVM_GET_VCPU_MMAP_SIZE: u32 = 0x04; // Get size for mmap(vcpu_fd) in bytes
29 pub const KVM_TRACE_ENABLE: u32 = 0x05;
30 pub const KVM_TRACE_PAUSE: u32 = 0x06;
31 pub const KVM_TRACE_DISABLE: u32 = 0x07;
32 
33 #[derive(Debug)]
34 pub struct KvmInode {
35     /// uuid 暂时不知道有什么用(x
36     // uuid: Uuid,
37     /// 指向自身的弱引用
38     self_ref: Weak<LockedKvmInode>,
39     /// 指向inode所在的文件系统对象的指针
40     fs: Weak<DevFS>,
41     /// INode 元数据
42     metadata: Metadata,
43 }
44 
45 #[derive(Debug)]
46 pub struct LockedKvmInode(SpinLock<KvmInode>);
47 
48 impl LockedKvmInode {
49     pub fn new() -> Arc<Self> {
50         let inode = KvmInode {
51             self_ref: Weak::default(),
52             fs: Weak::default(),
53             metadata: Metadata {
54                 dev_id: 1,
55                 inode_id: generate_inode_id(),
56                 size: 0,
57                 blk_size: 0,
58                 blocks: 0,
59                 atime: TimeSpec::default(),
60                 mtime: TimeSpec::default(),
61                 ctime: TimeSpec::default(),
62                 file_type: FileType::KvmDevice, // 文件夹,block设备,char设备
63                 mode: filesystem::vfs::syscall::ModeType::S_IALLUGO,
64                 nlinks: 1,
65                 uid: 0,
66                 gid: 0,
67                 raw_dev: make_rawdev(1, 4), // 这里用来作为device number
68             },
69         };
70 
71         let result = Arc::new(LockedKvmInode(SpinLock::new(inode)));
72         result.0.lock().self_ref = Arc::downgrade(&result);
73 
74         return result;
75     }
76 }
77 
78 impl DeviceINode for LockedKvmInode {
79     fn set_fs(&self, fs: Weak<DevFS>) {
80         self.0.lock().fs = fs;
81     }
82 }
83 
84 impl IndexNode for LockedKvmInode {
85     fn as_any_ref(&self) -> &dyn core::any::Any {
86         self
87     }
88 
89     fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> {
90         kdebug!("file private data:{:?}", _data);
91         return Ok(());
92     }
93 
94     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
95         return Ok(());
96     }
97 
98     fn metadata(&self) -> Result<Metadata, SystemError> {
99         return Ok(self.0.lock().metadata.clone());
100     }
101 
102     fn fs(&self) -> Arc<dyn FileSystem> {
103         return self.0.lock().fs.upgrade().unwrap();
104     }
105 
106     fn list(&self) -> Result<Vec<String>, SystemError> {
107         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
108     }
109 
110     fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> {
111         let mut inode = self.0.lock();
112         inode.metadata.atime = metadata.atime;
113         inode.metadata.mtime = metadata.mtime;
114         inode.metadata.ctime = metadata.ctime;
115         inode.metadata.mode = metadata.mode;
116         inode.metadata.uid = metadata.uid;
117         inode.metadata.gid = metadata.gid;
118 
119         return Ok(());
120     }
121 
122     /// @brief io control接口
123     ///
124     /// @param cmd 命令
125     /// @param data 数据
126     ///
127     /// @return 成功:Ok()
128     ///         失败:Err(错误码)
129     fn ioctl(&self, cmd: u32, data: usize) -> Result<usize, SystemError> {
130         match cmd {
131             0xdeadbeef => {
132                 kdebug!("kvm ioctl");
133                 Ok(0)
134             }
135             KVM_GET_API_VERSION => Ok(KVM_API_VERSION as usize),
136             KVM_CREATE_VM => {
137                 kdebug!("kvm KVM_CREATE_VM");
138                 kvm_dev_ioctl_create_vm(data)
139             }
140             KVM_CHECK_EXTENSION
141             | KVM_GET_VCPU_MMAP_SIZE
142             | KVM_TRACE_ENABLE
143             | KVM_TRACE_PAUSE
144             | KVM_TRACE_DISABLE => Err(SystemError::EOPNOTSUPP_OR_ENOTSUP),
145             _ => KVMArch::kvm_arch_dev_ioctl(cmd, data),
146         }
147     }
148     /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写
149     fn read_at(
150         &self,
151         _offset: usize,
152         _len: usize,
153         _buf: &mut [u8],
154         _data: &mut FilePrivateData,
155     ) -> Result<usize, SystemError> {
156         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
157     }
158 
159     /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写
160     fn write_at(
161         &self,
162         _offset: usize,
163         _len: usize,
164         _buf: &[u8],
165         _data: &mut FilePrivateData,
166     ) -> Result<usize, SystemError> {
167         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
168     }
169 }
170 
171 #[no_mangle]
172 pub fn kvm_dev_ioctl_create_vm(_vmtype: usize) -> Result<usize, SystemError> {
173     push_vm(0).expect("need a valid vm!");
174 
175     // 创建vm文件,返回文件描述符
176     let vm_inode = LockedVmInode::new();
177     let file: File = File::new(vm_inode, FileMode::O_RDWR)?;
178     let r = ProcessManager::current_pcb()
179         .fd_table()
180         .write()
181         .alloc_fd(file, None)
182         .map(|fd| fd as usize);
183     return r;
184 }
185