xref: /DragonOS/kernel/src/virt/kvm/vm.rs (revision 2eab6dd743e94a86a685f1f3c01e599adf86610a)
140314b30SXiaoye Zheng use crate::arch::kvm::vmx::vcpu::VmxVcpu;
2*2eab6dd7S曾俊 use crate::arch::KVMArch;
34fda81ceSLoGin use crate::arch::MMArch;
440314b30SXiaoye Zheng use crate::libs::mutex::Mutex;
54fda81ceSLoGin use crate::mm::MemoryManagementArch;
640314b30SXiaoye Zheng use alloc::sync::Arc;
740314b30SXiaoye Zheng use alloc::vec::Vec;
8*2eab6dd7S曾俊 use log::debug;
991e9d4abSLoGin use system_error::SystemError;
1040314b30SXiaoye Zheng 
1140314b30SXiaoye Zheng // use super::HOST_STACK_SIZE;
1240314b30SXiaoye Zheng use super::host_mem::{
1340314b30SXiaoye Zheng     KvmMemoryChange, KvmMemorySlot, KvmMemorySlots, KvmUserspaceMemoryRegion,
1440314b30SXiaoye Zheng     KVM_ADDRESS_SPACE_NUM, KVM_MEM_LOG_DIRTY_PAGES, KVM_MEM_MAX_NR_PAGES, KVM_MEM_READONLY,
1540314b30SXiaoye Zheng     KVM_MEM_SLOTS_NUM, KVM_USER_MEM_SLOTS, PAGE_SHIFT,
1640314b30SXiaoye Zheng };
1740314b30SXiaoye Zheng 
1840314b30SXiaoye Zheng #[derive(Debug, Clone)]
1940314b30SXiaoye Zheng pub struct Vm {
2040314b30SXiaoye Zheng     pub id: usize,
2140314b30SXiaoye Zheng     // vcpu config
2240314b30SXiaoye Zheng     pub nr_vcpus: u32, /* Number of cpus to run */
2340314b30SXiaoye Zheng     pub vcpu: Vec<Arc<Mutex<VmxVcpu>>>,
2440314b30SXiaoye Zheng     // memory config
2540314b30SXiaoye Zheng     pub nr_mem_slots: u32, /* Number of memory slots in each address space */
2640314b30SXiaoye Zheng     pub memslots: [KvmMemorySlots; KVM_ADDRESS_SPACE_NUM],
2740314b30SXiaoye Zheng     // arch related config
2840314b30SXiaoye Zheng     pub arch: KVMArch,
2940314b30SXiaoye Zheng }
3040314b30SXiaoye Zheng 
3140314b30SXiaoye Zheng impl Vm {
3240314b30SXiaoye Zheng     pub fn new(id: usize) -> Result<Self, SystemError> {
3340314b30SXiaoye Zheng         let vcpu = Vec::new();
3440314b30SXiaoye Zheng         // Allocate stack for vm-exit handlers and fill it with garbage data
3540314b30SXiaoye Zheng         let instance = Self {
3640314b30SXiaoye Zheng             id,
3740314b30SXiaoye Zheng             nr_vcpus: 0,
3840314b30SXiaoye Zheng             vcpu,
3940314b30SXiaoye Zheng             nr_mem_slots: KVM_MEM_SLOTS_NUM,
4040314b30SXiaoye Zheng             memslots: [KvmMemorySlots::default(); KVM_ADDRESS_SPACE_NUM],
4140314b30SXiaoye Zheng             arch: Default::default(),
4240314b30SXiaoye Zheng         };
4340314b30SXiaoye Zheng         Ok(instance)
4440314b30SXiaoye Zheng     }
4540314b30SXiaoye Zheng 
4640314b30SXiaoye Zheng     /// Allocate some memory and give it an address in the guest physical address space.
4740314b30SXiaoye Zheng     pub fn set_user_memory_region(
4840314b30SXiaoye Zheng         &mut self,
4940314b30SXiaoye Zheng         mem: &KvmUserspaceMemoryRegion,
5040314b30SXiaoye Zheng     ) -> Result<(), SystemError> {
51*2eab6dd7S曾俊         debug!("set_user_memory_region");
5240314b30SXiaoye Zheng         let id: u16 = mem.slot as u16; // slot id
5340314b30SXiaoye Zheng         let as_id = mem.slot >> 16; // address space id
54*2eab6dd7S曾俊         debug!("id={}, as_id={}", id, as_id);
5540314b30SXiaoye Zheng 
5640314b30SXiaoye Zheng         // 检查slot是否合法
5740314b30SXiaoye Zheng         if mem.slot as usize >= self.nr_mem_slots as usize {
5840314b30SXiaoye Zheng             return Err(SystemError::EINVAL);
5940314b30SXiaoye Zheng         }
6040314b30SXiaoye Zheng         // 检查flags是否合法
6140314b30SXiaoye Zheng         self.check_memory_region_flag(mem)?;
6240314b30SXiaoye Zheng         // 内存大小和地址必须是页对齐的
634fda81ceSLoGin         if (mem.memory_size & (MMArch::PAGE_SIZE - 1) as u64) != 0
644fda81ceSLoGin             || (mem.guest_phys_addr & (MMArch::PAGE_SIZE - 1) as u64) != 0
6540314b30SXiaoye Zheng         {
6640314b30SXiaoye Zheng             return Err(SystemError::EINVAL);
6740314b30SXiaoye Zheng         }
6840314b30SXiaoye Zheng         // 检查地址空间是否合法
6940314b30SXiaoye Zheng         if as_id >= (KVM_ADDRESS_SPACE_NUM as u32) || id >= KVM_MEM_SLOTS_NUM as u16 {
7040314b30SXiaoye Zheng             return Err(SystemError::EINVAL);
7140314b30SXiaoye Zheng         }
7240314b30SXiaoye Zheng         // if mem.memory_size < 0 {
7340314b30SXiaoye Zheng         //     return Err(SystemError::EINVAL);
7440314b30SXiaoye Zheng         // }
7540314b30SXiaoye Zheng         let slot = &self.memslots[as_id as usize].memslots[id as usize];
7640314b30SXiaoye Zheng         let base_gfn = mem.guest_phys_addr >> PAGE_SHIFT;
7740314b30SXiaoye Zheng         let npages = mem.memory_size >> PAGE_SHIFT;
7840314b30SXiaoye Zheng         if npages > KVM_MEM_MAX_NR_PAGES as u64 {
7940314b30SXiaoye Zheng             return Err(SystemError::EINVAL);
8040314b30SXiaoye Zheng         }
8140314b30SXiaoye Zheng         let change: KvmMemoryChange;
8240314b30SXiaoye Zheng 
8340314b30SXiaoye Zheng         let old_slot = slot;
8440314b30SXiaoye Zheng         let mut new_slot = KvmMemorySlot {
8540314b30SXiaoye Zheng             base_gfn, // 虚机内存区间起始物理页框号
8640314b30SXiaoye Zheng             npages,   // 虚机内存区间页数,即内存区间的大小
8740314b30SXiaoye Zheng             // dirty_bitmap: old_slot.dirty_bitmap,
8840314b30SXiaoye Zheng             userspace_addr: mem.userspace_addr, // 虚机内存区间对应的主机虚拟地址
8940314b30SXiaoye Zheng             flags: mem.flags,                   // 虚机内存区间属性
9040314b30SXiaoye Zheng             id,                                 // 虚机内存区间id
9140314b30SXiaoye Zheng         };
9240314b30SXiaoye Zheng 
9340314b30SXiaoye Zheng         // 判断新memoryslot的类型
9440314b30SXiaoye Zheng         if npages != 0 {
9540314b30SXiaoye Zheng             //映射内存有大小,不是删除内存条
9640314b30SXiaoye Zheng             if old_slot.npages == 0 {
9740314b30SXiaoye Zheng                 //内存槽号没有虚拟内存条,意味内存新创建
9840314b30SXiaoye Zheng                 change = KvmMemoryChange::Create;
9940314b30SXiaoye Zheng             } else {
10040314b30SXiaoye Zheng                 //修改已存在的内存,表示修改标志或者平移映射地址
10140314b30SXiaoye Zheng                 // 检查内存条是否可以修改
10240314b30SXiaoye Zheng                 if mem.userspace_addr != old_slot.userspace_addr
10340314b30SXiaoye Zheng                     || npages != old_slot.npages
10440314b30SXiaoye Zheng                     || (new_slot.flags ^ old_slot.flags & KVM_MEM_READONLY) != 0
10540314b30SXiaoye Zheng                 {
10640314b30SXiaoye Zheng                     return Err(SystemError::EINVAL);
10740314b30SXiaoye Zheng                 }
10840314b30SXiaoye Zheng                 if new_slot.base_gfn != old_slot.base_gfn {
10940314b30SXiaoye Zheng                     //guest地址不同,内存条平移
11040314b30SXiaoye Zheng                     change = KvmMemoryChange::Move;
11140314b30SXiaoye Zheng                 } else if new_slot.flags != old_slot.flags {
11240314b30SXiaoye Zheng                     //内存条标志不同,修改标志
11340314b30SXiaoye Zheng                     change = KvmMemoryChange::FlagsOnly;
11440314b30SXiaoye Zheng                 } else {
11540314b30SXiaoye Zheng                     return Ok(());
11640314b30SXiaoye Zheng                 }
11740314b30SXiaoye Zheng             }
11840314b30SXiaoye Zheng         } else {
11940314b30SXiaoye Zheng             if old_slot.npages == 0 {
12040314b30SXiaoye Zheng                 //内存槽号没有虚拟内存条,不可以删除
12140314b30SXiaoye Zheng                 return Err(SystemError::EINVAL);
12240314b30SXiaoye Zheng             }
12340314b30SXiaoye Zheng             //申请插入的内存为0,而内存槽上有内存,意味删除
12440314b30SXiaoye Zheng             change = KvmMemoryChange::Delete;
12540314b30SXiaoye Zheng             new_slot.base_gfn = 0;
12640314b30SXiaoye Zheng             new_slot.flags = 0;
12740314b30SXiaoye Zheng         }
12840314b30SXiaoye Zheng 
12940314b30SXiaoye Zheng         if change == KvmMemoryChange::Create || change == KvmMemoryChange::Move {
13040314b30SXiaoye Zheng             // 检查内存区域是否重叠
13140314b30SXiaoye Zheng             for i in 0..KVM_MEM_SLOTS_NUM {
13240314b30SXiaoye Zheng                 let memslot = &self.memslots[as_id as usize].memslots[i as usize];
13340314b30SXiaoye Zheng                 if memslot.id == id || memslot.id as u32 >= KVM_USER_MEM_SLOTS {
13440314b30SXiaoye Zheng                     continue;
13540314b30SXiaoye Zheng                 }
13640314b30SXiaoye Zheng                 // 当前已有的slot与new在guest物理地址上有交集
13740314b30SXiaoye Zheng                 if !(base_gfn + npages <= memslot.base_gfn
13840314b30SXiaoye Zheng                     || memslot.base_gfn + memslot.npages <= base_gfn)
13940314b30SXiaoye Zheng                 {
14040314b30SXiaoye Zheng                     return Err(SystemError::EEXIST);
14140314b30SXiaoye Zheng                 }
14240314b30SXiaoye Zheng             }
14340314b30SXiaoye Zheng         }
14440314b30SXiaoye Zheng 
145840045afSLoGin         if (new_slot.flags & KVM_MEM_LOG_DIRTY_PAGES) == 0 {
14640314b30SXiaoye Zheng             // new_slot.dirty_bitmap = 0;
14740314b30SXiaoye Zheng         }
14840314b30SXiaoye Zheng 
14940314b30SXiaoye Zheng         // 根据flags的值,决定是否创建内存脏页
15040314b30SXiaoye Zheng         // if (new_slot.flags & KVM_MEM_LOG_DIRTY_PAGES)!=0 && new_slot.dirty_bitmap == 0 {
15140314b30SXiaoye Zheng         //     let type_size = core::mem::size_of::<u64>() as u64;
15240314b30SXiaoye Zheng         //     let dirty_bytes = 2 * ((new_slot.npages+type_size-1) / type_size) / 8;
15340314b30SXiaoye Zheng         // new_slot.dirty_bitmap = Box::new(vec![0; dirty_bytes as u8]);
15440314b30SXiaoye Zheng         // }
15540314b30SXiaoye Zheng         if change == KvmMemoryChange::Create {
15640314b30SXiaoye Zheng             new_slot.userspace_addr = mem.userspace_addr;
157840045afSLoGin             let mut memslots = self.memslots[as_id as usize].memslots;
15840314b30SXiaoye Zheng             memslots[id as usize] = new_slot;
15940314b30SXiaoye Zheng             self.memslots[as_id as usize].memslots = memslots;
16040314b30SXiaoye Zheng             self.memslots[as_id as usize].used_slots += 1;
16140314b30SXiaoye Zheng             // KVMArch::kvm_arch_create_memslot(&mut new_slot, npages);
16240314b30SXiaoye Zheng             // KVMArch::kvm_arch_commit_memory_region(mem, &new_slot, old_slot, change);
16340314b30SXiaoye Zheng         }
16440314b30SXiaoye Zheng         // TODO--KvmMemoryChange::Delete & Move
16540314b30SXiaoye Zheng         Ok(())
16640314b30SXiaoye Zheng     }
16740314b30SXiaoye Zheng 
16840314b30SXiaoye Zheng     fn check_memory_region_flag(&self, mem: &KvmUserspaceMemoryRegion) -> Result<(), SystemError> {
16940314b30SXiaoye Zheng         let valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
17040314b30SXiaoye Zheng         // 除了valid_flags之外的flags被置1了,就返回错误
17140314b30SXiaoye Zheng         if mem.flags & !valid_flags != 0 {
17240314b30SXiaoye Zheng             return Err(SystemError::EINVAL);
17340314b30SXiaoye Zheng         }
17440314b30SXiaoye Zheng         Ok(())
17540314b30SXiaoye Zheng     }
17640314b30SXiaoye Zheng }
177