-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitfield.rs
More file actions
279 lines (250 loc) · 7.61 KB
/
bitfield.rs
File metadata and controls
279 lines (250 loc) · 7.61 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Provide a means to specify a bit field when working with CPU ID and feature registers
//!
use super::facts::GenericFact;
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt;
use std::ops;
pub type Register = u128;
/// A type is Bindable if it can be "bound" to a register
pub trait Bindable {
/// The value type that results from a bind
type Rep;
/// A function to extract the value from the register
fn value(&self, reg_val: Register) -> Option<Self::Rep>;
/// Retreive the name of the bindable
fn name(&self) -> &String;
}
#[enum_dispatch()]
pub trait Facter<T: From<u32> + From<bool>> {
fn collect_fact(&self) -> GenericFact<T>;
}
///Wraps a bit flag, usually representing if a feature is present or not
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Flag {
pub name: String,
pub bit: u8,
}
impl Bindable for Flag {
type Rep = bool;
fn value(&self, reg_val: Register) -> Option<Self::Rep> {
let flag = 1u128.checked_shl(self.bit.into())?;
Some((reg_val & flag) != 0)
}
fn name(&self) -> &String {
&self.name
}
}
///Wraps an integer value from a bit field
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Int {
pub name: String,
pub bounds: ops::Range<u8>,
}
impl Bindable for Int {
type Rep = u32;
fn value(&self, reg_val: Register) -> Option<Self::Rep> {
let shift = self.bounds.start;
let mut mask = 0u128;
for _bit in self.bounds.clone() {
mask <<= 1;
mask |= 1;
}
((reg_val >> shift) & mask).try_into().ok()
}
fn name(&self) -> &String {
&self.name
}
}
/// Wraps an X86Model representation
/// These can have a number of weird conditions and are always going to be a part of a bit field
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Model {
pub name: String,
}
const MODEL_START_BIT: u8 = 4;
const EXTENDED_MODEL_START_BIT: u8 = 16;
const FAMILY_START_BIT: u8 = 8;
impl Bindable for X86Model {
type Rep = u32;
fn value(&self, reg_val: Register) -> Option<Self::Rep> {
let reg32 = reg_val as u32;
let nibble_mask = 0xF;
let model = (reg32 >> MODEL_START_BIT) & nibble_mask;
let famil_id = (reg32 >> FAMILY_START_BIT) & nibble_mask;
match famil_id {
6 | 0xF => {
let extended_model = (reg32 >> EXTENDED_MODEL_START_BIT) & nibble_mask;
Some((extended_model << 4) | model)
}
_ => Some(model),
}
}
fn name(&self) -> &String {
&self.name
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Family {
pub name: String,
}
const EXTENDED_FAMILY_START_BIT: u8 = 20;
impl Bindable for X86Family {
type Rep = u32;
fn value(&self, reg_val: Register) -> Option<Self::Rep> {
let reg32 = reg_val as u32;
const FAMILY_MASK: u32 = 0xF;
const EXT_FAMILY_MASK: u32 = 0xFF;
let family = (reg32 >> FAMILY_START_BIT) & FAMILY_MASK;
let extended_family = (reg32 >> EXTENDED_FAMILY_START_BIT) & EXT_FAMILY_MASK;
match family {
0xF => Some(extended_family + family),
_ => Some(family),
}
}
fn name(&self) -> &String {
&self.name
}
}
pub struct Bound<'a, T: Bindable> {
reg_val: Register,
bits: &'a T,
}
impl<'a, T: Bindable> Bound<'a, T> {
pub fn new(reg_val: Register, bits: &'a T) -> Self {
Self { reg_val, bits }
}
pub fn name(&self) -> &String {
self.bits.name()
}
}
impl fmt::Display for Bound<'_, Flag> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{} = {:>10}",
self.bits.name,
if let Some(true) = self.bits.value(self.reg_val) {
"true"
} else {
"false"
}
)
}
}
impl fmt::Display for Bound<'_, Int> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{} = {:>10x}",
self.bits.name,
self.bits.value(self.reg_val).unwrap_or(0)
)
}
}
impl<B, R, T: From<u32> + From<bool>> Facter<T> for Bound<'_, B>
where
R: Default + Into<T>,
B: Bindable<Rep = R>,
{
fn collect_fact(&self) -> GenericFact<T> {
GenericFact::new(
self.bits.name().clone(),
self.bits.value(self.reg_val).unwrap_or_default().into(),
)
}
}
impl fmt::Display for Bound<'_, X86Model> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{} = {:>10}",
self.bits.name,
self.bits.value(self.reg_val).unwrap_or(0)
)
}
}
impl fmt::Display for Bound<'_, X86Family> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{} = {:>10}",
self.bits.name,
self.bits.value(self.reg_val).unwrap_or(0)
)
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum Field {
Int(Int),
Flag(Flag),
X86Model(X86Model),
X86Family(X86Family),
}
pub enum BoundField<'a> {
Int(Bound<'a, Int>),
Flag(Bound<'a, Flag>),
X86Model(Bound<'a, X86Model>),
X86Family(Bound<'a, X86Family>),
}
impl<'a> BoundField<'a> {
pub fn from_register_and_field(reg_val: Register, field: &'a Field) -> Self {
match field {
Field::Int(bits) => Self::Int(Bound { reg_val, bits }),
Field::Flag(bits) => Self::Flag(Bound { reg_val, bits }),
Field::X86Model(bits) => Self::X86Model(Bound { reg_val, bits }),
Field::X86Family(bits) => Self::X86Family(Bound { reg_val, bits }),
}
}
}
impl fmt::Display for BoundField<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::Int(bound) => bound.fmt(f),
Self::Flag(bound) => bound.fmt(f),
Self::X86Model(bound) => bound.fmt(f),
Self::X86Family(bound) => bound.fmt(f),
}
}
}
impl<T: From<bool> + From<u32>> Facter<T> for BoundField<'_> {
fn collect_fact(&self) -> GenericFact<T> {
match self {
Self::Int(bound) => bound.collect_fact(),
Self::Flag(bound) => bound.collect_fact(),
Self::X86Model(bound) => bound.collect_fact(),
Self::X86Family(bound) => bound.collect_fact(),
}
}
}
#[cfg(test)]
mod test {
use crate::bitfield::Bindable;
#[test]
fn x86_model_test() {
let field_definition = super::X86Model {
name: "model".to_string(),
};
let regular_model: super::Register = 0x0AF50341;
assert_eq!(field_definition.value(regular_model).unwrap(), 0x4);
let extended_family_model: super::Register = 0x0AF50641;
assert_eq!(field_definition.value(extended_family_model).unwrap(), 0x54);
let extended_family_model: super::Register = 0x0AF50F41;
assert_eq!(field_definition.value(extended_family_model).unwrap(), 0x54);
}
#[test]
fn x86_family_test() {
let field_definition = super::X86Family {
name: "model".to_string(),
};
let regular_model: super::Register = 0x0AE50341;
assert_eq!(field_definition.value(regular_model).unwrap(), 0x3);
let extended_family_model: super::Register = 0x0AE50F41;
assert_eq!(
field_definition.value(extended_family_model).unwrap(),
0xAE + 0xF
);
}
}