arm-tzc 0.2.1

Arm TrustZone Address Space Controller
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// SPDX-FileCopyrightText: Copyright The arm-tzc Contributors.
// SPDX-License-Identifier: MIT OR Apache-2.0

#![no_std]
#![doc = include_str!("../README.md")]
#![deny(clippy::undocumented_unsafe_blocks)]
#![deny(unsafe_op_in_unsafe_fn)]

mod fail;
mod region;
mod utils;

use bitflags::bitflags;
use safe_mmio::{
    SharedMmioPointer, UniqueMmioPointer, field, field_shared,
    fields::{ReadOnly, ReadPure, ReadPureWrite, WriteOnly},
};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

pub use crate::fail::{FailAccessDirection, FailControlRegister, FailIDRegister, TzcFail};
pub use crate::region::{RegionAttributes, RegionIDAccess, SecureAccess, TzcRegion, TzcRegionMut};
use crate::{
    fail::FailRegisters,
    region::RegionRegisters,
    utils::{extract_bits, get_bit, set_bit},
};

macro_rules! for_in_slice {
    ($var:ident in $slice:expr, $($body:stmt)*) => {
        let mut i = 0;
        while i < $slice.len() {
            let $var = &$slice[i];
            $($body)*
            i += 1;
        }
    };
}
pub(crate) use for_in_slice;

#[derive(Debug, Clone, Copy, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
/// Provides information about the configuration of the TZC-400.
#[repr(transparent)]
pub struct BuildConfigRegister(u32);

impl BuildConfigRegister {
    // Bit values for number of filters.
    const NF_1: u32 = 0b00;
    const NF_2: u32 = 0b01;
    const NF_4: u32 = 0b11;

    /// Returns the number of filter units in the design implementation.
    pub const fn number_of_filters(&self) -> usize {
        match extract_bits(self.0, 25, 24) {
            Self::NF_1 => 1,
            Self::NF_2 => 2,
            Self::NF_4 => 4,
            _ => panic!("Use of reserved value for number of filters"),
        }
    }

    // Bit values for address width.
    const AW_32: u32 = 0b011111;
    const AW_36: u32 = 0b100011;
    const AW_40: u32 = 0b100111;
    const AW_48: u32 = 0b101111;
    const AW_64: u32 = 0b111111;

    /// Returns the width of the ACE-Lite address bus.
    pub const fn address_width(&self) -> usize {
        match extract_bits(self.0, 13, 8) {
            Self::AW_32 => 32,
            Self::AW_36 => 36,
            Self::AW_40 => 40,
            Self::AW_48 => 48,
            Self::AW_64 => 64,
            _ => panic!("Use of reserved value for address width"),
        }
    }

    // Bit values for number of regions.
    const NR_9: u32 = 0b01000;

    /// Returns the number of regions that the TZC-400 provides.
    pub fn number_of_regions(&self) -> usize {
        match extract_bits(self.0, 4, 0) {
            Self::NR_9 => 9,
            _ => panic!("Use of reserved value for number of regions"),
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
/// Controls the interrupt and bus response signaling behavior of the TZC-400 when region permission
/// failures occur.
#[repr(transparent)]
pub struct ActionRegister(u32);

impl ActionRegister {
    /// Sets TZCINT LOW and issues an OKAY response.
    pub const TZCINTLOW_OKAY: ActionRegister = ActionRegister(0b00);
    /// Sets TZCINT LOW and issues a DECERR response.
    pub const TZCINTLOW_DECERR: ActionRegister = ActionRegister(0b01);
    /// Sets TZCINT HIGH and issues an OKAY response.
    pub const TZCINTHIGH_OKAY: ActionRegister = ActionRegister(0b10);
    /// Sets TZCINT HIGH and issues a DECERR response.
    pub const TZCINTHIGH_DECERR: ActionRegister = ActionRegister(0b11);
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u32)]
/// See [`GateKeeper::status`].
pub enum GateKeeperStatus {
    /// Access to the associated filter is not permitted.
    Closed = 0,
    /// Access to the associated filter is permitted.
    Opened = 1,
}

#[derive(Debug)]
/// Provides control and status for the gate keeper in each filter unit implemented.
///
/// ## Usage constraints Closing the gate can cause accesses on the command channels to stall and can inadvertently cause a bus deadlock. Use this with caution.
pub struct GateKeeper<'reg> {
    reg: SharedMmioPointer<'reg, ReadPureWrite<u32>>,
    index: usize,
}

impl<'reg> GateKeeper<'reg> {
    /// The current state of the gate keeper in the filter unit at `index`. `index` must be the
    /// index of a valid filter in the TZ-ASC configuration.
    pub fn status(&self) -> GateKeeperStatus {
        if get_bit(self.reg.read(), self.index + 16) {
            GateKeeperStatus::Opened
        } else {
            GateKeeperStatus::Closed
        }
    }
}

#[derive(Debug)]
/// Provides control and status for the gate keeper in each filter unit implemented.
///
/// ## Usage constraints
///
/// Closing the gate can cause accesses on the command channels to stall and can inadvertently cause
/// a bus deadlock. Use this with caution.
pub struct GateKeeperMut<'reg> {
    reg: UniqueMmioPointer<'reg, ReadPureWrite<u32>>,
    index: usize,
}

