arm-sysregs 0.1.0

Armv8-A system register helper crate
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// SPDX-FileCopyrightText: Copyright The arm-sysregs Contributors.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Manually implemented methods for system register types.

#[cfg(feature = "el1")]
use crate::{
    ClidrEl1, CsselrEl1, EsrEl1, IdAa64dfr0El1, IdAa64dfr1El1, IdAa64mmfr0El1, IdAa64mmfr1El1,
    IdAa64mmfr2El1, IdAa64mmfr3El1, IdAa64pfr0El1, IdAa64pfr1El1, MpidrEl1, SpsrEl1,
    read_mpidr_el1,
};
#[cfg(feature = "el2")]
use crate::{EsrEl2, SpsrEl2};
#[cfg(feature = "el3")]
use crate::{EsrEl3, MdcrEl3, SmcrEl3, SpsrEl3};
#[cfg(feature = "el1")]
use core::fmt::{self, Debug, Formatter};
use num_enum::TryFromPrimitive;

#[cfg(feature = "el1")]
impl ClidrEl1 {
    /// Returns the inner cache boundary level.
    pub fn icb_level(self) -> Option<CacheLevel> {
        let icb = self.icb();
        if icb != 0 {
            Some(CacheLevel(icb as u8))
        } else {
            None
        }
    }

    /// Returns Cache Type [1-7] fields.
    pub fn cache_type(self, level: CacheLevel) -> CacheType {
        self.ctype(level.level().into()).try_into().unwrap()
    }
}

#[cfg(feature = "el1")]
impl CsselrEl1 {
    /// Creates new instance. TnD is only valid if FEAT_MTE2 is implemented.
    pub fn new(tnd: bool, level: CacheLevel, ind: bool) -> Self {
        let mut instance = Self::from_bits_retain(u64::from(level) << 1);

        if ind {
            instance |= Self::IND;
        } else if tnd {
            // TnD is only valid if InD is not set.
            instance |= Self::TND;
        }

        instance
    }

    /// Returns the cache level of requested cache.
    pub fn cache_level(self) -> CacheLevel {
        CacheLevel(self.level() + 1)
    }
}

#[cfg(feature = "el1")]
impl EsrEl1 {
    /// Mask for the parts of an ESR value containing the opcode.
    pub const ISS_SYSREG_OPCODE_MASK: Self = Self::from_bits_retain(0x003f_fc1e);
}

#[cfg(feature = "el1")]
impl Debug for EsrEl1 {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "EsrEl1({:#x})", self.0)
    }
}

#[cfg(feature = "el2")]
impl EsrEl2 {
    /// Mask for the parts of an ESR value containing the opcode.
    pub const ISS_SYSREG_OPCODE_MASK: Self = Self::from_bits_retain(0x003f_fc1e);
}

#[cfg(feature = "el2")]
impl Debug for EsrEl2 {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "EsrEl2({:#x})", self.0)
    }
}

#[cfg(feature = "el3")]
impl EsrEl3 {
    /// Mask for the parts of an ESR value containing the opcode.
    pub const ISS_SYSREG_OPCODE_MASK: Self = Self::from_bits_retain(0x003f_fc1e);
}

#[cfg(feature = "el3")]
impl Debug for EsrEl3 {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "EsrEl3({:#x})", self.0)
    }
}

#[cfg(feature = "el1")]
impl IdAa64dfr0El1 {
    const SYS_REG_TRACE_SUPPORTED: u8 = 1;
    const SPE_SUPPORTED: u8 = 1;
    const TRF_SUPPORTED: u8 = 1;
    const TRBE_NOT_SUPPORTED: u8 = 0;
    const MTPMU_SUPPORTED: u8 = 1;

    /// Trace support. Indicates whether System register interface to a PE trace unit is
    /// implemented.
    pub fn is_feat_sys_reg_trace_present(self) -> bool {
        self.tracever() == Self::SYS_REG_TRACE_SUPPORTED
    }

    /// Indicates whether Armv8.1 Statistical Profiling Extension is implemented.
    pub fn is_feat_spe_present(self) -> bool {
        self.pmsver() >= Self::SPE_SUPPORTED
    }

    /// Indicates whether Armv8.4 Self-hosted Trace Extension is implemented.
    pub fn is_feat_trf_present(self) -> bool {
        self.tracefilt() == Self::TRF_SUPPORTED
    }

    /// Indicates whether Trace Buffer Extension is implemented.
    pub fn is_feat_trbe_present(self) -> bool {
        self.tracebuffer() != Self::TRBE_NOT_SUPPORTED
    }

