Skip to content

Commit 43c33f3

Browse files
committed
add RtcStateSer
This provides serialization capabilities to RTCState, and is defined in the vm-superio-ser crate. RtcStateSer mirrors the state structure from vm-superio and derives the required (de)serialization/versioning traits (i.e. Serialize, Deserialize, Versionize). Signed-off-by: Laura Loghin <lauralg@amazon.com>
1 parent ead2280 commit 43c33f3

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 98.6,
2+
"coverage_score": 96.5,
33
"exclude_path": "",
44
"crate_features": ""
55
}

crates/vm-superio-ser/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
//! implement the `Serialize`, `Deserialize` and `Versionize` traits as well.
99
1010
#![deny(missing_docs)]
11+
12+
pub mod rtc_pl031;
13+
14+
pub use rtc_pl031::RtcStateSer;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
4+
5+
//! Provides a wrapper over an `RtcState` that has serialization capabilities.
6+
//!
7+
//! This module defines the `RtcStateSer` abstraction which mirrors the
8+
//! `RtcState` from the base crate, and adds on top of it derives for
9+
//! the `Serialize`, `Deserialize` and `Versionize` traits.
10+
11+
use serde::{Deserialize, Serialize};
12+
use versionize::{VersionMap, Versionize, VersionizeResult};
13+
use versionize_derive::Versionize;
14+
use vm_superio::RtcState;
15+
16+
/// Wrapper over an `RtcState` that has serialization capabilities.
17+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize, Versionize)]
18+
pub struct RtcStateSer {
19+
/// The load register.
20+
pub lr: u32,
21+
/// The offset applied to the counter to get the RTC value.
22+
pub offset: i64,
23+
/// The MR register.
24+
pub mr: u32,
25+
/// The interrupt mask.
26+
pub imsc: u32,
27+
/// The raw interrupt value.
28+
pub ris: u32,
29+
}
30+
31+
impl From<&RtcStateSer> for RtcState {
32+
fn from(state: &RtcStateSer) -> Self {
33+
RtcState {
34+
lr: state.lr,
35+
offset: state.offset,
36+
mr: state.mr,
37+
imsc: state.imsc,
38+
ris: state.ris,
39+
}
40+
}
41+
}
42+
43+
impl RtcStateSer {
44+
/// Creates a new `RtcStateSer` from a reference to an `RtcState` object.
45+
pub fn new(state: &RtcState) -> RtcStateSer {
46+
RtcStateSer {
47+
lr: state.lr,
48+
offset: state.offset,
49+
mr: state.mr,
50+
imsc: state.imsc,
51+
ris: state.ris,
52+
}
53+
}
54+
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
use vm_superio::rtc_pl031::NoEvents;
60+
use vm_superio::Rtc;
61+
62+
#[test]
63+
fn test_state_ser() {
64+
let mut rtc = Rtc::default();
65+
let mut data = [0; 4];
66+
67+
// Do some operations with the RTC.
68+
// Get the RTC value with a load register of 0 (the initial value).
69+
rtc.read(0x000, &mut data);
70+
71+
let data2 = [1; 4];
72+
// Write to LR register.
73+
rtc.write(0x008, &data2);
74+
75+
let state = rtc.state();
76+
let ser_state = RtcStateSer::new(&state);
77+
78+
let state_after_restore = RtcState::from(&ser_state);
79+
let mut rtc_after_restore = Rtc::from_state(&state_after_restore, NoEvents);
80+
81+
// Reading from the LR register should return the same value as before saving the state.
82+
rtc_after_restore.read(0x008, &mut data);
83+
assert_eq!(data, data2);
84+
85+
// Check that the old and the new state are identical when using the intermediate
86+
// `RtcStateSer` object as well.
87+
assert_eq!(state.lr, state_after_restore.lr);
88+
assert_eq!(state.ris, state_after_restore.ris);
89+
assert_eq!(state.imsc, state_after_restore.imsc);
90+
assert_eq!(state.offset, state_after_restore.offset);
91+
assert_eq!(state.mr, state_after_restore.mr);
92+
}
93+
}

crates/vm-superio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod rtc_pl031;
2222
pub mod serial;
2323

2424
pub use i8042::I8042Device;
25-
pub use rtc_pl031::Rtc;
25+
pub use rtc_pl031::{Rtc, RtcState};
2626
pub use serial::Serial;
2727

2828
use std::result::Result;

crates/vm-superio/src/rtc_pl031.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<EV: RtcEvents> Rtc<EV> {
203203

204204
// A struct implementing RtcEvents for tracking the occurrence of
205205
// significant events.
206-
events
206+
events,
207207
}
208208
}
209209

0 commit comments

Comments
 (0)