|
1 | 1 | use iced_x86::{Decoder, DecoderOptions, Formatter as _, Instruction}; |
2 | | - |
| 2 | +use serde::{Deserialize, Serialize}; |
3 | 3 | use crate::emu::Emu; |
4 | 4 |
|
| 5 | +// about 10 mb should be on l3 cache |
| 6 | +// 8192 cache lines, |
| 7 | +// 32 instructions for each one, |
| 8 | +// 40 for the struct (I think we can make it smaller) |
| 9 | +const INSTRUCTION_ARRAY_SIZE: usize = 8192 * 32; |
| 10 | + |
| 11 | +// we want the cache size to be store in L1 cache which is lower than 40kb |
| 12 | +const CACHE_SIZE: usize = 2048 * 16; |
| 13 | +const CACHE_MASK: usize = CACHE_SIZE - 1; // Assumes power of 2 |
| 14 | +const MAX_CACHE_PER_LINE: usize = 16; |
| 15 | +pub const INVALID_LPF_ADDR: u64 = 0xffffffffffffffff; |
| 16 | + |
| 17 | +pub fn LPF_OF(addr: u64) -> u64 { |
| 18 | + // Implementation of LPF_OF macro/function |
| 19 | + addr & 0xfffffffffffff000 |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Clone, Serialize, Deserialize)] |
| 23 | +struct CachedInstruction { |
| 24 | + pub lpf: u64, |
| 25 | + pub instruction_key : usize, |
| 26 | + pub instruction_len: usize, |
| 27 | +} |
| 28 | + |
| 29 | +impl Default for CachedInstruction { |
| 30 | + fn default() -> Self { |
| 31 | + CachedInstruction { |
| 32 | + lpf: INVALID_LPF_ADDR, |
| 33 | + instruction_key: 0x0, |
| 34 | + instruction_len: 0x0, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl CachedInstruction { |
| 40 | + pub fn is_valid(&self) -> bool { |
| 41 | + self.lpf == INVALID_LPF_ADDR |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Clone, Serialize, Deserialize)] |
| 46 | +pub struct InstructionCache { |
| 47 | + cache_entries: Vec<CachedInstruction>, |
| 48 | + instructions: Vec<Instruction>, |
| 49 | + next_instruction_slot: usize, |
| 50 | + pub current_instruction_slot: usize, |
| 51 | + current_decode_len: usize, |
| 52 | + current_decode_idx: usize |
| 53 | + // probe_stats: ProbeStats, |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Clone, Serialize, Deserialize, Default)] |
| 57 | +struct ProbeStats { |
| 58 | + hits: usize, |
| 59 | + misses: usize, |
| 60 | + collisions: usize, |
| 61 | +} |
| 62 | + |
| 63 | +impl InstructionCache { |
| 64 | + pub fn new() -> Self { |
| 65 | + let mut cache = InstructionCache { |
| 66 | + cache_entries: vec![CachedInstruction::default(); CACHE_SIZE], |
| 67 | + instructions: vec![Instruction::default(); INSTRUCTION_ARRAY_SIZE], |
| 68 | + next_instruction_slot: 0, |
| 69 | + current_decode_len: 0, |
| 70 | + current_instruction_slot: 0, |
| 71 | + current_decode_idx: 0, |
| 72 | + // probe_stats: ProbeStats::default(), |
| 73 | + }; |
| 74 | + |
| 75 | + // Initialize all instructions to default state |
| 76 | + for inst in &mut cache.instructions { |
| 77 | + *inst = Instruction::default(); |
| 78 | + } |
| 79 | + |
| 80 | + cache |
| 81 | + } |
| 82 | + |
| 83 | + #[inline(always)] |
| 84 | + pub fn get_index_of(&self, lpf: u64, len: u64) -> usize { |
| 85 | + const TLB_MASK: u32 = ((CACHE_SIZE - 1) << 12) as u32; |
| 86 | + (((lpf + len) & (TLB_MASK as u64)) >> 12) as usize |
| 87 | + } |
| 88 | + |
| 89 | + #[inline] |
| 90 | + fn flush_cache_line(&mut self, idx: usize) { |
| 91 | + for i in 0..MAX_CACHE_PER_LINE { |
| 92 | + self.cache_entries[idx].lpf = INVALID_LPF_ADDR; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + pub fn lookup_entry(&mut self, addr: u64, len: u64) -> bool { |
| 97 | + let lpf = crate::maps::tlb::LPF_OF(addr); |
| 98 | + let idx = self.get_index_of(lpf, len); |
| 99 | + |
| 100 | + // do a linear probing for each cache line |
| 101 | + for i in 0..MAX_CACHE_PER_LINE { |
| 102 | + if self.cache_entries[idx+i].lpf == INVALID_LPF_ADDR { |
| 103 | + return false; |
| 104 | + } |
| 105 | + // found the instruction now do initialization and return true |
| 106 | + if self.cache_entries[idx+i].lpf == addr { |
| 107 | + let key = self.cache_entries[idx+i].instruction_key; |
| 108 | + self.current_instruction_slot = key; |
| 109 | + self.current_decode_len = self.cache_entries[idx+i].instruction_len; |
| 110 | + self.current_decode_idx = 0; |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // the cache_line is full now we flush all the cache line |
| 116 | + self.flush_cache_line(idx); |
| 117 | + true |
| 118 | + } |
| 119 | + |
| 120 | + #[inline(always)] |
| 121 | + fn flush_cache(&mut self) { |
| 122 | + self.cache_entries = vec![CachedInstruction::default(); CACHE_SIZE]; |
| 123 | + self.instructions = vec![Instruction::default(); INSTRUCTION_ARRAY_SIZE]; |
| 124 | + self.next_instruction_slot = 0; |
| 125 | + } |
| 126 | + |
| 127 | + pub fn insert_from_decoder(&mut self, decoder: &mut Decoder, addition: usize, rip_addr: u64) { |
| 128 | + let lpf = crate::maps::tlb::LPF_OF(rip_addr); |
| 129 | + let idx = self.get_index_of(lpf, 0); |
| 130 | + |
| 131 | + // copy the instruction to the slot |
| 132 | + // now the case when instruction slot is full, instead of complex algorithm |
| 133 | + // we just fudge everything and rebuild from scratch can be a better way |
| 134 | + // but I think this is simple and good enough |
| 135 | + let slot = self.next_instruction_slot; |
| 136 | + |
| 137 | + if self.next_instruction_slot >= INSTRUCTION_ARRAY_SIZE { |
| 138 | + self.flush_cache(); |
| 139 | + } |
| 140 | + let mut count: usize = 0; |
| 141 | + while decoder.can_decode() && decoder.position() + addition <= decoder.max_position() { |
| 142 | + decoder.decode_out(&mut self.instructions[slot+count]); |
| 143 | + count += 1; |
| 144 | + } |
| 145 | + self.next_instruction_slot += count; |
| 146 | + |
| 147 | + // insert to the cache |
| 148 | + for i in 0..MAX_CACHE_PER_LINE { |
| 149 | + if self.cache_entries[idx+i].lpf == INVALID_LPF_ADDR { |
| 150 | + self.cache_entries[idx+i].instruction_key = slot; |
| 151 | + self.cache_entries[idx+i].lpf = rip_addr; |
| 152 | + self.cache_entries[idx+i].instruction_len = count; |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + assert!(self.lookup_entry(rip_addr, 0), "Cache Insertion FAILED: There is support to be entry after insertion using insert_from_decoder"); |
| 158 | + } |
| 159 | + |
| 160 | + pub fn insert_instruction(&mut self, addr: u64, instrs: Vec<Instruction>) { |
| 161 | + let lpf = crate::maps::tlb::LPF_OF(addr); |
| 162 | + let idx = self.get_index_of(lpf, 0); |
| 163 | + |
| 164 | + // copy the instruction to the slot |
| 165 | + // now the case when instruction slot is full, instead of complex algorithm |
| 166 | + // we just fudge everything and rebuild from scratch can be a better way |
| 167 | + // but I think this is simple and good enough |
| 168 | + let slot = self.next_instruction_slot; |
| 169 | + self.next_instruction_slot += instrs.len(); |
| 170 | + if self.next_instruction_slot >= INSTRUCTION_ARRAY_SIZE { |
| 171 | + self.flush_cache(); |
| 172 | + } |
| 173 | + |
| 174 | + for i in 0..instrs.len() { |
| 175 | + self.instructions[slot+i] = instrs[i]; |
| 176 | + } |
| 177 | + |
| 178 | + // insert to the cache |
| 179 | + for i in 0..MAX_CACHE_PER_LINE { |
| 180 | + if self.cache_entries[idx+i].lpf == INVALID_LPF_ADDR { |
| 181 | + self.cache_entries[idx+i].instruction_key = slot; |
| 182 | + self.cache_entries[idx+i].lpf = addr; |
| 183 | + self.cache_entries[idx+i].instruction_len = instrs.len(); |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + pub fn decode_out(&mut self, instruction: &mut Instruction) { |
| 190 | + *instruction = self.instructions[self.current_instruction_slot + self.current_decode_idx]; |
| 191 | + self.current_decode_idx += 1; |
| 192 | + } |
| 193 | + |
| 194 | + pub fn can_decode(&self) -> bool { |
| 195 | + self.current_decode_idx < self.current_decode_len |
| 196 | + } |
| 197 | +} |
| 198 | + |
5 | 199 | impl Emu { |
6 | 200 | /// Disassemble an amount of instruccions on an specified address. |
7 | 201 | /// This not used on the emulation engine, just from console, |
|
0 commit comments