-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpersistent.rs
123 lines (105 loc) · 3.55 KB
/
persistent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! The persistent memory component offers a simple storage that remains after reboot.
//! See [`PersistentMem`] for more details.
use lrvm::board::Bus;
use lrvm_tools::metadata::{DeviceMetadata, StorageType};
/// The flash memory component contains a writable, persistent storage that does not reset with the motherboard.
/// It is though reset when the VM is destroyed.
pub struct PersistentMem {
storage: Vec<u32>,
size: u32,
hw_id: u64,
}
impl PersistentMem {
/// Create a new flash memory component
/// Returns an error message if the capacity is 0, not a multiple or 4 bytes or too large for the running CPU architecture.
pub fn new(size: u32, hw_id: u64) -> Result<Self, &'static str> {
if size == 0 {
Err("Flash memory's size cannot be 0")
} else if size % 4 != 0 {
Err("Flash memory's size must be a multiple of 4 bytes")
} else {
Ok(Self {
storage: vec![
0;
size.try_into().map_err(|_| {
"Flash memory size cannot exceed your CPU architecture's supported size"
})?
],
size: size / 4,
hw_id,
})
}
}
/// Create a new flash memory component from the provided storage
/// Returns an error message if the capacity is too large for the running CPU architecture.
pub fn from(storage: Vec<u32>, hw_id: u64) -> Result<Self, &'static str> {
let size: u32 = storage
.len()
.try_into()
.map_err(|_| "Flash memory's length cannot be larger than 2^32 words")?;
Ok(Self {
storage,
size: size / 4,
hw_id,
})
}
/// Create a new flash memory component from the provided storage and with a larger size than its storage
/// Returns an error message in case of fail
pub fn from_with_size(
mut storage: Vec<u32>,
size: u32,
hw_id: u64,
) -> Result<Self, &'static str> {
let _: u32 = storage
.len()
.try_into()
.map_err(|_| "Flash memory's length cannot be larger than 2^32 words")?;
let _: usize = size.try_into().map_err(|_| {
"Flash memory size cannot exceed your CPU architecture's supported size"
})?;
if storage.len() > size as usize {
return Err("Flash memory's size cannot be lower than its initial storage's size");
}
if size == 0 {
return Err("Flash memory's size cannot be 0");
}
if size % 4 != 0 {
return Err("Flash memory's size must be a multiple of 4 bytes");
}
let size = size / 4;
storage.resize(size as usize, 0);
Ok(Self {
storage,
size,
hw_id,
})
}
/// Get the flash memory's size
pub fn size(&self) -> u32 {
self.size
}
}
impl Bus for PersistentMem {
fn name(&self) -> &'static str {
"Flash Memory"
}
fn metadata(&self) -> [u32; 8] {
DeviceMetadata::new(
self.hw_id,
self.size * 4,
StorageType::Flash.into(),
None,
None,
)
.encode()
}
fn read(&mut self, addr: u32, _ex: &mut u16) -> u32 {
self.storage[addr as usize / 4]
}
fn write(&mut self, addr: u32, word: u32, _ex: &mut u16) {
self.storage[addr as usize / 4] = word;
}
fn reset(&mut self) {
self.storage = vec![0; self.storage.len()];
}
}