impl<'reg> GateKeeperMut<'reg> {
    /// See [GateKeeper::status].
    pub fn status(&self) -> GateKeeperStatus {
        GateKeeper {
            reg: *self.reg,
            index: self.index,
        }
        .status()
    }

    /// Request the gate at `index` to be set to `status`. The request will take effect once all
    /// outstanding accesses through the filter unit are complete.
    pub fn request(&mut self, status: GateKeeperStatus) {
        let mut value = self.reg.read();

        set_bit(
            &mut value,
            self.index,
            matches!(status, GateKeeperStatus::Opened),
        );
        self.reg.write(value);

        while self.status() != status {}
    }
}

#[derive(Debug, Clone, Copy, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
/// Controls the read access speculation and write access speculation.
#[repr(transparent)]
pub struct SpeculationControlRegister(u32);

bitflags! {
    impl SpeculationControlRegister: u32 {
        /// Disables read access speculation.
        ///
        /// Note: This bit is ignored and assumed to be zero at a filter unit if the corresponding
        /// QVNENABLE signal is HIGH.
        const READ_SPEC_DISABLE = 1 << 0;

        /// Disables write access speculation.
        ///
        /// Note: This bit is ignored and assumed to be zero at a filter unit if the corresponding
        /// QVNENABLE signal is HIGH.
        const WRITE_SPEC_DISABLE = 1 << 1;
    }
}

#[derive(Debug, Clone, Copy, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
/// Contains the status of the interrupt signal, *TZCINT*, that reports access security violations
/// or region overlap errors.
#[repr(transparent)]
pub struct InterruptStatusRegister(u32);

impl InterruptStatusRegister {
    /// When a bit is set to 1 it indicates a violation of the overlap region configuration rules
    /// for the associated filter unit. This occurs when an access matches with two enabled regions
    /// at the same time unless the overlap is only with Region 0.
    ///
    /// This bit is set even if the [`ActionRegister`] is set to not drive the interrupt.
    ///
    /// When this bit is true, the interrupt status bit is also set to true.
    ///
    /// Clear the interrupt status of the associated bit in the [`InterruptClearRegister`] to also
    /// clear this field.
    pub const fn overlap(&self, index: usize) -> bool {
        assert!(index < 4);
        get_bit(self.0, index + 16)
    }

    /// When a bit is set to `true`, it indicates the occurrence of two or more region permission or
    /// region overlapping failures at the associated filter unit after the interrupt was cleared by
    /// the associated bit in the [`InterruptClearRegister`].
    ///
    /// This bit is set even if the [`ActionRegister`] is set to not drive the interrupt.
    ///
    /// Clear the interrupt status of the associated bit in the [`InterruptClearRegister`] to also
    /// clear this field.
    pub const fn overrun(&self, index: usize) -> bool {
        assert!(index < 4);
        get_bit(self.0, index + 8)
    }

    /// This field indicates the status of the interrupt from the corresponding filter unit as
    /// follows:
    /// - `false`: Interrupt is not asserted.
    /// - `true`: Interrupt is asserted and waiting to be cleared.
    ///
    /// This bit is set even if the [`ActionRegister`] is set to not drive the interrupt output
    /// TZCINT HIGH.
    ///
    /// Therefore, the status acts as an indicator that a region permission check failure or an
    /// overlap error has occurred at a particular filter unit.
    pub const fn status(&self, index: usize) -> bool {
        assert!(index < 4);
        get_bit(self.0, index)
    }
}

