virtio-devices: Introduce new function translating GPAs into GVAs

In anticipation for the vDPA need to translate a GPA back into a GVA, we
extend the existing trait DmaRemapping and AccessPlatform to perform
such operation.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2022-04-04 14:11:29 +02:00
parent 059e787cb5
commit 1cdf8b232d
3 changed files with 35 additions and 2 deletions

View File

@@ -96,10 +96,13 @@ impl fmt::Display for VirtioDeviceType {
pub trait AccessPlatform: Send + Sync + Debug {
/// Provide a way to translate GVA address ranges into GPAs.
fn translate_gva(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>;
/// Provide a way to translate GPA address ranges into GVAs.
fn translate_gpa(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>;
}
pub trait Translatable {
fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self;
fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self;
}
impl Translatable for GuestAddress {
@@ -110,4 +113,11 @@ impl Translatable for GuestAddress {
*self
}
}
fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self {
if let Some(access_platform) = access_platform {
GuestAddress(access_platform.translate_gpa(self.0, len as u64).unwrap())
} else {
*self
}
}
}