-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbb.rs
196 lines (165 loc) · 6.35 KB
/
tbb.rs
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
use perfect::*;
use perfect::events::*;
use perfect::stats::*;
use rand::prelude::*;
use perfect::asm::Emitter;
fn main() {
let mut harness = HarnessConfig::default_zen2().emit();
TbbCapacitySpec::run(&mut harness);
}
/// Measure the capacity of the "taken branch buffer".
///
/// Context
/// =======
///
/// The function of the "taken branch buffer" is completely undocumented and
/// not mentioned in any AMD documentation, apart from the description of the
/// PMC event ([`DeDisDispatchTokenStalls0Mask::TakenBrnchBufferRsrc`]).
///
/// Test
/// ====
///
/// 1. Speculatively dispatch `N` always-taken control flow instructions
/// 2. If we measure stall cycles, `N` must be the number of available entries
/// in the taken branch buffer?
///
/// Results
/// =======
///
/// We always measure stall cycles after 31 taken control-flow instructions.
/// Stall cycles never occur for never-taken control-flow instructions.
///
/// If we assume that the capacity is actually 32 entries, this is some
/// evidence that the taken branch buffer tracks taken control-flow
/// instructions that have been speculatively completed but not-yet-retired.
/// Since the RET in our gadget is also pending, we would expect to only
/// measure the capacity at 31.
///
pub struct TbbCapacitySpec;
impl MispredictedReturnTemplate<usize> for TbbCapacitySpec {}
impl TbbCapacitySpec {
const CASES: StaticEmitterCases<usize> = StaticEmitterCases::new(&[
EmitterDesc { desc: "jmp 0x0",
func: |f, input| {
for _ in 0..=input {
dynasm!(f ; jmp >next ; next:)
}
}},
EmitterDesc { desc: "lea rdi, [>next]; jmp rdi",
func: |f, input| {
for _ in 0..=input {
dynasm!(f
; lea rdi, [>next]
; jmp rdi
; next:
)
}
}},
EmitterDesc { desc: "call 0x0",
func: |f, input| {
for _ in 0..=input {
dynasm!(f ; call >next ; next:)
}
}},
EmitterDesc { desc: "jz 0x0 (always-taken)",
func: |f, input| {
for _ in 0..=input {
dynasm!(f ; jz >next ; next:)
}
}},
EmitterDesc { desc: "jnz 0x0 (never-taken)",
func: |f, input| {
for _ in 0..=input {
dynasm!(f ; jnz >next ; next:)
}
}},
]);
fn run(harness: &mut PerfectHarness) {
let mut events = EventSet::new();
events.add(Zen2Event::DeDisDispatchTokenStalls1(
DeDisDispatchTokenStalls1Mask::TakenBrnchBufferRsrc
));
//events.add(Zen2Event::LsPrefInstrDisp(0x01));
let opts = MispredictedReturnOptions::zen2_defaults()
.post_prologue_fn(Some(|f, input| {
dynasm!(f
; cmp rcx, 0
);
}))
.speculative_epilogue_fn(Some(|f, input| {
dynasm!(f ; prefetch [rax])
}))
.rdpmc_strat(RdpmcStrategy::Gpr(Gpr::R15));
let mut exp_results = ExperimentResults::new();
for testcase in Self::CASES.iter() {
println!("[*] Testcase '{}'", testcase.desc);
let mut case_res = ExperimentCaseResults::new(testcase.desc);
for input in 0..=256 {
let asm = Self::emit(opts, input, testcase.func);
let asm_reader = asm.reader();
let asm_tgt_buf = asm_reader.lock();
let asm_tgt_ptr = asm_tgt_buf.ptr(AssemblyOffset(0));
let asm_fn: MeasuredFn = unsafe {
std::mem::transmute(asm_tgt_ptr)
};
for event in events.iter() {
let desc = event.as_desc();
let results = harness.measure(asm_fn,
desc.id(), desc.mask(), 256, InputMethod::Fixed(0, 0)
).unwrap();
case_res.record(*event, input, results);
}
}
exp_results.push(case_res.clone());
}
pub enum Strat { NonZero, Zero, None }
for case_results in exp_results.data.iter() {
for (event, event_results) in case_results.data.iter() {
let edesc = event.as_desc();
let minmax = event_results.local_minmax();
let (gmin, min_idx) = event_results.global_min();
let (gmax, max_idx) = event_results.global_max();
let strat = match event {
Zen2Event::DeDisDispatchTokenStalls1(
DeDisDispatchTokenStalls1Mask::TakenBrnchBufferRsrc
) => Strat::NonZero,
Zen2Event::LsPrefInstrDisp(_) => Strat::Zero,
_ => Strat::None,
};
match strat {
Strat::NonZero => {
let limit = minmax.iter().enumerate()
.filter(|(idx,x)| x.0 > 0)
.next()
.unwrap_or((0, &(0, 0)));
println!("{:03x}:{:02x}, limit={:4} ({}) {}",
edesc.id(), edesc.mask(), limit.0, case_results.desc,
edesc.name()
);
},
Strat::Zero => {
let limit = minmax.iter().enumerate()
.filter(|(idx,x)| x.0 == 0)
.next()
.unwrap_or((0, &(0, 0)));
println!("{:03x}:{:02x}, limit={:4} ({}) {}",
edesc.id(), edesc.mask(), limit.0, case_results.desc,
edesc.name()
);
},
Strat::None => {
println!("{:03x}:{:02x}, min={:4} (idx={}) max={:4} (idx={}) ({}) {}",
edesc.id(), edesc.mask(),
gmin,
min_idx,
gmax,
max_idx,
case_results.desc,
edesc.name()
);
},
}
}
}
}
}