#[derive(
    Debug, Clone, Copy, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq, Default,
)]
/// Clears the interrupt.
#[repr(transparent)]
pub struct InterruptClearRegister(u32);

impl InterruptClearRegister {
    /// Creates an instance of [`InterruptClearRegister`] requesting to clear all the interrupts
    /// present in `interrupts`.
    pub const fn new(interrupts: &[usize]) -> Self {
        let mut s = Self(0);

        for_in_slice!(int in interrupts, s.0 |= 1 << *int);

        s
    }
}
/// View over the TZC memory.
#[derive(Debug, Clone, Eq, FromBytes, Immutable, IntoBytes, KnownLayout, PartialEq)]
#[repr(C, align(4))]
pub struct TzcRegisters {
    /// 0x000: Build configuration register.
    build_config: ReadPure<BuildConfigRegister>,
    /// 0x004: Action register.
    action: ReadPureWrite<ActionRegister>,
    /// 0x008: Gate keeper register.
    gate_keeper: ReadPureWrite<u32>,
    /// 0x00C: Speculation register.
    speculation_ctrl: ReadPureWrite<SpeculationControlRegister>,
    /// 0x010: Interrupt status register.
    int_status: ReadOnly<InterruptStatusRegister>,
    /// 0x014: Interrupt clear register.
    int_clear: WriteOnly<InterruptClearRegister>,
    /// 0x018-0x020: reserved.
    reserved_18: [u32; 2],

    /// 0x020-0x060: The fail status registers provide information about an access that fails
    /// because of insufficient permissions or an overlap condition. A separate set of fail status
    /// registers is provided for each filter unit.
    fail_registers: [FailRegisters; 4],
    /// 0x060-0x100: reserved.
    reserved_60: [u32; 40],

    /// 0x100-0x218: Registers for region 0 to 8 (included).
    ///
    /// The region control registers control the address space that each region controls and the
    /// security attributes to use for that region.
    region_registers: [RegionRegisters; 9],

    /// 0x220-0xf00: reserved.
    ///
    /// Although the specification states that this reserved range should start at 0x218, the 8
    /// first reserved bytes are actually at the end of the `RegionRegister` entry for Region 9.
    reserved_220: [u32; 823],
    padding_f00: [u32; 53],

    /// 0xFD0: Peripheral ID 4 register.
    pid4: ReadPure<u32>,
    /// 0xFD4: Peripheral ID 5 register.
    pid5: ReadPure<u32>,
    /// 0xFD8: Peripheral ID 6 register.
    pid6: ReadPure<u32>,
    /// 0xFDC: Peripheral ID 7 register.
    pid7: ReadPure<u32>,
    /// 0xFE0: Peripheral ID 0 register.
    pid0: ReadPure<u32>,
    /// 0xFE4: Peripheral ID 1 register.
    pid1: ReadPure<u32>,
    /// 0xFE8: Peripheral ID 2 register.
    pid2: ReadPure<u32>,
    /// 0xFEC: Peripheral ID 3 register.
    pid3: ReadPure<u32>,

    /// 0xFF0: Component ID 0 register.
    cid0: ReadPure<u32>,
    /// 0xFF4: Component ID 1 register.
    cid1: ReadPure<u32>,
    /// 0xFF8: Component ID 2 register.
    cid2: ReadPure<u32>,
    /// 0xFFC: Component ID 3 register.
    cid3: ReadPure<u32>,
}

/// Interface to manipulate the [TZC-400 TrustZone Address Space Controller](https://developer.arm.com/documentation/ddi0504/latest).
pub struct Tzc<'a> {
    regs: UniqueMmioPointer<'a, TzcRegisters>,
}

impl<'a> Tzc<'a> {
    /// Creates new TZC instance from a [`UniqueMmioPointer`] that points to the memory region of
    /// the Controller.
    pub const fn new(regs: UniqueMmioPointer<'a, TzcRegisters>) -> Self {
        Self { regs }
    }

    /// Returns the [`BuildConfigRegister`] for this TZC-400 instance.
    pub fn build_configuration(&self) -> BuildConfigRegister {
        field_shared!(self.regs, build_config).read()
    }

    /// Returns the current [`ActionRegister`] configuration.
    pub fn action(&self) -> ActionRegister {
        field_shared!(self.regs, action).read()
    }
    /// Sets the [`ActionRegister`] configuration.
    pub fn set_action(&mut self, value: ActionRegister) {
        field!((self.regs), action).write(value);
    }

