2020-09-22 14:36:07 +02:00
|
|
|
use crate::{CacheStatus, ChannelFatalError, SideChannelError, SingleAddrCacheSideChannel};
|
|
|
|
use cache_utils::calibration::only_reload;
|
|
|
|
use cache_utils::flush;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NaiveFlushAndReload {
|
|
|
|
pub threshold: u64,
|
|
|
|
current: Option<*const u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NaiveFlushAndReload {
|
|
|
|
pub fn from_threshold(threshold: u64) -> Self {
|
|
|
|
NaiveFlushAndReload {
|
|
|
|
threshold,
|
|
|
|
current: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SingleAddrCacheSideChannel for NaiveFlushAndReload {
|
2020-09-22 17:09:46 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// addr needs to be a valid pointer
|
2020-09-22 16:49:22 +02:00
|
|
|
unsafe fn test_single(&mut self, addr: *const u8) -> Result<CacheStatus, SideChannelError> {
|
2020-09-22 14:36:07 +02:00
|
|
|
if self.current != Some(addr) {
|
2020-09-22 16:49:22 +02:00
|
|
|
return Err(SideChannelError::AddressNotReady(addr));
|
2020-09-22 14:36:07 +02:00
|
|
|
}
|
|
|
|
let t = unsafe { only_reload(addr) };
|
|
|
|
if t > self.threshold {
|
|
|
|
Ok(CacheStatus::Miss)
|
|
|
|
} else {
|
|
|
|
Ok(CacheStatus::Hit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:09:46 +02:00
|
|
|
/// # Safety:
|
|
|
|
///
|
|
|
|
/// addr needs to be a valid pointer
|
2020-09-22 16:49:22 +02:00
|
|
|
unsafe fn prepare_single(&mut self, addr: *const u8) -> Result<(), SideChannelError> {
|
2020-09-22 14:36:07 +02:00
|
|
|
unsafe { flush(addr) };
|
|
|
|
self.current = Some(addr);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn victim_single(&mut self, operation: &dyn Fn()) {
|
|
|
|
operation()
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:09:46 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// addr needs to be a valid pointer
|
2020-09-22 16:49:22 +02:00
|
|
|
unsafe fn calibrate_single(
|
2020-09-22 14:36:07 +02:00
|
|
|
&mut self,
|
|
|
|
_addresses: impl IntoIterator<Item = *const u8>,
|
|
|
|
) -> Result<(), ChannelFatalError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|