Skip to content

Commit b0b5063

Browse files
committed
further optimizations of locking
Signed-off-by: Wojciech Ozga <woz@zurich.ibm.com>
1 parent a63da69 commit b0b5063

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

security-monitor/src/confidential_flow/finite_state_machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> ConfidentialFlow<'a> {
143143
// Now, we are going to change the context between security domains.
144144
// 1) Store the hypervisor hart state that executed on this physical hart to the main memory.
145145
hardware_hart.hypervisor_hart_mut().save_in_main_memory();
146-
match ControlDataStorage::try_confidential_vm_mut(confidential_vm_id, |mut confidential_vm| {
146+
match ControlDataStorage::try_confidential_vm(confidential_vm_id, |confidential_vm| {
147147
confidential_vm.steal_confidential_hart(confidential_hart_id, hardware_hart)?;
148148
Ok(confidential_vm.allowed_external_interrupts())
149149
}) {
@@ -168,7 +168,7 @@ impl<'a> ConfidentialFlow<'a> {
168168
// Now, we are going to change the context between security domains.
169169
// 1) Store the confidential hart state that executed on this physical hart to the main memory.
170170
self.hardware_hart.confidential_hart_mut().save_in_main_memory();
171-
let _ = ControlDataStorage::try_confidential_vm_mut(self.confidential_vm_id(), |mut confidential_vm| {
171+
let _ = ControlDataStorage::try_confidential_vm(self.confidential_vm_id(), |confidential_vm| {
172172
Ok(confidential_vm.return_confidential_hart(self.hardware_hart))
173173
})
174174
// Below unwrap is safe because we are in the confidential flow that guarantees that the confidential VM with

security-monitor/src/core/control_data/confidential_vm.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)