@@ -8,11 +8,11 @@ use std::{
88use arm_vcpu:: Aarch64VCpuSetupConfig ;
99
1010use crate :: {
11- GuestPhysAddr , TASK_STACK_SIZE , VmRunCommonData , VmStatusInitOps ,
12- arch:: { VmStatusRunning , cpu:: VCpu } ,
11+ GuestPhysAddr , TASK_STACK_SIZE , VmAddrSpace , VmMachineInitedOps ,
12+ arch:: { VmMachineRunning , cpu:: VCpu } ,
1313 config:: AxVMConfig ,
14- data2 :: VmDataWeak ,
15- vm:: { MappingFlags , VmId } ,
14+ data :: VmDataWeak ,
15+ vm:: VmId ,
1616} ;
1717
1818const VM_ASPACE_BASE : GuestPhysAddr = GuestPhysAddr :: from_usize ( 0 ) ;
@@ -23,128 +23,14 @@ const VM_ASPACE_END: GuestPhysAddr =
2323pub struct VmMachineInited {
2424 pub id : VmId ,
2525 pub name : String ,
26- // pt_levels: usize,
27- // stop_requested: AtomicBool,
28- pub run_data : VmStatusRunning ,
26+ pub vcpus : Vec < VCpu > ,
27+ pub vmspace : VmAddrSpace ,
2928}
3029
31- impl VmMachineInited {
32- /// Creates a new VM with the given configuration
33- // pub fn new(config: &AxVMConfig) -> anyhow::Result<Self> {
34- // let vm = Self {
35- // id: config.id().into(),
36- // name: config.name().into(),
37- // pt_levels: 4,
38- // stop_requested: AtomicBool::new(false),
39- // run_data: None,
40- // };
41- // Ok(vm)
42- // }
43-
30+ impl VmMachineInited { }
4431
45- /// Initializes the VM, creating vCPUs and setting up memory
46- pub fn init ( & mut self , config : AxVMConfig ) -> anyhow:: Result < ( ) > {
47- debug ! ( "Initializing VM {} ({})" , self . id, self . name) ;
48-
49- let vcpus = self . new_vcpus ( & config) ?;
50-
51- let mut run_data = VmStatusRunning :: new (
52- VmRunCommonData :: new ( self . pt_levels , VM_ASPACE_BASE ..VM_ASPACE_END ) ?,
53- vcpus,
54- ) ;
55-
56- debug ! ( "Mapping memory regions for VM {} ({})" , self . id, self . name) ;
57- for memory_cfg in & config. memory_regions {
58- use crate :: vm:: MappingFlags ;
59- let m = run_data. data . try_use ( ) ?. new_memory (
60- memory_cfg,
61- MappingFlags :: READ
62- | MappingFlags :: WRITE
63- | MappingFlags :: EXECUTE
64- | MappingFlags :: USER ,
65- ) ;
66- run_data. data . add_memory ( m) ;
67- }
68-
69- run_data. data . try_use ( ) ?. load_kernel_image ( & config) ?;
70- run_data. make_dtb ( & config) ?;
71-
72- run_data. data . try_use ( ) ?. map_passthrough_regions ( ) ?;
73-
74- let kernel_entry = run_data. data . try_use ( ) ?. kernel_entry ( ) ;
75- let gpt_root = run_data. data . try_use ( ) ?. gpt_root ( ) ;
76-
77- // Setup vCPUs
78- for vcpu in & mut run_data. vcpus {
79- vcpu. vcpu . set_entry ( kernel_entry) . unwrap ( ) ;
80- vcpu. vcpu . set_dtb_addr ( run_data. dtb_addr ) . unwrap ( ) ;
81-
82- let setup_config = Aarch64VCpuSetupConfig {
83- passthrough_interrupt : config. interrupt_mode ( )
84- == axvmconfig:: VMInterruptMode :: Passthrough ,
85- passthrough_timer : config. interrupt_mode ( )
86- == axvmconfig:: VMInterruptMode :: Passthrough ,
87- } ;
88-
89- vcpu. vcpu
90- . setup ( setup_config)
91- . map_err ( |e| anyhow:: anyhow!( "Failed to setup vCPU : {e:?}" ) ) ?;
92-
93- // Set EPT root
94- vcpu. vcpu
95- . set_ept_root ( gpt_root)
96- . map_err ( |e| anyhow:: anyhow!( "Failed to set EPT root for vCPU : {e:?}" ) ) ?;
97-
98- run_data. vcpu_running_count . fetch_add ( 1 , Ordering :: SeqCst ) ;
99- }
100-
101- self . run_data = Some ( run_data) ;
102-
103- Ok ( ( ) )
104- }
105-
106- fn new_vcpus ( & mut self , config : & AxVMConfig ) -> anyhow:: Result < Vec < VCpu > > {
107- // Create vCPUs
108- let mut vcpus = Vec :: new ( ) ;
109-
110- let dtb_addr = GuestPhysAddr :: from_usize ( 0 ) ;
111-
112- match config. cpu_num {
113- crate :: config:: CpuNumType :: Alloc ( num) => {
114- for _ in 0 ..num {
115- let vcpu = VCpu :: new ( None , dtb_addr) ?;
116- debug ! ( "Created vCPU with {:?}" , vcpu. id) ;
117- vcpus. push ( vcpu) ;
118- }
119- }
120- crate :: config:: CpuNumType :: Fixed ( ref ids) => {
121- for id in ids {
122- let vcpu = VCpu :: new ( Some ( * id) , dtb_addr) ?;
123- debug ! ( "Created vCPU with {:?}" , vcpu. id) ;
124- vcpus. push ( vcpu) ;
125- }
126- }
127- }
128-
129- let vcpu_count = vcpus. len ( ) ;
130-
131- for vcpu in & vcpus {
132- let max_levels = vcpu. with_hcpu ( |cpu| cpu. max_guest_page_table_levels ( ) ) ;
133- if max_levels < self . pt_levels {
134- self . pt_levels = max_levels;
135- }
136- }
137-
138- debug ! (
139- "VM {} ({}) vCPU count: {}, Max Guest Page Table Levels: {}" ,
140- self . id, self . name, vcpu_count, self . pt_levels
141- ) ;
142- Ok ( vcpus)
143- }
144- }
145-
146- impl VmStatusInitOps for VmMachineInited {
147- type Running = VmStatusRunning ;
32+ impl VmMachineInitedOps for VmMachineInited {
33+ type Running = VmMachineRunning ;
14834
14935 fn id ( & self ) -> VmId {
15036 self . id
@@ -155,51 +41,14 @@ impl VmStatusInitOps for VmMachineInited {
15541 }
15642
15743 fn start ( self , vmdata : VmDataWeak ) -> Result < Self :: Running , ( anyhow:: Error , Self ) > {
158- let mut data = self . run_data . unwrap ( ) ;
159-
160- let mut vcpus = vec ! [ ] ;
161-
162- vcpus. append ( & mut data. vcpus ) ;
163- let mut vcpu_handles = vec ! [ ] ;
164- let vm_id = self . id ;
165-
166- for mut vcpu in vcpus. into_iter ( ) {
167- let vcpu_id = vcpu. id ;
168- let vcpu_running_count = data. vcpu_running_count . clone ( ) ;
169- let bind_id = vcpu. binded_cpu_id ( ) ;
170- let handle = std:: thread:: Builder :: new ( )
171- . name ( format ! ( "{vm_id}-{vcpu_id}" ) )
172- . stack_size ( TASK_STACK_SIZE )
173- . spawn ( move || {
174- assert ! (
175- set_current_affinity( AxCpuMask :: one_shot( bind_id. raw( ) ) ) ,
176- "Initialize CPU affinity failed!"
177- ) ;
178- match vcpu. run ( ) {
179- Ok ( ( ) ) => {
180- info ! ( "vCPU {} of VM {} exited normally" , vcpu_id, vm_id) ;
181- }
182- Err ( e) => {
183- error ! (
184- "vCPU {} of VM {} exited with error: {:?}" ,
185- vcpu_id, vm_id, e
186- ) ;
187- }
188- }
189- vcpu_running_count. fetch_sub ( 1 , Ordering :: SeqCst ) ;
190- vcpu
191- } )
192- . unwrap ( ) ;
193-
194- vcpu_handles. push ( handle) ;
195- }
196-
44+ debug ! ( "Starting VM {} ({})" , self . id, self . name) ;
45+ let running = VmMachineRunning :: new ( ) ;
19746 info ! (
19847 "VM {} ({}) with {} cpus booted successfully." ,
19948 self . id,
20049 self . name,
201- vcpu_handles . len( )
50+ self . vcpus . len( )
20251 ) ;
203- Ok ( data )
52+ Ok ( running )
20453 }
20554}
0 commit comments