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