Determine slice using msr instead of reversed hashing fn

This commit is contained in:
augustin64 2024-06-27 11:09:06 +02:00
parent 087997ce5c
commit 989ae0c0ea
5 changed files with 30 additions and 10 deletions

1
Cargo.lock generated
View File

@ -126,6 +126,7 @@ version = "0.1.0"
dependencies = [
"atomic",
"bitvec",
"cache_slice",
"cpuid",
"hashbrown",
"itertools 0.12.1",

View File

@ -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)
}
}
}

View File

@ -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<u16>) -> Option<usize> {
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::*;

View File

@ -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 }

View File

@ -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<I: Iterator<Item = (usize, usize)>, 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);