    /// Returns a [`GateKeeper`] wrapper to read from the Gate Keeper Register.
    pub fn gate_keeper<'reg>(&'reg self, index: usize) -> Option<GateKeeper<'reg>> {
        if index < self.build_configuration().number_of_filters() {
            Some(GateKeeper {
                reg: field_shared!(self.regs, gate_keeper),
                index,
            })
        } else {
            None
        }
    }

    /// Returns a [`GateKeeperMut`] wrapper to read from/write to the Gate Keeper Register.
    pub fn gate_keeper_mut<'reg>(&'reg mut self, index: usize) -> Option<GateKeeperMut<'reg>> {
        if index < self.build_configuration().number_of_filters() {
            Some(GateKeeperMut {
                reg: field!(self.regs, gate_keeper),
                index,
            })
        } else {
            None
        }
    }

    /// Returns the current [`SpeculationControlRegister`] configuration.
    pub fn speculation_control(&self) -> SpeculationControlRegister {
        field_shared!(self.regs, speculation_ctrl).read()
    }
    /// Sets the [`SpeculationControlRegister`] configuration.
    pub fn set_speculation_control(&mut self, value: SpeculationControlRegister) {
        field!(self.regs, speculation_ctrl).write(value);
    }

    /// Returns the current [`InterruptStatusRegister`].
    pub fn interrupt_status(&mut self) -> InterruptStatusRegister {
        field!(self.regs, int_status).read()
    }

    /// Sets the [`InterruptClearRegister`].
    pub fn clear_interrupts(&mut self, value: InterruptClearRegister) {
        field!(self.regs, int_clear).write(value);
    }

    /// Returns a [`TzcFail`] structure to read from the failure register of the filter at `index`,
    /// or `None` if `index` is out of bounds.
    pub fn filter_failure<'regs>(&'regs self, idx: usize) -> Option<TzcFail<'regs>> {
        if idx < self.build_configuration().number_of_filters() {
            let fails = field_shared!(self.regs, fail_registers);
            fails.get(idx).map(|regs| TzcFail { regs })
        } else {
            None
        }
    }

    /// Returns a [`TzcRegion`] structure to read from the region registers at `index`, or `None` if
    /// `index` is out of bounds.
    pub fn region<'regs>(&'regs self, idx: usize) -> Option<TzcRegion<'regs>> {
        let config = self.build_configuration();

        if idx < config.number_of_regions() {
            let regions = field_shared!(self.regs, region_registers);
            regions.get(idx).map(|regs| TzcRegion {
                regs,
                address_width_mask: ((1_u64.unbounded_shl(config.address_width() as u32))
                    .wrapping_sub(1)),
            })
        } else {
            None
        }
    }

    /// Returns a [`TzcRegionMut`] structure to read and write from the region registers at `index`,
    /// or `None` if `index` is out of bounds.
    pub fn region_mut<'regs>(&'regs mut self, idx: usize) -> Option<TzcRegionMut<'regs>> {
        let config = self.build_configuration();

        if idx < config.number_of_regions() {
            let regions = field!(self.regs, region_registers);
            regions.take(idx).map(|regs| TzcRegionMut {
                regs,
                addresses_writable: idx != 0,
                address_width_mask: ((1_u64.unbounded_shl(config.address_width() as u32))
                    .wrapping_sub(1)),
            })
        } else {
            None
        }
    }

    /// Returns the peripheral ID, which is `0x4002BB460`.
    pub fn peripheral_id(&self) -> u64 {
        let values = [
            field_shared!(self.regs, pid0).read(),
            field_shared!(self.regs, pid1).read(),
            field_shared!(self.regs, pid2).read(),
            field_shared!(self.regs, pid3).read(),
            field_shared!(self.regs, pid4).read(),
            field_shared!(self.regs, pid5).read(),
            field_shared!(self.regs, pid6).read(),
            field_shared!(self.regs, pid7).read(),
        ];

        values
            .into_iter()
            .map(|v| v as u64)
            .enumerate()
            .fold(0, |acc, (idx, value)| (value << (8 * idx as u64)) | acc)
    }

    /// Holds a 32 bit component ID value. Used for automatic BIOS configuration. The result is
    /// `0xB105F00D`.
    pub fn component_id(&self) -> u64 {
        let values = [
            field_shared!(self.regs, cid0).read(),
            field_shared!(self.regs, cid1).read(),
            field_shared!(self.regs, cid2).read(),
            field_shared!(self.regs, cid3).read(),
        ];

        values
            .into_iter()
            .map(|v| v as u64)
            .enumerate()
            .fold(0, |acc, (idx, value)| (value << (8 * idx as u64)) | acc)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use safe_mmio::UniqueMmioPointer;
    use zerocopy::transmute_mut;

    use crate::*;

    const DEFAULT_BUILD_CONFIG: u32 = 0b0000_0001_0000_0000_0010_1111_0000_1000;

    pub(crate) struct FakeTZCRegisters {
        regs: [u32; 1024],
    }

    impl FakeTZCRegisters {
        pub fn new() -> Self {
            let mut s = Self { regs: [0u32; 1024] };
            s.reg_write(0x000, DEFAULT_BUILD_CONFIG);
            s
        }

        pub fn reg_write(&mut self, offset: usize, value: u32) {
            self.regs[offset / 4] = value;
        }

        pub fn reg_read(&self, offset: usize) -> u32 {
            self.regs[offset / 4]
        }

        fn get(&mut self) -> UniqueMmioPointer<'_, TzcRegisters> {
            UniqueMmioPointer::from(transmute_mut!(&mut self.regs))
        }

        pub fn tzc_for_test(&mut self) -> Tzc<'_> {
            Tzc::new(self.get())
        }
    }

    #[test]
    fn size() {
        assert_eq!(size_of::<TzcRegisters>(), 4096);
    }

    #[test]
    fn build_config() {
        let mut regs = FakeTZCRegisters::new();

        let config = regs.tzc_for_test().build_configuration();
        assert_eq!(config.number_of_filters(), 2);
        assert_eq!(config.address_width(), 48);
        assert_eq!(config.number_of_regions(), 9);

        regs.reg_write(0x000, 0b0000_0000_0000_0000_0011_1111_0000_1000);
        let config = regs.tzc_for_test().build_configuration();
        assert_eq!(config.number_of_filters(), 1);
        assert_eq!(config.address_width(), 64);
        assert_eq!(config.number_of_regions(), 9);

        regs.reg_write(0x000, 0b0000_0011_0000_0000_0010_0111_0000_1000);
        let config = regs.tzc_for_test().build_configuration();
        assert_eq!(config.number_of_filters(), 4);
        assert_eq!(config.address_width(), 40);
        assert_eq!(config.number_of_regions(), 9);

        regs.reg_write(0x000, 0b0000_0011_0000_0000_0010_0011_0000_1000);
        let config = regs.tzc_for_test().build_configuration();
        assert_eq!(config.number_of_filters(), 4);
        assert_eq!(config.address_width(), 36);
        assert_eq!(config.number_of_regions(), 9);

        regs.reg_write(0x000, 0b0000_0011_0000_0000_0001_1111_0000_1000);
        let config = regs.tzc_for_test().build_configuration();
        assert_eq!(config.number_of_filters(), 4);
        assert_eq!(config.address_width(), 32);
        assert_eq!(config.number_of_regions(), 9);
    }

    #[test]
    #[should_panic]
    fn build_config_reserved_filters() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0x000, 0b0000_0010_0000_0000_0010_1111_0000_1000);
        let config = regs.tzc_for_test().build_configuration();

        config.number_of_filters();
    }

    #[test]
    #[should_panic]
    fn build_config_reserved_address_width() {
        fn run(address_width: u32, regs: &mut FakeTZCRegisters) {
            regs.reg_write(
                0x000,
                0b0000_0001_0000_0000_0000_0000_0000_1000 | address_width << 8,
            );
            regs.tzc_for_test().build_configuration().address_width();
        }

        let mut regs = FakeTZCRegisters::new();

        run(0b11_0011, &mut regs);
        run(0b11_1101, &mut regs);
        run(0b00_0000, &mut regs);
    }

    #[test]
    #[should_panic]
    fn build_config_reserved_regions() {
        fn run(region_count: u32, regs: &mut FakeTZCRegisters) {
            regs.reg_write(
                0x000,
                0b0000_0001_0000_0000_0011_1111_0000_0000 | region_count,
            );
            regs.tzc_for_test()
                .build_configuration()
                .number_of_regions();
        }

        let mut regs = FakeTZCRegisters::new();

        run(0b1_1111, &mut regs);
        run(0b0_0000, &mut regs);
        run(0b0_1100, &mut regs);
    }

    #[test]
    fn build_config_64_bit_address_region() {
        let mut regs = FakeTZCRegisters::new();

        // Testing address mask calculation with 64 bit addresses.
        regs.reg_write(0x000, 0b0000_0000_0000_0000_0011_1111_0000_1000);

        let mut tzc = regs.tzc_for_test();

        let region = tzc.region(1);
        assert!(region.is_some());
        assert_eq!(u64::MAX, region.unwrap().address_width_mask);

        let region = tzc.region_mut(1);
        assert!(region.is_some());
        assert_eq!(u64::MAX, region.unwrap().address_width_mask);
    }

    #[test]
    fn get_action() {
        let mut regs = FakeTZCRegisters::new();

        regs.reg_write(0x004, 0b0000_0000_0000_0000_0000_0000_0000_0000);
        assert_eq!(regs.tzc_for_test().action(), ActionRegister::TZCINTLOW_OKAY);

        regs.reg_write(0x004, 0b0000_0000_0000_0000_0000_0000_0000_0001);
        assert_eq!(
            regs.tzc_for_test().action(),
            ActionRegister::TZCINTLOW_DECERR
        );

        regs.reg_write(0x004, 0b0000_0000_0000_0000_0000_0000_0000_0010);
        assert_eq!(
            regs.tzc_for_test().action(),
            ActionRegister::TZCINTHIGH_OKAY
        );

        regs.reg_write(0x004, 0b0000_0000_0000_0000_0000_0000_0000_0011);
        assert_eq!(
            regs.tzc_for_test().action(),
            ActionRegister::TZCINTHIGH_DECERR
        );
    }

    #[test]
    fn set_action() {
        let mut regs = FakeTZCRegisters::new();
        let mut tzc = regs.tzc_for_test();

        tzc.set_action(ActionRegister::TZCINTLOW_OKAY);
        assert_eq!(tzc.action(), ActionRegister::TZCINTLOW_OKAY);

        tzc.set_action(ActionRegister::TZCINTLOW_DECERR);
        assert_eq!(tzc.action(), ActionRegister::TZCINTLOW_DECERR);

        tzc.set_action(ActionRegister::TZCINTHIGH_OKAY);
        assert_eq!(tzc.action(), ActionRegister::TZCINTHIGH_OKAY);

        tzc.set_action(ActionRegister::TZCINTHIGH_DECERR);
        assert_eq!(tzc.action(), ActionRegister::TZCINTHIGH_DECERR);
    }

    #[test]
    fn gate_keeper_oob() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0x008, 0b0000_0000_0000_0010_0000_0000_0000_0000);
        let mut tzc = regs.tzc_for_test();

        assert!(tzc.gate_keeper(2).is_none());
        assert!(tzc.gate_keeper(3).is_none());
        assert!(tzc.gate_keeper(4).is_none());
        assert!(tzc.gate_keeper(18).is_none());

        assert!(tzc.gate_keeper_mut(2).is_none());
        assert!(tzc.gate_keeper_mut(3).is_none());
        assert!(tzc.gate_keeper_mut(4).is_none());
        assert!(tzc.gate_keeper_mut(18).is_none());
    }

    #[test]
    fn gate_keeper_status() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0x008, 0b0000_0000_0000_0010_0000_0000_0000_0000);
        let tzc = regs.tzc_for_test();

        assert_eq!(
            tzc.gate_keeper(0).unwrap().status(),
            GateKeeperStatus::Closed
        );
        assert_eq!(
            tzc.gate_keeper(1).unwrap().status(),
            GateKeeperStatus::Opened
        );
    }

    #[test]
    fn gate_keeper_request() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0x008, 0b0000_0000_0000_0010_0000_0000_0000_0001);
        let mut tzc = regs.tzc_for_test();

        tzc.gate_keeper_mut(0)
            .unwrap()
            .request(GateKeeperStatus::Closed);
        tzc.gate_keeper_mut(1)
            .unwrap()
            .request(GateKeeperStatus::Opened);

        assert_eq!(
            regs.reg_read(0x008),
            0b0000_0000_0000_0010_0000_0000_0000_0010,
        )
    }

    #[test]
    fn get_speculation_control() {
        fn run(flags: u32, regs: &mut FakeTZCRegisters, value: SpeculationControlRegister) {
            regs.reg_write(0x00C, flags);
            let spec = regs.tzc_for_test().speculation_control();
            assert_eq!(spec, value);
        }

        let mut regs = FakeTZCRegisters::new();

        run(0b00, &mut regs, SpeculationControlRegister::empty());
        run(
            0b01,
            &mut regs,
            SpeculationControlRegister::READ_SPEC_DISABLE,
        );
        run(
            0b10,
            &mut regs,
            SpeculationControlRegister::WRITE_SPEC_DISABLE,
        );
        run(
            0b11,
            &mut regs,
            SpeculationControlRegister::READ_SPEC_DISABLE
                | SpeculationControlRegister::WRITE_SPEC_DISABLE,
        );
    }

    #[test]
    fn set_speculation_control() {
        fn run(flags: u32, regs: &mut FakeTZCRegisters, value: SpeculationControlRegister) {
            regs.tzc_for_test().set_speculation_control(value);
            assert_eq!(regs.reg_read(0x00C), flags);
        }

        let mut regs = FakeTZCRegisters::new();

        run(0b00, &mut regs, SpeculationControlRegister::empty());
        run(
            0b01,
            &mut regs,
            SpeculationControlRegister::READ_SPEC_DISABLE,
        );
        run(
            0b10,
            &mut regs,
            SpeculationControlRegister::WRITE_SPEC_DISABLE,
        );
        run(
            0b11,
            &mut regs,
            SpeculationControlRegister::READ_SPEC_DISABLE
                | SpeculationControlRegister::WRITE_SPEC_DISABLE,
        );
    }

    #[test]
    pub fn interrupt_status() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0x010, 0b0000_0000_0000_0010_0000_0001_0000_0010);
        let mut tzc = regs.tzc_for_test();

        let status = tzc.interrupt_status();

        assert!(!status.overlap(0));
        assert!(status.overlap(1));

        assert!(status.overrun(0));
        assert!(!status.overrun(1));

        assert!(!status.status(0));
        assert!(status.status(1));
    }

    #[test]
    pub fn interrupt_clear() {
        let mut regs = FakeTZCRegisters::new();
        let mut tzc = regs.tzc_for_test();

        let clear = InterruptClearRegister::new(&[0, 3]);
        tzc.clear_interrupts(clear);

        assert_eq!(
            regs.reg_read(0x014),
            0b0000_0000_0000_0000_0000_0000_0000_1001
        );
    }

    #[test]
    fn peripheral_id() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0xFD0, 0x04);
        regs.reg_write(0xFD4, 0x00);
        regs.reg_write(0xFD8, 0x00);
        regs.reg_write(0xFDC, 0x00);
        regs.reg_write(0xFE0, 0x60);
        regs.reg_write(0xFE4, 0xB4);
        regs.reg_write(0xFE8, 0x2B);
        regs.reg_write(0xFEC, 0x00);

        let tzc = regs.tzc_for_test();
        assert_eq!(tzc.peripheral_id(), 0x0000_0004_002B_B460);
    }

    #[test]
    fn component_id() {
        let mut regs = FakeTZCRegisters::new();
        regs.reg_write(0xFF0, 0x0D);
        regs.reg_write(0xFF4, 0xF0);
        regs.reg_write(0xFF8, 0x05);
        regs.reg_write(0xFFC, 0xB1);

        let tzc = regs.tzc_for_test();
        assert_eq!(tzc.component_id(), 0xB105F00D);
    }

    #[test]
    fn fail_oob() {
        let mut regs = FakeTZCRegisters::new();
        let tzc = regs.tzc_for_test();

        assert!(tzc.filter_failure(4).is_none());
        assert!(tzc.filter_failure(8).is_none());
        assert!(tzc.filter_failure(123124).is_none());
    }

    #[test]
    fn region_oob() {
        let mut regs = FakeTZCRegisters::new();
        let tzc = regs.tzc_for_test();

        assert!(tzc.region(9).is_none());
        assert!(tzc.region(32).is_none());
        assert!(tzc.region(123124).is_none());
    }

    #[test]
    fn region_mut_oob() {
        let mut regs = FakeTZCRegisters::new();
        let mut tzc = regs.tzc_for_test();

        assert!(tzc.region_mut(9).is_none());
        assert!(tzc.region_mut(32).is_none());
        assert!(tzc.region_mut(123124).is_none());
    }
}