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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
mod abort;
mod common;
mod ldc;
mod mcr;
mod wf;
use abort::{decode_iss_data_abort, decode_iss_instruction_abort};
use bit_field::BitField;
use ldc::decode_iss_ldc;
use mcr::{decode_iss_mcr, decode_iss_mcrr};
use std::fmt::{self, Debug, Display, Formatter};
use std::num::ParseIntError;
use thiserror::Error;
use wf::decode_iss_wf;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FieldInfo {
pub name: &'static str,
pub start: usize,
pub width: usize,
pub value: u64,
pub decoded: Option<Decoded>,
}
impl FieldInfo {
fn get(register: u64, name: &'static str, start: usize, end: usize) -> Self {
let value = register.get_bits(start..end);
Self {
name,
start,
width: end - start,
value,
decoded: None,
}
}
fn get_bit(register: u64, name: &'static str, bit: usize) -> Self {
Self::get(register, name, bit, bit + 1)
}
fn with_decoded(self, decoded: Decoded) -> Self {
Self {
decoded: Some(decoded),
..self
}
}
fn with_description(self, description: String) -> Self {
self.with_decoded(Decoded {
description: Some(description),
fields: vec![],
})
}
fn as_bit(&self) -> bool {
assert!(self.width == 1);
self.value == 1
}
fn describe_bit<F>(self, describer: F) -> Self
where
F: FnOnce(bool) -> &'static str,
{
let bit = self.as_bit();
let description = describer(bit).to_string();
self.with_description(description)
}
fn describe<F>(self, describer: F) -> Result<Self, DecodeError>
where
F: FnOnce(u64) -> Result<&'static str, DecodeError>,
{
let description = describer(self.value)?.to_string();
Ok(self.with_description(description))
}
fn check_res0(self) -> Result<Self, DecodeError> {
if self.value != 0 {
Err(DecodeError::InvalidRes0 { res0: self.value })
} else {
Ok(self)
}
}
pub fn value_string(&self) -> String {
if self.width == 1 {
if self.value == 1 { "true" } else { "false" }.to_string()
} else {
format!("{:#01$x}", self.value, (self.width + 3) / 4 + 2,)
}
}
pub fn value_binary_string(&self) -> String {
format!("{:#01$b}", self.value, self.width + 2)
}
}
impl Display for FieldInfo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.width == 1 {
write!(
f,
"{}: {}",
self.name,
if self.value == 1 { "true" } else { "false" }
)
} else {
write!(
f,
"{}: {} {}",
self.name,
self.value_string(),
self.value_binary_string(),
)
}
}
}
#[derive(Debug, Error)]
pub enum DecodeError {
#[error("Invalid ESR, res0 is {res0:#x}")]
InvalidRes0 { res0: u64 },
#[error("Invalid EC {ec:#x}")]
InvalidEc { ec: u64 },
#[error("Invalid DFSC or IFSC {fsc:#x}")]
InvalidFsc { fsc: u64 },
#[error("Invalid SET {set:#x}")]
InvalidSet { set: u64 },
#[error("Invalid AM {am:#x}")]
InvalidAm { am: u64 },
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Decoded {
pub description: Option<String>,
pub fields: Vec<FieldInfo>,
}
fn decode_iss_res0(iss: u64) -> Result<Decoded, DecodeError> {
if iss == 0 {
Ok(Decoded {
description: Some("ISS is RES0".to_string()),
fields: vec![],
})
} else {
Err(DecodeError::InvalidRes0 { res0: iss })
}
}
pub fn decode(esr: u64) -> Result<Decoded, DecodeError> {
let res0 = FieldInfo::get(esr, "RES0", 37, 64).check_res0()?;
let iss2 = FieldInfo::get(esr, "ISS2", 32, 37);
let ec = FieldInfo::get(esr, "EC", 26, 32);
let il = FieldInfo::get_bit(esr, "IL", 25).describe_bit(describe_il);
let iss = FieldInfo::get(esr, "ISS", 0, 25);
let (class, iss_decoded) = match ec.value {
0b000000 => ("Unknown reason", Some(decode_iss_res0(iss.value)?)),
0b000001 => ("Wrapped WF* instruction execution", Some(decode_iss_wf(iss.value)?)),
0b000011 => ("Trapped MCR or MRC access with coproc=0b1111", Some(decode_iss_mcr(iss.value)?)),
0b000100 => ("Trapped MCRR or MRRC access with coproc=0b1111", Some(decode_iss_mcrr(iss.value)?)),
0b000101 => ("Trapped MCR or MRC access with coproc=0b1110", Some(decode_iss_mcr(iss.value)?)),
0b000110 => ("Trapped LDC or STC access", Some(decode_iss_ldc(iss.value)?)),
0b000111 => ("Trapped access to SVE, Advanced SIMD or floating point", None),
0b001010 => ("Trapped execution of an LD64B, ST64B, ST64BV, or ST64BV0 instruction", None),
0b001100 => ("Trapped MRRC access with (coproc==0b1110)", None),
0b001101 => ("Branch Target Exception", None),
0b001110 => ("Illegal Execution state", Some(decode_iss_res0(iss.value)?)),
0b010001 => ("SVC instruction execution in AArch32 state", None),
0b010101 => ("SVC instruction execution in AArch64 state", None),
0b011000 => ("Trapped MSR, MRS or System instruction execution in AArch64 state", None),
0b011001 => ("Access to SVE functionality trapped as a result of CPACR_EL1.ZEN, CPTR_EL2.ZEN, CPTR_EL2.TZ, or CPTR_EL3.EZ", Some(decode_iss_res0(iss.value)?)),
0b011100 => ("Exception from a Pointer Authentication instruction authentication failure", None),
0b100000 => ("Instruction Abort from a lower Exception level", Some(decode_iss_instruction_abort(iss.value)?)),
0b100001 => ("Instruction Abort taken without a change in Exception level", Some(decode_iss_instruction_abort(iss.value)?)),
0b100010 => ("PC alignment fault exception", Some(decode_iss_res0(iss.value)?)),
0b100100 => ("Data Abort from a lower Exception level", Some(decode_iss_data_abort(iss.value)?)),
0b100101 => ("Data Abort taken without a change in Exception level", Some(decode_iss_data_abort(iss.value)?)),
0b100110 => ("SP alignment fault exception", Some(decode_iss_res0(iss.value)?)),
0b101000 => ("Trapped floating-point exception taken from AArch32 state", None),
0b101100 => ("Trapped floating-point exception taken from AArch64 state", None),
0b101111 => ("SError interrupt", None),
0b110000 => ("Breakpoint exception from a lower Exception level", None),
0b110001 => ("Breakpoint exception taken without a change in Exception level", None),
0b110010 => ("Software Step exception from a lower Exception level", None),
0b110011 => ("Software Step exception taken without a change in Exception level", None),
0b110100 => ("Watchpoint exception from a lower Exception level", None),
0b110101 => ("Watchpoint exception taken without a change in Exception level", None),
0b111000 => ("BKPT instruction execution in AArch32 state", None),
0b111100 => ("BRK instruction execution in AArch64 state", None),
_ => return Err(DecodeError::InvalidEc { ec: ec.value }),
};
let iss = FieldInfo {
decoded: iss_decoded,
..iss
};
let ec = ec.with_decoded(Decoded {
description: Some(class.to_string()),
fields: vec![],
});
Ok(Decoded {
description: Some(class.to_string()),
fields: vec![res0, iss2, ec, il, iss],
})
}
fn describe_il(il: bool) -> &'static str {
if il {
"32-bit instruction trapped"
} else {
"16-bit instruction trapped"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown() {
let decoded = decode(0).unwrap();
assert_eq!(
decoded,
Decoded {
description: Some("Unknown reason".to_string()),
fields: vec![
FieldInfo {
name: "RES0",
start: 37,
width: 27,
value: 0,
decoded: None
},
FieldInfo {
name: "ISS2",
start: 32,
width: 5,
value: 0,
decoded: None
},
FieldInfo {
name: "EC",
start: 26,
width: 6,
value: 0,
decoded: Some(Decoded {
description: Some("Unknown reason".to_string()),
fields: vec![]
})
},
FieldInfo {
name: "IL",
start: 25,
width: 1,
value: 0,
decoded: Some(Decoded {
description: Some("16-bit instruction trapped".to_string()),
fields: vec![]
})
},
FieldInfo {
name: "ISS",
start: 0,
width: 25,
value: 0,
decoded: Some(Decoded {
description: Some("ISS is RES0".to_string()),
fields: vec![]
})
}
]
}
);
}
}
pub fn parse_number(s: &str) -> Result<u64, ParseIntError> {
if let Some(hex) = s.strip_prefix("0x") {
u64::from_str_radix(hex, 16)
} else {
s.parse()
}
}