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