xref: /DragonOS/kernel/src/virt/kvm/vm.rs (revision af3543100543b9ac6159dc9f9367a76ff670b1f3)
1 use crate::arch::kvm::vmx::vcpu::VmxVcpu;
2 use crate::arch::MMArch;
3 use crate::libs::mutex::Mutex;
4 use crate::mm::MemoryManagementArch;
5 use crate::syscall::SystemError;
6 use crate::{arch::KVMArch, kdebug};
7 use alloc::sync::Arc;
8 use alloc::vec::Vec;
9 
10 // use super::HOST_STACK_SIZE;
11 use super::host_mem::{
12     KvmMemoryChange, KvmMemorySlot, KvmMemorySlots, KvmUserspaceMemoryRegion,
13     KVM_ADDRESS_SPACE_NUM, KVM_MEM_LOG_DIRTY_PAGES, KVM_MEM_MAX_NR_PAGES, KVM_MEM_READONLY,
14     KVM_MEM_SLOTS_NUM, KVM_USER_MEM_SLOTS, PAGE_SHIFT,
15 };
16 // use crate::kdebug;
17 
18 #[derive(Debug, Clone)]
19 pub struct Vm {
20     pub id: usize,
21     // vcpu config
22     pub nr_vcpus: u32, /* Number of cpus to run */
23     pub vcpu: Vec<Arc<Mutex<VmxVcpu>>>,
24     // memory config
25     pub nr_mem_slots: u32, /* Number of memory slots in each address space */
26     pub memslots: [KvmMemorySlots; KVM_ADDRESS_SPACE_NUM],
27     // arch related config
28     pub arch: KVMArch,
29 }
30 
31 impl Vm {
32     pub fn new(id: usize) -> Result<Self, SystemError> {
33         let vcpu = Vec::new();
34         // Allocate stack for vm-exit handlers and fill it with garbage data
35         let instance = Self {
36             id,
37             nr_vcpus: 0,
38             vcpu,
39             nr_mem_slots: KVM_MEM_SLOTS_NUM,
40             memslots: [KvmMemorySlots::default(); KVM_ADDRESS_SPACE_NUM],
41             arch: Default::default(),
42         };
43         Ok(instance)
44     }
45 
46     /// Allocate some memory and give it an address in the guest physical address space.
47     pub fn set_user_memory_region(
48         &mut self,
49         mem: &KvmUserspaceMemoryRegion,
50     ) -> Result<(), SystemError> {
51         kdebug!("set_user_memory_region");
52         let id: u16 = mem.slot as u16; // slot id
53         let as_id = mem.slot >> 16; // address space id
54         kdebug!("id={}, as_id={}", id, as_id);
55 
56         // 检查slot是否合法
57         if mem.slot as usize >= self.nr_mem_slots as usize {
58             return Err(SystemError::EINVAL);
59         }
60         // 检查flags是否合法
61         self.check_memory_region_flag(mem)?;
62         // 内存大小和地址必须是页对齐的
63         if (mem.memory_size & (MMArch::PAGE_SIZE - 1) as u64) != 0
64             || (mem.guest_phys_addr & (MMArch::PAGE_SIZE - 1) as u64) != 0
65         {
66             return Err(SystemError::EINVAL);
67         }
68         // 检查地址空间是否合法
69         if as_id >= (KVM_ADDRESS_SPACE_NUM as u32) || id >= KVM_MEM_SLOTS_NUM as u16 {
70             return Err(SystemError::EINVAL);
71         }
72         // if mem.memory_size < 0 {
73         //     return Err(SystemError::EINVAL);
74         // }
75         let slot = &self.memslots[as_id as usize].memslots[id as usize];
76         let base_gfn = mem.guest_phys_addr >> PAGE_SHIFT;
77         let npages = mem.memory_size >> PAGE_SHIFT;
78         if npages > KVM_MEM_MAX_NR_PAGES as u64 {
79             return Err(SystemError::EINVAL);
80         }
81         let change: KvmMemoryChange;
82 
83         let old_slot = slot;
84         let mut new_slot = KvmMemorySlot {
85             base_gfn, // 虚机内存区间起始物理页框号
86             npages,   // 虚机内存区间页数,即内存区间的大小
87             // dirty_bitmap: old_slot.dirty_bitmap,
88             userspace_addr: mem.userspace_addr, // 虚机内存区间对应的主机虚拟地址
89             flags: mem.flags,                   // 虚机内存区间属性
90             id,                                 // 虚机内存区间id
91         };
92 
93         // 判断新memoryslot的类型
94         if npages != 0 {
95             //映射内存有大小,不是删除内存条
96             if old_slot.npages == 0 {
97                 //内存槽号没有虚拟内存条,意味内存新创建
98                 change = KvmMemoryChange::Create;
99             } else {
100                 //修改已存在的内存,表示修改标志或者平移映射地址
101                 // 检查内存条是否可以修改
102                 if mem.userspace_addr != old_slot.userspace_addr
103                     || npages != old_slot.npages
104                     || (new_slot.flags ^ old_slot.flags & KVM_MEM_READONLY) != 0
105                 {
106                     return Err(SystemError::EINVAL);
107                 }
108                 if new_slot.base_gfn != old_slot.base_gfn {
109                     //guest地址不同,内存条平移
110                     change = KvmMemoryChange::Move;
111                 } else if new_slot.flags != old_slot.flags {
112                     //内存条标志不同,修改标志
113                     change = KvmMemoryChange::FlagsOnly;
114                 } else {
115                     return Ok(());
116                 }
117             }
118         } else {
119             if old_slot.npages == 0 {
120                 //内存槽号没有虚拟内存条,不可以删除
121                 return Err(SystemError::EINVAL);
122             }
123             //申请插入的内存为0,而内存槽上有内存,意味删除
124             change = KvmMemoryChange::Delete;
125             new_slot.base_gfn = 0;
126             new_slot.flags = 0;
127         }
128 
129         if change == KvmMemoryChange::Create || change == KvmMemoryChange::Move {
130             // 检查内存区域是否重叠
131             for i in 0..KVM_MEM_SLOTS_NUM {
132                 let memslot = &self.memslots[as_id as usize].memslots[i as usize];
133                 if memslot.id == id || memslot.id as u32 >= KVM_USER_MEM_SLOTS {
134                     continue;
135                 }
136                 // 当前已有的slot与new在guest物理地址上有交集
137                 if !(base_gfn + npages <= memslot.base_gfn
138                     || memslot.base_gfn + memslot.npages <= base_gfn)
139                 {
140                     return Err(SystemError::EEXIST);
141                 }
142             }
143         }
144 
145         if !(new_slot.flags & KVM_MEM_LOG_DIRTY_PAGES != 0) {
146             // new_slot.dirty_bitmap = 0;
147         }
148 
149         // 根据flags的值,决定是否创建内存脏页
150         // if (new_slot.flags & KVM_MEM_LOG_DIRTY_PAGES)!=0 && new_slot.dirty_bitmap == 0 {
151         //     let type_size = core::mem::size_of::<u64>() as u64;
152         //     let dirty_bytes = 2 * ((new_slot.npages+type_size-1) / type_size) / 8;
153         // new_slot.dirty_bitmap = Box::new(vec![0; dirty_bytes as u8]);
154         // }
155         if change == KvmMemoryChange::Create {
156             new_slot.userspace_addr = mem.userspace_addr;
157             let mut memslots = self.memslots[as_id as usize].memslots.clone();
158             memslots[id as usize] = new_slot;
159             self.memslots[as_id as usize].memslots = memslots;
160             self.memslots[as_id as usize].used_slots += 1;
161             // KVMArch::kvm_arch_create_memslot(&mut new_slot, npages);
162             // KVMArch::kvm_arch_commit_memory_region(mem, &new_slot, old_slot, change);
163         }
164         // TODO--KvmMemoryChange::Delete & Move
165         Ok(())
166     }
167 
168     fn check_memory_region_flag(&self, mem: &KvmUserspaceMemoryRegion) -> Result<(), SystemError> {
169         let valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
170         // 除了valid_flags之外的flags被置1了,就返回错误
171         if mem.flags & !valid_flags != 0 {
172             return Err(SystemError::EINVAL);
173         }
174         Ok(())
175     }
176 }
177