1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
use std::collections::HashSet;
use rustv::isa::{self, IsaType};
use rustv::memory::{MemoryError, MemoryInterface, Result, SharedMemory};
pub const WRITE_TRAP_VALUE: isa::Byte = isa::Byte(0xE0);
pub const AREA_TRAP_VALUE: isa::Byte = isa::Byte(0xE4);
pub const WRITE_TRAP_STALL: u32 = 1_000_000;
/// A cache that can be used as two separate caches or one
/// set-associative cache.
pub struct ShareableCache<'a> {
core_id: usize,
primary: SharedMemory<'a>,
secondary: SharedMemory<'a>,
secondary_enabled: bool,
use_secondary: bool,
traps_hit: HashSet<isa::Address>,
}
// Cache snooping: update all cache lines when a write is made
macro_rules! snoop {
($cache: expr, $write_value: ident, $address: ident, $value: ident) => {
if $cache.borrow().is_address_accessible($address) {
// depends on invariant: write_word completes instantly
// when the address is accessible (in-cache)
let _ = $cache.borrow_mut().$write_value($address, $value);
}
}
}
macro_rules! check_traps {
($core_id: expr, $cache: expr, $traps_hit: expr, $write_value: ident,
$address: ident, $value: ident) => {{
// Requires invariant: if x is the address of a word, x + 0 is
// the address of the LSB and x + 3 is the address of the MSB.
let accessible = {
$cache.borrow().is_address_accessible($address)
};
if accessible {
// No stall - check for trap
let old_value = {
$cache.borrow_mut().read_word($address)
};
match old_value {
Ok(old_value) => {
let old_bytes = old_value.as_bytes();
let new_bytes = $value.as_bytes();
let mut num_traps_hit = 0;
let iter = old_bytes.iter()
// Skip {offset} bytes
.skip(($address.0 & 0x3) as usize)
.take(new_bytes.len())
.enumerate();
for (offset, old) in iter {
// Make sure offset bits are cleared before
// adding offset
let trap_address = ($address & 0xFFFFFFFC) +
isa::Word(offset as u32);
if *old == WRITE_TRAP_VALUE &&
!$traps_hit.contains(&trap_address) {
num_traps_hit += 1;
$traps_hit.insert(trap_address);
}
else if *old != WRITE_TRAP_VALUE {
$traps_hit.remove(&trap_address);
}
}
if num_traps_hit > 0 {
println!("Checking for traps: {:?} vs {:?}", old_bytes, new_bytes);
info!("[memory] core {}: {} write trap(s) hit at address {:x},\
stalling for 1_000_000 cycles each",
$core_id, num_traps_hit, $address);
Err(MemoryError::CacheMiss {
stall_cycles: num_traps_hit * WRITE_TRAP_STALL,
})
}
else {
$cache.borrow_mut().$write_value($address, $value)
}
}
Err(e) => {
panic!("Could not read accessible value: {:?}", e)
}
}
}
else {
// Not in cache - defer to fetch
$cache.borrow_mut().$write_value($address, $value)
}
}}
}
macro_rules! write_value {
($self_: ident, $write_value: ident, $address: ident, $value: ident) => {
if $self_.secondary_enabled {
let (primary_accessible, secondary_accessible) =
$self_.address_accessible($address);
if primary_accessible {
snoop!($self_.secondary, $write_value, $address, $value);
check_traps!($self_.core_id, $self_.primary,
&mut $self_.traps_hit,
$write_value, $address, $value)
}
else if secondary_accessible {
snoop!($self_.primary, $write_value, $address, $value);
check_traps!($self_.core_id, $self_.secondary,
&mut $self_.traps_hit,
$write_value, $address, $value)
}
else {
$self_.use_secondary = !$self_.use_secondary;
if $self_.use_secondary {
snoop!($self_.primary, $write_value, $address, $value);
check_traps!($self_.core_id, $self_.secondary,
&mut $self_.traps_hit,
$write_value, $address, $value)
}
else {
snoop!($self_.secondary, $write_value, $address, $value);
check_traps!($self_.core_id, $self_.primary,
&mut $self_.traps_hit,
$write_value, $address, $value)
}
}
}
else {
snoop!($self_.secondary, $write_value, $address, $value);
check_traps!($self_.core_id, $self_.primary,
&mut $self_.traps_hit,
$write_value, $address, $value)
}
}
}
impl<'a> ShareableCache<'a> {
pub fn new(core_id: usize,
cache1: SharedMemory<'a>, cache2: SharedMemory<'a>)
-> ShareableCache<'a> {
ShareableCache {
core_id: core_id,
primary: cache1.clone(),
secondary: cache2.clone(),
secondary_enabled: false,
use_secondary: false,
traps_hit: HashSet::new(),
}
}
pub fn enable_secondary(&mut self) {
self.secondary_enabled = true;
self.use_secondary = true;
}
pub fn disable_secondary(&mut self) {
self.secondary_enabled = false;
}
fn address_accessible(&self, address: isa::Address) -> (bool, bool) {
// Use scopes to make sure these borrows end before the
// branches of the if statement begin
let primary_accessible = {
self.primary.borrow().is_address_accessible(address)
};
let secondary_accessible = {
self.secondary.borrow().is_address_accessible(address)
};
(primary_accessible, secondary_accessible)
}
}
impl<'a> MemoryInterface for ShareableCache<'a> {
fn latency(&self) -> u32 {
self.primary.borrow().latency()
}
fn step(&mut self) {
// We only step the primary cache. The idea is that the
// secondary cache should be the primary cache of another
// ShareableCache.
self.primary.borrow_mut().step();
}
fn is_address_accessible(&self, address: isa::Address) -> bool {
self.primary.borrow().is_address_accessible(address) ||
(self.secondary_enabled &&
self.secondary.borrow().is_address_accessible(address))
}
fn read_word(&mut self, address: isa::Address) -> Result<isa::Word> {
// TODO: disallow access to high or low memory unless
// secondary cache is enabled. Remember: addresses are already
// translated
// TODO: is CacheRacer physically or virtually addressed?
if self.secondary_enabled {
let (primary_accessible, secondary_accessible) =
self.address_accessible(address);
if primary_accessible {
self.primary.borrow_mut().read_word(address)
}
else if secondary_accessible {
self.secondary.borrow_mut().read_word(address)
}
else {
self.use_secondary = !self.use_secondary;
if self.use_secondary {
self.secondary.borrow_mut().read_word(address)
}
else {
self.primary.borrow_mut().read_word(address)
}
}
}
else {
self.primary.borrow_mut().read_word(address)
}
}
fn write_word(&mut self, address: isa::Address, value: isa::Word) -> Result<()> {
write_value!(self, write_word, address, value)
}
fn write_halfword(&mut self, address: isa::Address, value: isa::HalfWord) -> Result<()> {
write_value!(self, write_halfword, address, value)
}
fn write_byte(&mut self, address: isa::Address, value: isa::Byte) -> Result<()> {
write_value!(self, write_byte, address, value)
}
}
|