    /// Indicates whether Multi Threaded PMU Extension is implemented.
    pub fn is_feat_mtpmu_present(self) -> bool {
        self.mtpmu() == Self::MTPMU_SUPPORTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64dfr1El1 {
    const EBEP_IMPLEMENTED: u8 = 0b1;

    /// Indicates whether FEAT_EBEP is implemented.
    pub fn is_feat_ebep_present(self) -> bool {
        self.ebep() == Self::EBEP_IMPLEMENTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64mmfr0El1 {
    const FGT_SUPPORTED: u8 = 0b0001;
    const FGT2_SUPPORTED: u8 = 0b0001;

    /// Indicates whether Fine Grain Traps Extension is implemented.
    pub fn is_feat_fgt_present(self) -> bool {
        let val = self.fgt();
        val == Self::FGT_SUPPORTED || val == Self::FGT2_SUPPORTED
    }

    /// Indicates whether Fine Grain Traps 2 Extension is implemented.
    pub fn is_feat_fgt2_present(self) -> bool {
        self.fgt() == Self::FGT2_SUPPORTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64mmfr1El1 {
    const VH_SUPPORTED: u8 = 0b0001;
    const HCX_SUPPORTED: u8 = 0b0001;

    /// Indicates presence of FEAT_VHE.
    pub fn is_feat_vhe_present(self) -> bool {
        self.vh() >= Self::VH_SUPPORTED
    }

    /// Indicates presence of FEAT_HCX.
    pub fn is_feat_hcx_present(self) -> bool {
        self.hcx() >= Self::HCX_SUPPORTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64mmfr2El1 {
    const CCIDX_64_BIT: u8 = 0b0001;

    /// Checks whether 64-bit format is implemented for all levels of the CCSIDR_EL1.
    pub fn has_64_bit_ccsidr_el1(self) -> bool {
        self.ccidx() == Self::CCIDX_64_BIT
    }
}

#[cfg(feature = "el1")]
impl IdAa64mmfr3El1 {
    const TCRX_SUPPORTED: u8 = 1;

    /// Indicates presence of FEAT_TCR2.
    pub fn is_feat_tcr2_present(self) -> bool {
        self.tcrx() >= Self::TCRX_SUPPORTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64pfr0El1 {
    const SVE_SUPPORTED: u8 = 1;
    const MPAM_SUPPORTED: u8 = 1;

    /// Indicates whether SVE is implemented.
    pub fn is_feat_sve_present(self) -> bool {
        self.sve() == Self::SVE_SUPPORTED
    }

    /// Indicates whether MPAM Extension is implemented.
    pub fn is_feat_mpam_present(self) -> bool {
        self.mpam() == Self::MPAM_SUPPORTED
    }
}

#[cfg(feature = "el1")]
impl IdAa64pfr1El1 {
    const SSBS_IMPLEMENTED: u8 = 0b1;
    const MTE_IMPLEMENTED: u8 = 0b0001;
    const MTE2_IMPLEMENTED: u8 = 0b0010;
    const SME_IMPLEMENTED: u8 = 0b0001;
    const SME2_IMPLEMENTED: u8 = 0b0010;
    const NMI_IMPLEMENTED: u8 = 0b1;
    const GCS_IMPLEMENTED: u8 = 0b1;

    /// Indicates whether FEAT_SSBS is implemented.
    pub fn is_feat_ssbs_present(self) -> bool {
        self.ssbs() >= Self::SSBS_IMPLEMENTED
    }

    /// Indicates whether FEAT_MTE is implemented.
    pub fn is_feat_mte_present(self) -> bool {
        self.mte() >= Self::MTE_IMPLEMENTED
    }

    /// Indicates whether FEAT_MTE2 is implemented.
    pub fn is_feat_mte2_present(self) -> bool {
        self.mte() >= Self::MTE2_IMPLEMENTED
    }

    /// Indicates whether FEAT_SME is implemented.
    pub fn is_feat_sme_present(self) -> bool {
        self.sme() >= Self::SME_IMPLEMENTED
    }

    /// Indicates whether FEAT_SME2 is implemented.
    pub fn is_feat_sme2_present(self) -> bool {
        self.sme() >= Self::SME2_IMPLEMENTED
    }

    /// Indicates whether FEAT_NMI is implemented.
    pub fn is_feat_nmi_present(self) -> bool {
        self.nmi() == Self::NMI_IMPLEMENTED
    }

    /// Indicates whether FEAT_GCS is implemented.
    pub fn is_feat_gcs_present(self) -> bool {
        self.gcs() == Self::GCS_IMPLEMENTED
    }
}

#[cfg(feature = "el3")]
impl MdcrEl3 {
    /// Set to 0b10 to disable AArch32 Secure self-hosted privileged debug from S-EL1.
    pub const SPD32: Self = Self::from_bits_retain(0b10 << 14);
    /// Non-secure state owns the Profiling Buffer. Profiling is disabled in Secure and Realm
    /// states.
    pub const NSPB_NS: Self = Self::from_bits_retain(0b11 << 12);
    /// Enable TRBE register access for the security state that owns the buffer.
    pub const NSTB_EN: Self = Self::from_bits_retain(1 << 24);
    /// Together with MDCR_EL3.NSTBE determines which security state owns the trace buffer
    pub const NSTB_SS: Self = Self::from_bits_retain(1 << 25);
}

#[cfg(feature = "el1")]
impl MpidrEl1 {
    /// Size in bits of the affinity fields.
    pub const AFFINITY_BITS: usize = 8;

    /// Converts a PSCI MPIDR value into the equivalent `MpidrEL1` value.
    ///
    /// This reads the MT and U bits from the current CPU's MPIDR_EL1 value and combines them with
    /// the affinity values from the given `psci_mpidr`.
    ///
    /// This assumes that the MPIDR_EL1 values of all CPUs in a system have the same values for the
    /// MT and U bits.
    pub fn from_psci_mpidr(psci_mpidr: u64) -> Self {
        let mpidr_el1 = read_mpidr_el1();
        Self::from_bits_retain(psci_mpidr) | (mpidr_el1 & (Self::MT | Self::U))
    }
}

#[cfg(feature = "el3")]
impl SmcrEl3 {
    /// Build SMCR_EL3 register value from given SSVE vector length.
    pub fn from_ssve_vector_len(vector_length: u64) -> Self {
        Self::from_bits_retain(((vector_length - 1) / 128) & Self::LEN_MASK)
    }
}

#[cfg(feature = "el1")]
impl SpsrEl1 {
    /// All of the N, Z, C and V bits.
    pub const NZCV: Self = Self::V.union(Self::C).union(Self::Z).union(Self::N);
}

#[cfg(feature = "el2")]
impl SpsrEl2 {
    /// All of the N, Z, C and V bits.
    pub const NZCV: Self = Self::V.union(Self::C).union(Self::Z).union(Self::N);
}

#[cfg(feature = "el3")]
impl SpsrEl3 {
    /// AArch64 execution state, EL0.
    pub const M_AARCH64_EL0: Self = Self::from_bits_retain(0b00000);
    /// AArch64 execution state, EL1 with SP_EL0.
    pub const M_AARCH64_EL1T: Self = Self::from_bits_retain(0b00100);
    /// AArch64 execution state, EL1 with SP_EL1.
    pub const M_AARCH64_EL1H: Self = Self::from_bits_retain(0b00101);
    /// AArch64 execution state, EL2 with SP_EL0.
    pub const M_AARCH64_EL2T: Self = Self::from_bits_retain(0b01000);
    /// AArch64 execution state, EL2 with SP_EL2.
    pub const M_AARCH64_EL2H: Self = Self::from_bits_retain(0b01001);
    /// AArch64 execution state, EL3 with SP_EL0.
    pub const M_AARCH64_EL3T: Self = Self::from_bits_retain(0b01100);
    /// AArch64 execution state, EL3 with SP_EL3.
    pub const M_AARCH64_EL3H: Self = Self::from_bits_retain(0b01101);

    /// Exception was taken with PSTATE.SP set to SP_EL0.
    pub const SP_EL0: Self = Self::from_bits_retain(0);
    /// Exception was taken with PSTATE.SP set to SP_ELx.
    pub const SP_ELX: Self = Self::from_bits_retain(1);

    /// All of the N, Z, C and V bits.
    pub const NZCV: Self = Self::V.union(Self::C).union(Self::Z).union(Self::N);

    /// Speculative Store Bypass Safe.
    pub const SSBS: Self = Self::from_bits_retain(1 << 12);

    const EL_MASK: u64 = 0x3;
    const EL_SHIFT: usize = 2;
    const SP_MASK: u64 = 0x1;

    /// Returns the value of the EL field.
    pub const fn exception_level(self) -> ExceptionLevel {
        match (self.bits() >> Self::EL_SHIFT) & Self::EL_MASK {
            0 => ExceptionLevel::El0,
            1 => ExceptionLevel::El1,
            2 => ExceptionLevel::El2,
            3 => ExceptionLevel::El3,
            _ => unreachable!(),
        }
    }

    /// Returns the value of the SP field.
    pub const fn stack_pointer(self) -> StackPointer {
        match self.bits() & Self::SP_MASK {
            0 => StackPointer::El0,
            1 => StackPointer::ElX,
            _ => unreachable!(),
        }
    }
}

/// Cache type enum.
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
pub enum CacheType {
    /// No cache.
    NoCache = 0b000,
    /// Instruction cache only.
    InstructionOnly = 0b001,
    /// Data cache only.
    DataOnly = 0b010,
    /// Separate instruction and data caches.
    SeparateInstructionAndData = 0b011,
    /// Unified cache.
    Unified = 0b100,
}

/// Wrapper type for describing cache level in a human readable format, i.e. L3 cache = `CacheLevel(3)`
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CacheLevel(pub(crate) u8);

impl CacheLevel {
    /// Creates new instance.
    pub fn new(level: u8) -> Self {
        assert!((1..8).contains(&level));
        Self(level)
    }

    /// Returns the level value.
    pub fn level(&self) -> u8 {
        self.0
    }
}

impl From<CacheLevel> for u32 {
    fn from(value: CacheLevel) -> Self {
        (value.0 - 1).into()
    }
}

impl From<CacheLevel> for u64 {
    fn from(value: CacheLevel) -> Self {
        u32::from(value).into()
    }
}

/// An AArch64 exception level.
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, TryFromPrimitive)]
#[repr(u8)]
pub enum ExceptionLevel {
    /// Exception level 0.
    El0 = 0,
    /// Exception level 1.
    El1 = 1,
    /// Exception level 2.
    El2 = 2,
    /// Exception level 3.
    El3 = 3,
}

/// Values for SPSEL.
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, TryFromPrimitive)]
#[repr(u8)]
pub enum StackPointer {
    /// Use SP_EL0.
    El0 = 0,
    /// Use SP_EL1, SP_EL2 or SP_EL3 according to the current exception level.
    ElX = 1,
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "el1")]
    use super::*;

    #[test]
    #[cfg(feature = "el1")]
    fn debug_mpidr_el1() {
        assert_eq!(format!("{:?}", MpidrEl1::empty()), "MpidrEl1(0x0)");
        assert_eq!(
            format!("{:?}", MpidrEl1::MT | MpidrEl1::U),
            "MpidrEl1(MT | U)"
        );
        assert_eq!(
            format!("{:?}", MpidrEl1::from_bits_retain(0x12_4134_5678)),
            "MpidrEl1(MT | U | 0x1200345678)"
        );
    }

    #[cfg(feature = "el1")]
    #[test]
    fn debug_spsr_el1() {
        assert_eq!(format!("{:?}", SpsrEl1::empty()), "SpsrEl1(0x0)");
        assert_eq!(format!("{:?}", SpsrEl1::NZCV), "SpsrEl1(V | C | Z | N)");
    }

    #[cfg(feature = "el2")]
    #[test]
    fn debug_spsr_el2() {
        assert_eq!(format!("{:?}", SpsrEl2::empty()), "SpsrEl2(0x0)");
        assert_eq!(format!("{:?}", SpsrEl2::NZCV), "SpsrEl2(V | C | Z | N)");
    }

    #[cfg(feature = "el3")]
    #[test]
    fn debug_spsr_el3() {
        assert_eq!(format!("{:?}", SpsrEl3::empty()), "SpsrEl3(0x0)");
        assert_eq!(format!("{:?}", SpsrEl3::NZCV), "SpsrEl3(V | C | Z | N)");
        assert_eq!(format!("{:?}", SpsrEl3::M_AARCH64_EL3H), "SpsrEl3(0xd)");
    }

    #[cfg(feature = "el1")]
    #[test]
    fn debug_esr_el1() {
        assert_eq!(format!("{:?}", EsrEl1::empty()), "EsrEl1(0x0)");
        assert_eq!(format!("{:?}", EsrEl1::IL), "EsrEl1(0x2000000)");
        assert_eq!(
            format!("{:?}", EsrEl1::ISS_SYSREG_OPCODE_MASK),
            "EsrEl1(0x3ffc1e)"
        );
    }

    #[cfg(feature = "el2")]
    #[test]
    fn debug_esr_el2() {
        assert_eq!(format!("{:?}", EsrEl2::empty()), "EsrEl2(0x0)");
        assert_eq!(format!("{:?}", EsrEl2::IL), "EsrEl2(0x2000000)");
        assert_eq!(
            format!("{:?}", EsrEl2::ISS_SYSREG_OPCODE_MASK),
            "EsrEl2(0x3ffc1e)"
        );
    }

    #[cfg(feature = "el3")]
    #[test]
    fn debug_esr_el3() {
        assert_eq!(format!("{:?}", EsrEl3::empty()), "EsrEl3(0x0)");
        assert_eq!(format!("{:?}", EsrEl3::IL), "EsrEl3(0x2000000)");
        assert_eq!(
            format!("{:?}", EsrEl3::ISS_SYSREG_OPCODE_MASK),
            "EsrEl3(0x3ffc1e)"
        );
    }
}