From b3509129c1c066c384399ea96a2e74c747e4be79 Mon Sep 17 00:00:00 2001 From: Guillume DIDIER Date: Tue, 19 Oct 2021 15:17:06 +0200 Subject: [PATCH] Add lazy static and start initializing global state of the WX allocator --- prefetcher_reverse/Cargo.toml | 1 + prefetcher_reverse/src/ip_tool.rs | 58 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/prefetcher_reverse/Cargo.toml b/prefetcher_reverse/Cargo.toml index f1dac60..c0a5528 100644 --- a/prefetcher_reverse/Cargo.toml +++ b/prefetcher_reverse/Cargo.toml @@ -14,3 +14,4 @@ flush_reload = { path = "../flush_reload" } basic_timing_cache_channel = { path = "../basic_timing_cache_channel" } nix = "0.20.0" rand = "0.8.3" +lazy_static = "1.4.0" diff --git a/prefetcher_reverse/src/ip_tool.rs b/prefetcher_reverse/src/ip_tool.rs index 9fb5d8d..d8c384a 100644 --- a/prefetcher_reverse/src/ip_tool.rs +++ b/prefetcher_reverse/src/ip_tool.rs @@ -1,18 +1,68 @@ +use cache_utils::mmap::MMappedMemory; +use lazy_static::lazy_static; +use std::collections::LinkedList; +use std::sync::Mutex; + +struct WXRange { + bitmap: Vec, // fixme bit vector + pages: Vec>, +} + +struct WXAllocator { + ranges: LinkedList, + // Possible improvement : a dedicated data structure, with optimised lookup of which range + // contains the right address, plus reasonably easy ability to merge nodes +} + +impl WXAllocator { + fn new() -> Self { + WXAllocator { + ranges: LinkedList::::new(), + } + } +} + +pub struct FunctionTemplate { + start: unsafe extern "C" fn(*const u8) -> u64, + ip: *const u8, + end: *const u8, +} +lazy_static! { + static ref wx_allocator: Mutex = Mutex::new(WXAllocator::new()); +} +const TIMED_MACCESS: FunctionTemplate = FunctionTemplate { + start: timed_maccess_template, + ip: timed_maccess_template_ip as *const u8, + end: timed_maccess_template_end as *const u8, +}; + +const TIMED_CLFLUSH: FunctionTemplate = FunctionTemplate { + start: timed_clflush_template, + ip: timed_clflush_template_ip as *const u8, + end: timed_clflush_template_end as *const u8, +}; + global_asm!( ".global timed_maccess_template", "timed_maccess_template:", "mfence", + "lfence", "rdtsc", "shl rdx, 32", "mov rsi, rdx", "add rsi, rax", "mfence", + "lfence", + ".global timed_maccess_template_ip", + "timed_maccess_template_ip:", "mov rdi, [rdi]", "mfence", + "lfence", "rdtsc", "shl rdx, 32", "add rax, rdx", "mfence", + "lfence", "sub rax, rsi", "ret", ".global timed_maccess_template_end", @@ -21,17 +71,23 @@ global_asm!( ".global timed_clflush_template", "timed_clflush_template:", "mfence", + "lfence", "rdtsc", "shl rdx, 32", "mov rsi, rdx", "add rsi, rax", "mfence", + "lfence", + ".global timed_clflush_template_ip", + "timed_clflush_template_ip:", "clflush [rdi]", "mfence", + "lfence", "rdtsc", "shl rdx, 32", "add rax, rdx", "mfence", + "lfence", "sub rax, rsi", "ret", ".global timed_clflush_template_end", @@ -41,8 +97,10 @@ global_asm!( extern "C" { fn timed_maccess_template(pointer: *const u8) -> u64; + fn timed_maccess_template_ip(); fn timed_maccess_template_end(); fn timed_clflush_template(pointer: *const u8) -> u64; + fn timed_clflush_template_ip(); fn timed_clflush_template_end(); }