Skip to content

Commit

Permalink
vstate: Set memory attributes of regions
Browse files Browse the repository at this point in the history
Co-developed-by: Jake Correnti <[email protected]>
Signed-off-by: Tyler Fanelli <[email protected]>
  • Loading branch information
tylerfanelli committed Dec 17, 2024
1 parent 535914f commit 28f070d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/vmm/src/linux/vstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ use kvm_bindings::{
};

#[cfg(feature = "amd-sev")]
use kvm_bindings::{kvm_create_guest_memfd, kvm_userspace_memory_region2, KVM_MEM_GUEST_MEMFD};
use kvm_bindings::{
kvm_create_guest_memfd, kvm_memory_attributes, kvm_userspace_memory_region2,
KVM_MEM_GUEST_MEMFD,
};

use kvm_bindings::KVM_API_VERSION;
use kvm_ioctls::*;
Expand Down Expand Up @@ -119,6 +122,9 @@ pub enum Error {
/// Error creating memory map for SHM region.
ShmMmap(io::Error),
#[cfg(feature = "amd-sev")]
/// Error setting KVM memory attributes of a region.
SetMemoryAttributes(kvm_ioctls::Error),
#[cfg(feature = "amd-sev")]
/// Error initializing the Secure Virtualization Backend (SEV).
SevSecVirtInit(SevError),
#[cfg(feature = "amd-sev")]
Expand Down Expand Up @@ -278,6 +284,10 @@ impl Display for Error {
),
SetUserMemoryRegion(e) => write!(f, "Cannot set the memory regions: {e}"),
ShmMmap(e) => write!(f, "Error creating memory map for SHM region: {e}"),
#[cfg(feature = "amd-sev")]
SetMemoryAttributes(e) => {
write!(f, "Error setting KVM memory attributes of a region: {e}")
}
#[cfg(feature = "tee")]
SevSecVirtInit(e) => {
write!(
Expand Down Expand Up @@ -624,6 +634,18 @@ impl Vm {
self.fd.set_user_memory_region2(memory_region).unwrap();
}

let attr = kvm_memory_attributes {
address: region.start_addr().raw_value(),
size: region.len(),
// KVM_MEMORY_ATTRIBUTE_PRIVATE,
attributes: 1 << 3,
flags: 0,
};

self.fd
.set_memory_attributes(attr)
.map_err(Error::SetMemoryAttributes)?;

self.next_mem_slot += 1;
}

Expand Down

0 comments on commit 28f070d

Please sign in to comment.