diff --git a/Cargo.lock b/Cargo.lock index 14f27a7..2f63c5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,6 +126,7 @@ version = "0.1.0" dependencies = [ "atomic", "bitvec", + "cache_slice", "cpuid", "hashbrown", "itertools 0.12.1", diff --git a/cache_slice/src/bin/scan.rs b/cache_slice/src/bin/scan.rs index 1c00017..edbd70c 100644 --- a/cache_slice/src/bin/scan.rs +++ b/cache_slice/src/bin/scan.rs @@ -1,4 +1,4 @@ -use cache_slice::monitor_address; +use cache_slice::determine_slice; use cache_slice::utils::core_per_package; use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet}; use nix::unistd::Pid; @@ -24,10 +24,9 @@ pub fn main() { cpu_set.set(core).unwrap(); sched_setaffinity(Pid::this(), &cpu_set).unwrap(); for addr in target.iter() { - let res = unsafe { monitor_address(addr as *const u64 as *const u8, core as u8, nb_cores) }.unwrap(); - let slice = res.iter().enumerate().max_by_key(|(_i, val)| { **val }); + let slice = determine_slice(addr, nb_cores, Some(core)); match slice { - Some((slice, _)) => { + Some(slice) => { println!("({:2}) Slice for addr {:x}: {}", core, addr as *const u64 as usize, slice) } None => { @@ -36,14 +35,13 @@ pub fn main() { } } for addr in target.iter() { - let res = unsafe { monitor_address(addr as *const u64 as *const u8, 0, nb_cores) }.unwrap(); - let slice = res.iter().enumerate().max_by_key(|(_i, val)| { **val }); + let slice = determine_slice(addr, nb_cores, Some(0)); match slice { - Some((slice, _)) => { - println!("({:2}) Slice for addr {:x}: {}", 0, addr as *const u64 as usize, slice) + Some(slice) => { + println!("({:2}) Slice for addr {:x}: {}", core, addr as *const u64 as usize, slice) } None => { - eprintln!("({:2}) Failed to find slice for addr {:x}", 0, addr as *const u64 as usize) + eprintln!("({:2}) Failed to find slice for addr {:x}", core, addr as *const u64 as usize) } } } diff --git a/cache_slice/src/lib.rs b/cache_slice/src/lib.rs index d2df003..e59c323 100644 --- a/cache_slice/src/lib.rs +++ b/cache_slice/src/lib.rs @@ -125,6 +125,22 @@ pub unsafe fn monitor_address(addr: *const u8, cpu: u8, max_cbox: u16) -> Result } } +pub fn determine_slice(addr: *const u8, nb_cores: u16, core: Option) -> Option { + let res = unsafe { + monitor_address(addr as *const u64 as *const u8, core.unwrap_or(0) as u8, nb_cores) + }.unwrap(); + + let slice = res.iter().enumerate().max_by_key(|(_i, val)| { **val }); + + let maxi = res.iter().max().unwrap(); + let slice = if *maxi == 0 { None } else { slice }; + + match slice { + Some((slice, _)) => { Some(slice) } + None => { None } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/cache_utils/Cargo.toml b/cache_utils/Cargo.toml index a93fca7..5cc270e 100644 --- a/cache_utils/Cargo.toml +++ b/cache_utils/Cargo.toml @@ -20,6 +20,7 @@ nix = { version = "0.29.0", optional = true, features = ["process", "mman", "sch libc = { version = "0.2.153", optional = true } hashbrown = { version = "0.11.2", optional = true } turn_lock = { path = "../turn_lock", optional = true } +cache_slice = { path = "../cache_slice" } lazy_static = "1.4.0" bitvec = { version = "0.22.3", optional = true } diff --git a/cache_utils/src/calibrate_2t.rs b/cache_utils/src/calibrate_2t.rs index f003cbc..16e7164 100644 --- a/cache_utils/src/calibrate_2t.rs +++ b/cache_utils/src/calibrate_2t.rs @@ -7,6 +7,8 @@ use crate::complex_addressing::CacheAttackSlicing; use core::arch::x86_64 as arch_x86; use itertools::Itertools; use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet}; +use cache_slice::utils::core_per_package; +use cache_slice::determine_slice; use nix::unistd::Pid; use nix::Error; use std::cmp::min; @@ -210,12 +212,14 @@ fn calibrate_fixed_freq_2_thread_impl, T>( Some(ref ima) => Box::new(ima.values().copied()), None => Box::new((0..len as isize).step_by(cache_line_length)), };*/ + let nb_cores = core_per_package(); for i in offsets { let pointer = unsafe { p.offset(i) }; params.address = pointer; - let hash = slicing.hash(pointer as usize); + //let hash = slicing.hash(pointer as usize); + let hash = determine_slice(pointer, nb_cores, None).unwrap(); if options.verbosity >= Thresholds { print!("Calibration for {:p}", pointer);