@@ -97,23 +97,22 @@ impl ConfidentialVm {
9797 /// # Guarantees
9898 ///
9999 /// The physical hart is configured to enforce memory access control so that the confidential VM has access only to its own memory.
100- pub fn steal_confidential_hart ( & mut self , confidential_hart_id : usize , hardware_hart : & mut HardwareHart ) -> Result < ( ) , Error > {
100+ pub fn steal_confidential_hart ( & self , confidential_hart_id : usize , hardware_hart : & mut HardwareHart ) -> Result < ( ) , Error > {
101101 ensure ! ( confidential_hart_id < self . confidential_harts. len( ) , Error :: InvalidHartId ( ) ) ?;
102- let confidential_hart = self . confidential_harts [ confidential_hart_id] . get_mut ( ) ;
102+ let mut confidential_hart = self . confidential_harts [ confidential_hart_id] . write ( ) ;
103103 // The hypervisor might try to schedule the same confidential hart on different physical harts. We detect it
104104 // because after a confidential_hart is scheduled for the first time, its token is stolen and the
105105 // ConfidentialVM is left with a dummy confidential_hart. A dummy confidential hart is a hart not associated
106106 // with any confidential vm.
107107 ensure_not ! ( confidential_hart. is_dummy( ) , Error :: HartAlreadyRunning ( ) ) ?;
108108 // The hypervisor might try to schedule a confidential hart that has never been started. This is forbidden.
109109 ensure ! ( confidential_hart. is_executable( ) , Error :: HartNotExecutable ( ) ) ?;
110- unsafe {
111- core:: ptr:: swap ( hardware_hart. confidential_hart_mut ( ) as * mut ConfidentialHart , confidential_hart as * mut ConfidentialHart ) ;
112- // Reconfigure the hardware memory isolation mechanism to enforce that the confidential virtual machine has access only to the
113- // memory regions it owns. Below invocation is safe because we are now in the confidential flow part of the finite state
114- // machine and the virtual hart is assigned to the hardware hart.
115- self . memory_protector ( ) . enable ( )
116- } ;
110+
111+ core:: mem:: swap ( hardware_hart. confidential_hart_mut ( ) , & mut confidential_hart) ;
112+ // Reconfigure the hardware memory isolation mechanism to enforce that the confidential virtual machine has access only to the
113+ // memory regions it owns. Below invocation is safe because we are now in the confidential flow part of the finite state
114+ // machine and the virtual hart is assigned to the hardware hart.
115+ unsafe { self . memory_protector ( ) . enable ( ) } ;
117116 Ok ( ( ) )
118117 }
119118
@@ -123,15 +122,12 @@ impl ConfidentialVm {
123122 /// # Safety
124123 ///
125124 /// A confidential hart belongs to this confidential VM and is currently assigned to the hardware hart.
126- pub fn return_confidential_hart ( & mut self , hardware_hart : & mut HardwareHart ) {
125+ pub fn return_confidential_hart ( & self , hardware_hart : & mut HardwareHart ) {
127126 assert ! ( !hardware_hart. confidential_hart( ) . is_dummy( ) ) ;
128127 assert ! ( Some ( self . id) == hardware_hart. confidential_hart( ) . confidential_vm_id( ) ) ;
129128 let confidential_hart_id = hardware_hart. confidential_hart ( ) . confidential_hart_id ( ) ;
130129 assert ! ( self . confidential_harts. len( ) > confidential_hart_id) ;
131- let confidential_hart = self . confidential_harts [ confidential_hart_id] . get_mut ( ) ;
132- unsafe {
133- core:: ptr:: swap ( hardware_hart. confidential_hart_mut ( ) as * mut ConfidentialHart , confidential_hart as * mut ConfidentialHart ) ;
134- }
130+ core:: mem:: swap ( hardware_hart. confidential_hart_mut ( ) , & mut self . confidential_harts [ confidential_hart_id] . write ( ) ) ;
135131 }
136132}
137133
0 commit comments