aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2021-05-30 01:59:03 +0200
committerjohannst <johannes.stoelp@gmail.com>2021-05-30 01:59:03 +0200
commit85d55ca8f2539a5f766aa375b011163832d5a592 (patch)
treeb7bc5a12470f73d3f9f4737c51ad0c817b2cb16e
parent795a5d7ecdbc1999b7cf7eabf5251a4e3cd192ce (diff)
downloadmini-kvm-rs-85d55ca8f2539a5f766aa375b011163832d5a592.tar.gz
mini-kvm-rs-85d55ca8f2539a5f766aa375b011163832d5a592.zip
usermem: add load fn
-rw-r--r--src/lib.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6793272..83532c2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -63,7 +63,7 @@ impl UserMem {
}
/// Allocate a zero-initialized memory region of `len` bytes and initialize the first bytes
- /// with `init_len`.
+ /// with `init_from`.
///
/// # Panics
///
@@ -72,9 +72,21 @@ impl UserMem {
assert!(len >= init_from.len());
let mut m = UserMem::new(len)?;
- m.as_mut()[..init_from.len()].copy_from_slice(init_from);
+ m.load(PhysAddr(0), init_from);
Ok(m)
}
+
+ /// Load the bytes stored in `data` into memory at physical address `addr`.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `addr + data.len` is larger than the memory size `len`.
+ pub fn load(&mut self, addr: PhysAddr, data: &[u8]) {
+ assert!(self.len >= addr.0 as usize + data.len());
+
+ let addr = addr.0 as usize;
+ self.as_mut()[addr..addr + data.len()].copy_from_slice(data);
+ }
}
impl ops::Drop for UserMem {