earlgrey/
epmp.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
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! The EarlGrey SoC ePMP implementation.
//!
//! Refer to the main [`EarlGreyEPMP`] struct documentation.

use core::cell::Cell;
use core::fmt;
use core::marker::PhantomData;
use kernel::platform::mpu;
use kernel::utilities::registers::FieldValue;
use rv32i::csr;
use rv32i::pmp::{
    format_pmp_entries, pmpcfg_octet, NAPOTRegionSpec, TORRegionSpec, TORUserPMP, TORUserPMPCFG,
};

// ---------- EarlGrey ePMP implementation named constants ---------------------
//
// The ePMP implementation (in part) relies on these constant values. Simply
// changing them here may break the implementation below.
const PMP_ENTRIES: usize = 16;
const PMP_ENTRIES_OVER_TWO: usize = 8;
const TOR_USER_REGIONS_DEBUG_ENABLE: usize = 4;
const TOR_USER_REGIONS_DEBUG_DISABLE: usize = 4;
const TOR_USER_ENTRIES_OFFSET_DEBUG_ENABLE: usize = 0;
const TOR_USER_ENTRIES_OFFSET_DEBUG_DISABLE: usize = 4;

// ---------- EarlGrey ePMP memory region wrapper types ------------------------
//
// These types exist primarily to avoid argument confusion in the
// [`EarlGreyEPMP`] constructor, which accepts the addresses of these memory
// regions as arguments. They further encode whether a region must adhere to the
// `NAPOT` or `TOR` addressing mode constraints:

/// The EarlGrey SOC's flash memory region address range.
///
/// Configured in the PMP as a `NAPOT` region.
#[derive(Copy, Clone, Debug)]
pub struct FlashRegion(pub NAPOTRegionSpec);

/// The EarlGrey SOC's RAM region address range.
///
/// Configured in the PMP as a `NAPOT` region.
#[derive(Copy, Clone, Debug)]
pub struct RAMRegion(pub NAPOTRegionSpec);

/// The EarlGrey SOC's MMIO region address range.
///
/// Configured in the PMP as a `NAPOT` region.
#[derive(Copy, Clone, Debug)]
pub struct MMIORegion(pub NAPOTRegionSpec);

/// The EarlGrey SOC's PMP region specification for the kernel `.text` section.
///
/// This is to be made accessible to machine-mode as read-execute. Configured in
/// the PMP as a `TOR` region.
#[derive(Copy, Clone, Debug)]
pub struct KernelTextRegion(pub TORRegionSpec);

/// The EarlGrey SOC's RISC-V Debug Manager memory region.
///
/// Configured in the PMP as a read/write/execute `NAPOT` region. Because R/W/X
/// regions are not supported in machine-mode lockdown (MML) mode, to enable
/// JTAG debugging, the generic [`EPMPDebugConfig`] argument must be set to
/// [`EPMPDebugEnable`], which will configure the ePMP to operate in non
/// machine-mode lockdown (MML), but still machine-mode whitelist policy (MMWP),
/// instead.
#[derive(Copy, Clone, Debug)]
pub struct RVDMRegion(pub NAPOTRegionSpec);

// ---------- EarlGrey SoC ePMP JTAG Debugging Configuration -------------------

/// EarlGrey SoC ePMP JTAG Debugging Configuration
///
/// The EarlGrey SoC includes a RISC-V Debug Manager mapped to a NAPOT-aligned
/// memory region. To use a JTAG-debugger with the EarlGrey SoC, this region
/// needs to be allowed as R/W/X in the ePMP, at least for machine-mode.
/// However, the RISC-V ePMP does not support R/W/X regions when in machine-mode
/// lockdown (MML) mode. Furthermore, with the machine-mode whitelist policy
/// (MMWP) enabled, machine-mode (the kernel) must be given explicit access for
/// any memory regions to be accessed.
///
/// Thus, to enable debugger access, the following changes have to be made in
/// the EarlGrey ePMP from its default locked-down configuration:
///
/// - Machine-Mode Lockdown (MML) must not be enabled
///
/// - A locked (machine-mode) PMP memory region must be allocated for the RISC-V
///   Debug Manager (RVDM) allocated, and be given R/W/X permissions.
///
/// - Locked regions are enforced & locked for both machine-mode and
///   user-mode. This means that we can no longer use locked regions in
///   combination with the machine-mode whitelist policy to take away access
///   permissions from user-mode. This means that we need to place all user-mode
///   regions as non-locked regions _in front of_ all locked machine-mode
///   regions, and insert a "deny-all" non-locked fallback user-mode region in
///   between to achieve our desired isolation properties.
///
/// As a consequence, because of this "deny-all" user-mode region, we have one
/// fewer memory regions available to be used as a userspace MPU.
///
/// Because all of this is much too complex to implement at runtime (and can't
/// be reconfigured at runtime once MML is configured), we define a new trait
/// [`EPMPDebugConfig`] with two implementations [`EPMPDebugEnable`] and
/// [`EPMPDebugDisable`]. The EPMP implementation is generic over those traits
/// and can, for instance, advertise a different number of MPU regions available
/// for userspace. It further contains a method to retrieve the RVDM memory
/// region's NAPOT address specification irrespective of whether the debug
/// memory is enabled, and an associated constant to use in the configuration
/// code (such that the branches not taken can be optimized out).
pub trait EPMPDebugConfig {
    /// Whether the debug port shall be enabled or not.
    const DEBUG_ENABLE: bool;

    /// How many userspace MPU (TOR) regions are available under this
    /// configuration.
    const TOR_USER_REGIONS: usize;

    /// The offset where the user-mode TOR PMP entries start. This counts
    /// "entries", meaning `pmpaddrX` registers. A single "TOR region" uses two
    /// consecutive "entries".
    const TOR_USER_ENTRIES_OFFSET: usize;
}

pub enum EPMPDebugEnable {}
impl EPMPDebugConfig for EPMPDebugEnable {
    const DEBUG_ENABLE: bool = true;
    const TOR_USER_REGIONS: usize = TOR_USER_REGIONS_DEBUG_ENABLE;
    const TOR_USER_ENTRIES_OFFSET: usize = TOR_USER_ENTRIES_OFFSET_DEBUG_ENABLE;
}

pub enum EPMPDebugDisable {}
impl EPMPDebugConfig for EPMPDebugDisable {
    const DEBUG_ENABLE: bool = false;
    const TOR_USER_REGIONS: usize = TOR_USER_REGIONS_DEBUG_DISABLE;
    const TOR_USER_ENTRIES_OFFSET: usize = TOR_USER_ENTRIES_OFFSET_DEBUG_DISABLE;
}

/// EarlGrey ePMP Configuration Errors
#[derive(Debug, Copy, Clone)]
pub enum EarlGreyEPMPError {
    /// The ePMP driver cannot be instantiated because of an unexpected
    /// `mseccfg` register value.
    InvalidInitialMseccfgValue,
    /// The ePMP driver cannot be instantiated because of an unexpected `pmpcfg`
    /// register value (where the `usize` value contains the index of the
    /// `pmpcfg` register).
    InvalidInitialPmpcfgValue(usize),
    /// The ePMP registers do not match their expected values after
    /// configuration. The system cannot be assumed to be in a secure state.
    SanityCheckFail,
}

/// RISC-V ePMP memory protection implementation for the EarlGrey SoC.
///
/// The EarlGrey ePMP implementation hard-codes many assumptions about the
/// behavior and state of the underlying hardware, to reduce complexity of this
/// codebase, and improve its security, reliability and auditability.
///
/// Namely, it makes and checks assumptions about the machine security policy
/// prior to its initialization routine, locks down the hardware through a
/// static set of PMP configuration steps, and then exposes a subset of regions
/// for user-mode protection through the `PMPUserMPU` trait.
///
/// The EarlGrey ePMP implementation supports JTAG debug-port access through the
/// integrated RISC-V Debug Manger (RVDM) core, which requires R/W/X-access to a
/// given region of memory in machine-mode and user-mode. The [`EarlGreyEPMP`]
/// struct accepts a generic [`EPMPDebugConfig`] implementation, which either
/// enables (in the case of [`EPMPDebugEnable`]) or disables
/// ([`EPMPDebugDisable`]) the debug-port access. However, enabling debug-port
/// access can potentially weaken the system's security by not enabling
/// machine-mode lockdown (MML), and uses an additional PMP region otherwise
/// available to userspace. See the documentation of [`EPMPDebugConfig`] for
/// more information on this.
///
/// ## ePMP Region Layout & Configuration (`EPMPDebugDisable` mode)
///
/// Because of the machine-mode lockdown (MML) mode, no region can have R/W/X
/// permissions. The machine-mode whitelist policy (MMWP) further requires all
/// memory accessed by machine-mode to have a corresponding locked PMP entry
/// defined. Lower-indexed PMP entires have precedence over entries with higher
/// indices. Under MML mode, a non-locked (user-mode) entry prevents
/// machine-mode access to that memory. Thus, the ePMP is to be configured in a
/// "sandwiched" layout (with decreasing precedence):
///
/// 1. High-priority machine-mode "lockdown" entries.
///
///    These entries are only accessible to machine mode. Once locked, they can
///    only be changed through a hart reset. Examples for such memory sections
///    can be the kernel's `.text` or certain RAM (e.g. stack) sections.
///
/// 2. Tock's user-mode "MPU"
///
///    This section defines entries corresponding to memory sections made
///    accessible to user-mode. These entires are exposed through the
///    implementation of the `TORUserPMP` trait.
///
///    **Effectively, this is Tock's "MPU" sandwiched in between the
///    high-priority and low-priority PMP sections.**
///
///    These entires are not locked and must be turned off prior to the kernel
///    being able to access them.
///
///    This section must take precende over the lower kernel-mode entries, as
///    these entries are aliased by the lower kernel-mode entries. Having a
///    locked machine-mode entry take precende over an alias a user-space one
///    prevents user-mode from accessing the aliased memory.
///
/// 3. Low-priority machine-mode "accessability" entires.
///
///    These entires provide the kernel access to memory regions which are
///    (partially) aliased by user-mode regions above. This allows for
///    implementing memory sharing between userspace and the kernel (moving
///    acccess to user-mode by turning on a region above, and falling back onto
///    these rules when turning the user-mode region off).
///
///    These regions can be granular (e.g. grant R/W on the entire RAM), but
///    should not provide any excess permissions where not required (e.g.  avoid
///    granting R/X on flash-memory where only R is required, because the
///    kernel-text is already marked as R/X in the high-priority regions above.
///
/// Because the ROM_EXT and test ROM set up different ePMP configs, there are
/// separate initialization routines (`new` and `new_test_rom`) for those
/// environments.
///
/// `new` (only available when the debug-port is disabled) attempts to set up
/// the following memory protection rules and layout:
///
/// - `msseccfg` CSR:
///
///   ```text
///   |-----+-----------------------------------------------------------+-------|
///   | BIT | LABEL                                                     | STATE |
///   |-----+-----------------------------------------------------------+-------|
///   |   0 | Machine-Mode Lockdown (MML)                               |     1 |
///   |   1 | Machine-Mode Whitelist Policy (MMWP)                      |     1 |
///   |   2 | Rule-Lock Bypass (RLB)                                    |     0 |
///   |-----+-----------------------------------------------------------+-------|
///   ```
///
/// - `pmpcfgX` / `pmpaddrX` CSRs:
///
///   ```text
///   |-------+----------------------------------------+-----------+---+-------|
///   | ENTRY | REGION / ADDR                          | MODE      | L | PERMS |
///   |-------+----------------------------------------+-----------+---+-------|
///   |     0 | Locked by the ROM_EXT or unused        | NAPOT/OFF | X |       |
///   |       |                                        |           |   |       |
///   |     1 | Locked by the ROM_EXT or unused        | NAPOT/OFF | X |       |
///   |       |                                        |           |   |       |
///   |     2 | -------------------------------------- | OFF       | X | ----- |
///   |     3 | Kernel .text section                   | TOR       | X | R/X   |
///   |       |                                        |           |   |       |
///   |     4 | /                                    \ | OFF       |   |       |
///   |     5 | \ Userspace TOR region #0            / | TOR       |   | ????? |
///   |       |                                        |           |   |       |
///   |     6 | /                                    \ | OFF       |   |       |
///   |     7 | \ Userspace TOR region #1            / | TOR       |   | ????? |
///   |       |                                        |           |   |       |
///   |     8 | /                                    \ | OFF       |   |       |
///   |     9 | \ Userspace TOR region #2            / | TOR       |   | ????? |
///   |       |                                        |           |   |       |
///   |    10 | /                                    \ | OFF       |   |       |
///   |    11 | \ Userspace TOR region #3            / | TOR       |   | ????? |
///   |       |                                        |           |   |       |
///   |    12 | FLASH (spanning kernel & apps)         | NAPOT     | X | R     |
///   |       |                                        |           |   |       |
///   |    13 | -------------------------------------- | OFF       | X | ----- |
///   |       |                                        |           |   |       |
///   |    14 | RAM (spanning kernel & apps)           | NAPOT     | X | R/W   |
///   |       |                                        |           |   |       |
///   |    15 | MMIO                                   | NAPOT     | X | R/W   |
///   |-------+----------------------------------------+-----------+---+-------|
///   ```
///
/// `new_test_rom` (only available when the debug-port is disabled) attempts to
/// set up the following memory protection rules and layout:
///
/// - `msseccfg` CSR:
///
///   ```text
///   |-----+-----------------------------------------------------------+-------|
///   | BIT | LABEL                                                     | STATE |
///   |-----+-----------------------------------------------------------+-------|
///   |   0 | Machine-Mode Lockdown (MML)                               |     1 |
///   |   1 | Machine-Mode Whitelist Policy (MMWP)                      |     1 |
///   |   2 | Rule-Lock Bypass (RLB)                                    |     0 |
///   |-----+-----------------------------------------------------------+-------|
///   ```
///
/// - `pmpcfgX` / `pmpaddrX` CSRs:
///
///   ```text
///   |-------+---------------------------------------------+-------+---+-------|
///   | ENTRY | REGION / ADDR                               | MODE  | L | PERMS |
///   |-------+---------------------------------------------+-------+---+-------|
///   |     0 | ------------------------------------------- | OFF   | X | ----- |
///   |     1 | Kernel .text section                        | TOR   | X | R/X   |
///   |       |                                             |       |   |       |
///   |     2 | ------------------------------------------- | OFF   | X |       |
///   |       |                                             |       |   |       |
///   |     3 | ------------------------------------------- | OFF   | X |       |
///   |       |                                             |       |   |       |
///   |     4 | /                                         \ | OFF   |   |       |
///   |     5 | \ Userspace TOR region #0                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     6 | /                                         \ | OFF   |   |       |
///   |     7 | \ Userspace TOR region #1                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     8 | /                                         \ | OFF   |   |       |
///   |     9 | \ Userspace TOR region #2                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |    10 | /                                         \ | OFF   |   |       |
///   |    11 | \ Userspace TOR region #3                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |    12 | ------------------------------------------- | OFF   | X | ----- |
///   |       |                                             |       |   |       |
///   |    13 | FLASH (spanning kernel & apps)              | NAPOT | X | R     |
///   |       |                                             |       |   |       |
///   |    14 | RAM (spanning kernel & apps)                | NAPOT | X | R/W   |
///   |       |                                             |       |   |       |
///   |    15 | MMIO                                        | NAPOT | X | R/W   |
///   |-------+---------------------------------------------+-------+---+-------|
///   ```
///
/// ## ePMP Region Layout & Configuration (`EPMPDebugEnable` mode)
///
/// When enabling the RISC-V Debug Manager (JTAG debug port), the ePMP must be
/// configured differently. This is because the `RVDM` requires a memory section
/// to be mapped with read-write-execute privileges, which is not possible under
/// the machine-mode lockdown (MML) mode. However, when simply disabling MML in
/// the above policy, it would grant userspace access to kernel memory through
/// the locked PMP entires. We still need to define locked PMP entries to grant
/// the kernel (machine-mode) access to its required memory regions, as the
/// machine-mode whitelist policy (MMWP) is enabled.
///
/// Thus we split the PMP entires into three parts, as outlined in the
/// following:
///
/// 1. Tock's user-mode "MPU"
///
///    This section defines entries corresponding to memory sections made
///    accessible to user-mode. These entires are exposed through the
///    implementation of the `TORUserPMP` trait.
///
///    These entires are not locked. Because the machine-mode lockdown (MML)
///    mode is not enabled, non-locked regions are ignored in machine-mode. The
///    kernel does not have to disable these entires prior to being able to
///    access them.
///
///    This section must take precende over the lower kernel-mode entries, as
///    these entries are aliased by the lower kernel-mode entries. Having a
///    locked machine-mode entry take precende over an alias a user-space one
///    prevents user-mode from accessing the aliased memory.
///
/// 2. User-mode "deny-all" rule.
///
///    Without machine-mode lockdown (MML) mode, locked regions apply to both
///    user- and kernel-mode. Because the machine-mode whitelist policy (MMWP)
///    is enabled, the kernel must be granted explicit permission to access
///    memory (default-deny policy). This means that we must prevent any
///    user-mode access from "falling through" to kernel-mode regions. For this
///    purpose, we insert a non-locked "deny-all" rule which disallows all
///    user-mode accesses to the entire address space, if no other
///    higher-priority user-mode rule matches.
///
/// 3. Machine-mode "accessability" entires.
///
///    These entires provide the kernel access to certain memory regions, as
///    required by the machine-mode whitelist policy (MMWP).
///
/// `new_debug` (only available when the debug-port is enabled) attempts to set
/// up the following memory protection rules and layout:
///
/// - `msseccfg` CSR:
///
///   ```text
///   |-----+-----------------------------------------------------------+-------|
///   | BIT | LABEL                                                     | STATE |
///   |-----+-----------------------------------------------------------+-------|
///   |   0 | Machine-Mode Lockdown (MML)                               |     0 |
///   |   1 | Machine-Mode Whitelist Policy (MMWP)                      |     1 |
///   |   2 | Rule-Lock Bypass (RLB)                                    |     0 |
///   |-----+-----------------------------------------------------------+-------|
///   ```
///
/// - `pmpcfgX` / `pmpaddrX` CSRs:
///
///   ```text
///   |-------+---------------------------------------------+-------+---+-------|
///   | ENTRY | REGION / ADDR                               | MODE  | L | PERMS |
///   |-------+---------------------------------------------+-------+---+-------|
///   |     0 | /                                         \ | OFF   |   |       |
///   |     1 | \ Userspace TOR region #0                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     2 | /                                         \ | OFF   |   |       |
///   |     3 | \ Userspace TOR region #1                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     4 | /                                         \ | OFF   |   |       |
///   |     5 | \ Userspace TOR region #2                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     6 | /                                         \ | OFF   |   |       |
///   |     7 | \ Userspace TOR region #3                 / | TOR   |   | ????? |
///   |       |                                             |       |   |       |
///   |     8 | ------------------------------------------- | OFF   |   | ----- |
///   |       |                                             |       |   |       |
///   |     9 | "Deny-all" user-mode rule (all memory)      | NAPOT |   | ----- |
///   |       |                                             |       |   |       |
///   |    10 | ------------------------------------------- | OFF   | X | ----- |
///   |    11 | Kernel .text section                        | TOR   | X | R/X   |
///   |       |                                             |       |   |       |
///   |    12 | RVDM Debug Core Memory                      | NAPOT | X | R/W/X |
///   |       |                                             |       |   |       |
///   |    13 | FLASH (spanning kernel & apps)              | NAPOT | X | R     |
///   |       |                                             |       |   |       |
///   |    14 | RAM (spanning kernel & apps)                | NAPOT | X | R/W   |
///   |       |                                             |       |   |       |
///   |    15 | MMIO                                        | NAPOT | X | R/W   |
///   |-------+---------------------------------------------+-------+---+-------|
///   ```
pub struct EarlGreyEPMP<const HANDOVER_CONFIG_CHECK: bool, DBG: EPMPDebugConfig> {
    user_pmp_enabled: Cell<bool>,
    // We can't use our generic parameter to determine the length of the
    // TORUserPMPCFG array (missing `generic_const_exprs` feature). Thus we
    // always assume that the debug-port is disabled and we can fit
    // `TOR_USER_REGIONS_DEBUG_DISABLE` user-mode TOR regions.
    shadow_user_pmpcfgs: [Cell<TORUserPMPCFG>; TOR_USER_REGIONS_DEBUG_DISABLE],
    _pd: PhantomData<DBG>,
}

impl<const HANDOVER_CONFIG_CHECK: bool> EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, EPMPDebugDisable> {
    pub unsafe fn new(
        flash: FlashRegion,
        ram: RAMRegion,
        mmio: MMIORegion,
        kernel_text: KernelTextRegion,
    ) -> Result<Self, EarlGreyEPMPError> {
        use kernel::utilities::registers::interfaces::{Readable, Writeable};

        // --> We start with the "high-priority" ("lockdown") section of the
        // ePMP configuration:

        // Provide R/X access to the kernel .text as passed to us above.
        // Allocate a TOR region in PMP entries 2 and 3:
        csr::CSR.pmpaddr2.set((kernel_text.0.start() as usize) >> 2);
        csr::CSR.pmpaddr3.set((kernel_text.0.end() as usize) >> 2);

        // Set the appropriate `pmpcfg0` register value:
        //
        // 0x80 = 0b10000000, for start the address of the kernel .text TOR
        //        entry as well as entries 0 and 1.
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x8d = 0b10001101, for kernel .text TOR region
        //        setting L(7) = 1, A(4-3) = TOR,   X(2) = 1, W(1) = 0, R(0) = 1
        //
        // Note that we try to lock entries 0 and 1 into OFF mode. If the
        // ROM_EXT set these up and locked them, this will do nothing, otherwise
        // it will permanently disable these entries (preventing them from being
        // misused later).
        csr::CSR.pmpcfg0.set(0x8d_80_80_80);

        // --> Continue with the "low-priority" ("accessibility") section of the
        // ePMP configuration:

        // Configure a Read-Only NAPOT region for the entire flash (spanning
        // kernel & apps, but overlayed by the R/X kernel text TOR section)
        csr::CSR.pmpaddr12.set(flash.0.napot_addr());

        // Configure a Read-Write NAPOT region for MMIO.
        csr::CSR.pmpaddr14.set(mmio.0.napot_addr());

        // Configure a Read-Write NAPOT region for the entire RAM (spanning
        // kernel & apps)
        csr::CSR.pmpaddr15.set(ram.0.napot_addr());

        // With the FLASH, RAM and MMIO configured in separate regions, we can
        // activate this new configuration, and further adjust the permissions
        // of the (currently all-capable) last PMP entry `pmpaddr15` to be R/W,
        // as required for MMIO:
        //
        // 0x99 = 0b10011001, for FLASH NAPOT region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 1
        //
        // 0x80 = 0b10000000, for the unused region
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x9B = 0b10011011, for RAM & MMIO NAPOT regions
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 1, R(0) = 1
        csr::CSR.pmpcfg3.set(0x9B_9B_80_99);

        // Ensure that the other pmpcfgX CSRs are cleared:
        csr::CSR.pmpcfg1.set(0x00000000);
        csr::CSR.pmpcfg2.set(0x00000000);

        // ---------- PMP machine CSRs configured, lock down the system

        // Finally, enable machine-mode lockdown.
        // Set RLB(2) = 0, MMWP(1) = 1, MML(0) = 1
        csr::CSR.mseccfg.set(0x00000003);

        // ---------- System locked down, cross-check config

        // Now, cross-check that the CSRs have the expected values. This acts as
        // a sanity check, and can also help to protect against some set of
        // fault-injection attacks. These checks can't be optimized out by the
        // compiler, as they invoke assembly underneath which is not marked as
        // ["pure"](https://doc.rust-lang.org/reference/inline-assembly.html).
        //
        // Note that different ROM_EXT versions configure entries 0 and 1
        // differently, so we only confirm they are locked here.
        if csr::CSR.mseccfg.get() != 0x00000003
            || (csr::CSR.pmpcfg0.get() & 0xFFFF8080) != 0x8d808080
            || csr::CSR.pmpcfg1.get() != 0x00000000
            || csr::CSR.pmpcfg2.get() != 0x00000000
            || csr::CSR.pmpcfg3.get() != 0x9B9B8099
            || csr::CSR.pmpaddr2.get() != (kernel_text.0.start() as usize) >> 2
            || csr::CSR.pmpaddr3.get() != (kernel_text.0.end() as usize) >> 2
            || csr::CSR.pmpaddr12.get() != flash.0.napot_addr()
            || csr::CSR.pmpaddr14.get() != mmio.0.napot_addr()
            || csr::CSR.pmpaddr15.get() != ram.0.napot_addr()
        {
            return Err(EarlGreyEPMPError::SanityCheckFail);
        }

        // The ePMP hardware was correctly configured, build the ePMP struct:
        const DEFAULT_USER_PMPCFG_OCTET: Cell<TORUserPMPCFG> = Cell::new(TORUserPMPCFG::OFF);
        Ok(EarlGreyEPMP {
            user_pmp_enabled: Cell::new(false),
            shadow_user_pmpcfgs: [DEFAULT_USER_PMPCFG_OCTET; TOR_USER_REGIONS_DEBUG_DISABLE],
            _pd: PhantomData,
        })
    }

    pub unsafe fn new_test_rom(
        flash: FlashRegion,
        ram: RAMRegion,
        mmio: MMIORegion,
        kernel_text: KernelTextRegion,
    ) -> Result<Self, EarlGreyEPMPError> {
        use kernel::utilities::registers::interfaces::{Readable, Writeable};

        if HANDOVER_CONFIG_CHECK {
            Self::check_initial_hardware_config()?;
        } else {
            // We aren't supposed to run a handover configuration check. This is
            // useful for environments which don't replicate the OpenTitan
            // EarlGrey chip behavior entirely accurately, such as
            // QEMU. However, in those environments, we cannot guarantee that
            // this configuration is actually going to work, and not break the
            // system in the meantime.
            //
            // We perform a best-effort configuration, starting by setting rule-lock
            // bypass...
            csr::CSR.mseccfg.set(0x00000004);
            // ...adding our required kernel-mode mode memory access rule...
            csr::CSR.pmpaddr15.set(0x7FFFFFFF);
            csr::CSR.pmpcfg3.set(0x9F000000);
            // ...and enabling the machine-mode whitelist policy:
            csr::CSR.mseccfg.set(0x00000006);
        }

        // ---------- HW configured as expected, start setting PMP CSRs

        // The below instructions are an intricate dance to achieve our desired
        // ePMP configuration. For correctness sake, we -- at no intermediate
        // point -- want to lose access to RAM, FLASH or MMIO.
        //
        // This is challenging, as the last section currently provides us access
        // to all of these regions, and we can't atomically change both its
        // pmpaddrX and pmpcfgX CSRs to limit it to a subset of its address
        // range and permissions. Thus, before changing the `pmpcfg3` /
        // `pmpaddr15` region, we first utilize another higher-priority CSR to
        // provide us access to one of the memory regions we'd lose access to,
        // namely we use the PMP entry 12 to provide us access to MMIO.

        // --> We start with the "high-priority" ("lockdown") section of the
        // ePMP configuration:

        // Provide R/X access to the kernel .text as passed to us above.
        // Allocate a TOR region in PMP entries 0 and 1:
        csr::CSR.pmpaddr0.set((kernel_text.0.start() as usize) >> 2);
        csr::CSR.pmpaddr1.set((kernel_text.0.end() as usize) >> 2);

        // Set the appropriate `pmpcfg0` register value:
        //
        // 0x80 = 0b10000000, for start address of the kernel .text TOR entry
        //        and to disable regions 2 & 3 (to be compatible with the
        //        non-test-rom constructor).
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x8d = 0b10001101, for kernel .text TOR region
        //        setting L(7) = 1, A(4-3) = TOR,   X(2) = 1, W(1) = 0, R(0) = 1
        csr::CSR.pmpcfg0.set(0x80808d80);

        // --> Continue with the "low-priority" ("accessability") section of the
        // ePMP configuration:

        // Now, onto `pmpcfg3`. As discussed above, we want to use a temporary
        // region to retain MMIO access while reconfiguring the `pmpcfg3` /
        // `pmpaddr15` register. Thus, write the MMIO region access into
        // `pmpaddr12`:
        csr::CSR.pmpaddr12.set(mmio.0.napot_addr());

        // Configure a Read-Only NAPOT region for the entire flash (spanning
        // kernel & apps, but overlayed by the R/X kernel text TOR section)
        csr::CSR.pmpaddr13.set(flash.0.napot_addr());

        // Configure a Read-Write NAPOT region for the entire RAM (spanning
        // kernel & apps)
        csr::CSR.pmpaddr14.set(ram.0.napot_addr());

        // With the FLASH, RAM and MMIO configured in separate regions, we can
        // activate this new configuration, and further adjust the permissions
        // of the (currently all-capable) last PMP entry `pmpaddr15` to be R/W,
        // as required for MMIO:
        //
        // 0x99 = 0b10011001, for FLASH NAPOT region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 1
        //
        // 0x9B = 0b10011011, for RAM & MMIO NAPOT regions
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 1, R(0) = 1
        csr::CSR.pmpcfg3.set(0x9B9B999B);

        // With the new configuration in place, we can adjust the last region's
        // address to be limited to the MMIO region, ...
        csr::CSR.pmpaddr15.set(mmio.0.napot_addr());

        // ...and then deactivate the `pmpaddr12` fallback MMIO region
        //
        // Remove the temporary MMIO region permissions from `pmpaddr12`:
        //
        // 0x80 = 0b10000000
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x99 = 0b10011001, for FLASH NAPOT region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 1
        //
        // 0x9B = 0b10011011, for RAM & MMIO NAPOT regions
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 1, R(0) = 1
        csr::CSR.pmpcfg3.set(0x9B9B9980);

        // Ensure that the other pmpcfgX CSRs are cleared:
        csr::CSR.pmpcfg1.set(0x00000000);
        csr::CSR.pmpcfg2.set(0x00000000);

        // ---------- PMP machine CSRs configured, lock down the system

        // Finally, unset the rule-lock bypass (RLB) bit. If we don't have a
        // debug memory region provided, further set machine-mode lockdown (we
        // can't enable MML and also have a R/W/X region). We also set MMWP for
        // good measure, but that shouldn't make a difference -- it can't be
        // cleared anyways as it is a sticky bit.
        //
        // Unsetting RLB with at least one locked region will mean that we can't
        // set it again, thus actually enforcing the region lock bits.
        //
        // Set RLB(2) = 0, MMWP(1) = 1, MML(0) = 1
        csr::CSR.mseccfg.set(0x00000003);

        // ---------- System locked down, cross-check config

        // Now, cross-check that the CSRs have the expected values. This acts as
        // a sanity check, and can also help to protect against some set of
        // fault-injection attacks. These checks can't be optimized out by the
        // compiler, as they invoke assembly underneath which is not marked as
        // ["pure"](https://doc.rust-lang.org/reference/inline-assembly.html).
        if csr::CSR.mseccfg.get() != 0x00000003
            || csr::CSR.pmpcfg0.get() != 0x00008d80
            || csr::CSR.pmpcfg1.get() != 0x00000000
            || csr::CSR.pmpcfg2.get() != 0x00000000
            || csr::CSR.pmpcfg3.get() != 0x9B9B9980
            || csr::CSR.pmpaddr0.get() != (kernel_text.0.start() as usize) >> 2
            || csr::CSR.pmpaddr1.get() != (kernel_text.0.end() as usize) >> 2
            || csr::CSR.pmpaddr13.get() != flash.0.napot_addr()
            || csr::CSR.pmpaddr14.get() != ram.0.napot_addr()
            || csr::CSR.pmpaddr15.get() != mmio.0.napot_addr()
        {
            return Err(EarlGreyEPMPError::SanityCheckFail);
        }

        // The ePMP hardware was correctly configured, build the ePMP struct:
        const DEFAULT_USER_PMPCFG_OCTET: Cell<TORUserPMPCFG> = Cell::new(TORUserPMPCFG::OFF);
        Ok(EarlGreyEPMP {
            user_pmp_enabled: Cell::new(false),
            shadow_user_pmpcfgs: [DEFAULT_USER_PMPCFG_OCTET; TOR_USER_REGIONS_DEBUG_DISABLE],
            _pd: PhantomData,
        })
    }
}

impl<const HANDOVER_CONFIG_CHECK: bool> EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, EPMPDebugEnable> {
    pub unsafe fn new_debug(
        flash: FlashRegion,
        ram: RAMRegion,
        mmio: MMIORegion,
        kernel_text: KernelTextRegion,
        debug_memory: RVDMRegion,
    ) -> Result<Self, EarlGreyEPMPError> {
        use kernel::utilities::registers::interfaces::{Readable, Writeable};

        if HANDOVER_CONFIG_CHECK {
            Self::check_initial_hardware_config()?;
        } else {
            // We aren't supposed to run a handover configuration check. This is
            // useful for environments which don't replicate the OpenTitan
            // EarlGrey chip behavior entirely accurately, such as
            // QEMU. However, in those environments, we cannot guarantee that
            // this configuration is actually going to work, and not break the
            // system in the meantime.
            //
            // We perform a best-effort configuration, starting by setting rule-lock
            // bypass...
            csr::CSR.mseccfg.set(0x00000004);
            // ...adding our required kernel-mode mode memory access rule...
            csr::CSR.pmpaddr15.set(0x7FFFFFFF);
            csr::CSR.pmpcfg3.set(0x9F000000);
            // ...and enabling the machine-mode whitelist policy:
            csr::CSR.mseccfg.set(0x00000006);
        }

        // ---------- HW configured as expected, start setting PMP CSRs

        // The below instructions are an intricate dance to achieve our desired
        // ePMP configuration. For correctness sake, we -- at no intermediate
        // point -- want to lose access to RAM, FLASH or MMIO.
        //
        // This is challenging, as the last section currently provides us access
        // to all of these regions, and we can't atomically change both its
        // pmpaddrX and pmpcfgX CSRs to limit it to a subset of its address
        // range and permissions. Thus, before changing the `pmpcfg3` /
        // `pmpaddr15` region, we first utilize another higher-priority CSR to
        // provide us access to one of the memory regions we'd lose access to,
        // namely we use the PMP entry 12 to provide us access to MMIO.

        // Provide R/X access to the kernel .text as passed to us above.
        // Allocate a TOR region in PMP entries 10 and 11:
        csr::CSR
            .pmpaddr10
            .set((kernel_text.0.start() as usize) >> 2);
        csr::CSR.pmpaddr11.set((kernel_text.0.end() as usize) >> 2);

        // Set the appropriate `pmpcfg2` register value:
        //
        // 0x80 = 0b10000000, for start address of the kernel .text TOR entry
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x8d = 0b10001101, for kernel .text TOR region
        //        setting L(7) = 1, A(4-3) = TOR,   X(2) = 1, W(1) = 0, R(0) = 1
        csr::CSR.pmpcfg2.set(0x8d800000);

        // Now, onto `pmpcfg3`. As discussed above, we want to use a temporary
        // region to retain MMIO access while reconfiguring the `pmpcfg3` /
        // `pmpaddr15` register. Thus, write the MMIO region access into
        // `pmpaddr12`:
        csr::CSR.pmpaddr12.set(mmio.0.napot_addr());

        // Configure a Read-Only NAPOT region for the entire flash (spanning
        // kernel & apps, but overlayed by the R/X kernel text TOR section)
        csr::CSR.pmpaddr13.set(flash.0.napot_addr());

        // Configure a Read-Write NAPOT region for the entire RAM (spanning
        // kernel & apps)
        csr::CSR.pmpaddr14.set(ram.0.napot_addr());

        // With the FLASH, RAM and MMIO configured in separate regions, we can
        // activate this new configuration, and further adjust the permissions
        // of the (currently all-capable) last PMP entry `pmpaddr15` to be R/W,
        // as required for MMIO:
        //
        // 0x99 = 0b10011001, for FLASH NAPOT region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 1
        //
        // 0x9B = 0b10011011, for RAM & MMIO NAPOT regions
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 1, R(0) = 1
        csr::CSR.pmpcfg3.set(0x9B9B999B);

        // With the new configuration in place, we can adjust the last region's
        // address to be limited to the MMIO region, ...
        csr::CSR.pmpaddr15.set(mmio.0.napot_addr());

        // ...and then repurpose `pmpaddr12` for the debug port:
        csr::CSR.pmpaddr12.set(debug_memory.0.napot_addr());

        // 0x9F = 0b10011111, for RVDM R/W/X memory region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 1, W(1) = 1, R(0) = 1
        //
        // 0x99 = 0b10011001, for FLASH NAPOT region
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 1
        //
        // 0x9B = 0b10011011, for RAM & MMIO NAPOT regions
        //        setting L(7) = 1, A(4-3) = NAPOT, X(2) = 0, W(1) = 1, R(0) = 1
        csr::CSR.pmpcfg3.set(0x9B9B999F);

        // Ensure that the other pmpcfgX CSRs are cleared:
        csr::CSR.pmpcfg0.set(0x00000000);
        csr::CSR.pmpcfg1.set(0x00000000);

        // ---------- PMP machine CSRs configured, lock down the system

        // Finally, unset the rule-lock bypass (RLB) bit. If we don't have a
        // debug memory region provided, further set machine-mode lockdown (we
        // can't enable MML and also have a R/W/X region). We also set MMWP for
        // good measure, but that shouldn't make a difference -- it can't be
        // cleared anyways as it is a sticky bit.
        //
        // Unsetting RLB with at least one locked region will mean that we can't
        // set it again, thus actually enforcing the region lock bits.
        //
        // Set RLB(2) = 0, MMWP(1) = 1, MML(0) = 0
        csr::CSR.mseccfg.set(0x00000002);

        // ---------- System locked down, cross-check config

        // Now, cross-check that the CSRs have the expected values. This acts as
        // a sanity check, and can also help to protect against some set of
        // fault-injection attacks. These checks can't be optimized out by the
        // compiler, as they invoke assembly underneath which is not marked as
        // ["pure"](https://doc.rust-lang.org/reference/inline-assembly.html).
        if csr::CSR.mseccfg.get() != 0x00000002
            || csr::CSR.pmpcfg0.get() != 0x00000000
            || csr::CSR.pmpcfg1.get() != 0x00000000
            || csr::CSR.pmpcfg2.get() != 0x8d800000
            || csr::CSR.pmpcfg3.get() != 0x9B9B999F
            || csr::CSR.pmpaddr10.get() != (kernel_text.0.start() as usize) >> 2
            || csr::CSR.pmpaddr11.get() != (kernel_text.0.end() as usize) >> 2
            || csr::CSR.pmpaddr12.get() != debug_memory.0.napot_addr()
            || csr::CSR.pmpaddr13.get() != flash.0.napot_addr()
            || csr::CSR.pmpaddr14.get() != ram.0.napot_addr()
            || csr::CSR.pmpaddr15.get() != mmio.0.napot_addr()
        {
            return Err(EarlGreyEPMPError::SanityCheckFail);
        }

        // Now, as we're not in the machine-mode lockdown (MML) mode, locked PMP
        // regions will still be accessible to userspace. To prevent our
        // kernel-mode access regions from being accessible to user-mode, we use
        // the last user-mode TOR region (`pmpaddr9`) to configure a
        // "protection" region which disallows access to all memory that has not
        // otherwise been granted access to.
        csr::CSR.pmpaddr9.set(0x7FFFFFFF); // the entire address space

        // And finally apply this configuration to the `pmpcfg2` CSR. For good
        // measure, we also include the locked regions (which we can no longer
        // modify thanks to RLB = 0).
        //
        // 0x18 = 0b00011000, to revoke user-mode perms to all memory
        //        setting L(7) = 0, A(4-3) = NAPOT, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x80 = 0b10000000, for start address of the kernel .text TOR entry
        //        setting L(7) = 1, A(4-3) = OFF, X(2) = 0, W(1) = 0, R(0) = 0
        //
        // 0x8d = 0b10001101, for kernel .text TOR region
        //        setting L(7) = 1, A(4-3) = TOR,   X(2) = 1, W(1) = 0, R(0) = 1
        csr::CSR.pmpcfg2.set(0x8d81800);

        // The ePMP hardware was correctly configured, build the ePMP struct:
        const DEFAULT_USER_PMPCFG_OCTET: Cell<TORUserPMPCFG> = Cell::new(TORUserPMPCFG::OFF);
        let epmp = EarlGreyEPMP {
            user_pmp_enabled: Cell::new(false),
            shadow_user_pmpcfgs: [DEFAULT_USER_PMPCFG_OCTET; TOR_USER_REGIONS_DEBUG_DISABLE],
            _pd: PhantomData,
        };

        Ok(epmp)
    }
}

impl<const HANDOVER_CONFIG_CHECK: bool, DBG: EPMPDebugConfig>
    EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, DBG>
{
    fn check_initial_hardware_config() -> Result<(), EarlGreyEPMPError> {
        use kernel::utilities::registers::interfaces::Readable;

        // This initialization code is written to work with 16 PMP entries. Add
        // an explicit assertion such that things break when the constant above
        // is changed:
        #[allow(clippy::assertions_on_constants)]
        const _: () = assert!(
            PMP_ENTRIES_OVER_TWO == 8,
            "EarlGrey ePMP initialization is written for 16 PMP entries.",
        );

        // ---------- Check current HW config

        // Ensure that the `mseccfg` CSR has the expected value, namely that
        // we're in "machine-mode whitelist policy" and have "rule-lock bypass"
        // enabled. If this register has an unexpected value, we risk
        // accidentally revoking important permissions for the Tock kernel
        // itself.
        if csr::CSR.mseccfg.get() != 0x00000006 {
            return Err(EarlGreyEPMPError::InvalidInitialMseccfgValue);
        }

        // We assume the very last PMP region is set to provide us RXW access to
        // the entirety of memory, and all other regions are disabled. Check the
        // CSRs to make sure that this is indeed the case.
        for i in 0..(PMP_ENTRIES_OVER_TWO / 2 - 1) {
            // 0x98 = 0b10011000, extracting L(7) and A(4-3) bits.
            if csr::CSR.pmpconfig_get(i) & 0x98989898 != 0x00000000 {
                return Err(EarlGreyEPMPError::InvalidInitialPmpcfgValue(i));
            }
        }

        // The last CSR is special, as we expect it to contain the NAPOT region
        // which currently gives us memory access.
        //
        // 0x98 = 0b10011000, extracting L(7) and A(4-3) bits.
        // 0x9F = 0b10011111, extracing L(7), A(4-3), X(2), W(1), R(0) bits.
        if csr::CSR.pmpconfig_get(PMP_ENTRIES_OVER_TWO / 2 - 1) & 0x9F989898 != 0x9F000000 {
            return Err(EarlGreyEPMPError::InvalidInitialPmpcfgValue(
                PMP_ENTRIES_OVER_TWO / 2 - 1,
            ));
        }

        Ok(())
    }

    // ---------- Backing functions for the TORUserPMP implementations ---------
    //
    // The EarlGrey ePMP implementations of `TORUserPMP` differ between
    // `EPMPDebugEnable` and `EPMPDebugDisable` configurations. These backing
    // functions here are applicable to both, and called by those trait
    // implementations respectively:

    fn user_available_regions<const TOR_USER_REGIONS: usize>(&self) -> usize {
        // Always assume to have `TOR_USER_REGIONS` usable TOR regions. We have a
        // fixed number of kernel memory protection regions, and a fixed mapping
        // of user regions to hardware PMP entries.
        TOR_USER_REGIONS
    }

    fn user_configure_pmp<const TOR_USER_REGIONS: usize>(
        &self,
        regions: &[(TORUserPMPCFG, *const u8, *const u8); TOR_USER_REGIONS],
    ) -> Result<(), ()> {
        // Configure all of the regions' addresses and store their pmpcfg octets
        // in our shadow storage. If the user PMP is already enabled, we further
        // apply this configuration (set the pmpcfgX CSRs) by running
        // `enable_user_pmp`:
        for (i, (region, shadow_user_pmpcfg)) in regions
            .iter()
            .zip(self.shadow_user_pmpcfgs.iter())
            .enumerate()
        {
            // The ePMP in MML mode does not support read-write-execute
            // regions. If such a region is to be configured, abort. As this
            // loop here only modifies the shadow state, we can simply abort and
            // return an error. We don't make any promises about the ePMP state
            // if the configuration files, but it is still being activated with
            // `enable_user_pmp`:
            if region.0.get()
                == <TORUserPMPCFG as From<mpu::Permissions>>::from(
                    mpu::Permissions::ReadWriteExecute,
                )
                .get()
            {
                return Err(());
            }

            // Set the CSR addresses for this region (if its not OFF, in which
            // case the hardware-configured addresses are irrelevant):
            if region.0 != TORUserPMPCFG::OFF {
                csr::CSR.pmpaddr_set(
                    DBG::TOR_USER_ENTRIES_OFFSET + (i * 2) + 0,
                    (region.1 as usize).overflowing_shr(2).0,
                );
                csr::CSR.pmpaddr_set(
                    DBG::TOR_USER_ENTRIES_OFFSET + (i * 2) + 1,
                    (region.2 as usize).overflowing_shr(2).0,
                );
            }

            // Store the region's pmpcfg octet:
            shadow_user_pmpcfg.set(region.0);
        }

        // If the PMP is currently active, apply the changes to the CSRs:
        if self.user_pmp_enabled.get() {
            self.user_enable_user_pmp()?;
        }

        Ok(())
    }

    fn user_enable_user_pmp(&self) -> Result<(), ()> {
        // Currently, this code requires the TOR regions to start at an even PMP
        // region index. Assert that this is indeed the case:
        #[allow(clippy::let_unit_value)]
        let _: () = assert!(DBG::TOR_USER_ENTRIES_OFFSET % 2 == 0);

        // We store the "enabled" PMPCFG octets of user regions in the
        // `shadow_user_pmpcfg` field, such that we can re-enable the PMP
        // without a call to `configure_pmp` (where the `TORUserPMPCFG`s are
        // provided by the caller).

        // Could use `iter_array_chunks` once that's stable.
        //
        // Limit iteration to `DBG::TOR_USER_REGIONS` to avoid overwriting any
        // configured debug regions in the last user-mode TOR region.
        let mut shadow_user_pmpcfgs_iter = self.shadow_user_pmpcfgs[..DBG::TOR_USER_REGIONS].iter();
        let mut i = DBG::TOR_USER_ENTRIES_OFFSET / 2;

        while let Some(first_region_pmpcfg) = shadow_user_pmpcfgs_iter.next() {
            // If we're at a "region" offset divisible by two (where "region" =
            // 2 PMP "entries"), then we can configure an entire `pmpcfgX` CSR
            // in one operation. As CSR writes are expensive, this is an
            // operation worth making:
            let second_region_opt = if i % 2 == 0 {
                shadow_user_pmpcfgs_iter.next()
            } else {
                None
            };

            if let Some(second_region_pmpcfg) = second_region_opt {
                // We're at an even index and have two regions to configure, so
                // do that with a single CSR write:
                csr::CSR.pmpconfig_set(
                    i / 2,
                    u32::from_be_bytes([
                        second_region_pmpcfg.get().get(),
                        TORUserPMPCFG::OFF.get(),
                        first_region_pmpcfg.get().get(),
                        TORUserPMPCFG::OFF.get(),
                    ]) as usize,
                );

                i += 2;
            } else if i % 2 == 0 {
                // This is a single region at an even index. Thus, modify the
                // first two pmpcfgX octets for this region.
                csr::CSR.pmpconfig_modify(
                    i / 2,
                    FieldValue::<usize, csr::pmpconfig::pmpcfg::Register>::new(
                        0x0000FFFF,
                        0, // lower two octets
                        u32::from_be_bytes([
                            0,
                            0,
                            first_region_pmpcfg.get().get(),
                            TORUserPMPCFG::OFF.get(),
                        ]) as usize,
                    ),
                );

                i += 1;
            } else {
                // This is a single region at an odd index. Thus, modify the
                // latter two pmpcfgX octets for this region.
                csr::CSR.pmpconfig_modify(
                    i / 2,
                    FieldValue::<usize, csr::pmpconfig::pmpcfg::Register>::new(
                        0x0000FFFF,
                        16, // higher two octets
                        u32::from_be_bytes([
                            0,
                            0,
                            first_region_pmpcfg.get().get(),
                            TORUserPMPCFG::OFF.get(),
                        ]) as usize,
                    ),
                );

                i += 1;
            }
        }

        self.user_pmp_enabled.set(true);

        Ok(())
    }

    fn user_disable_user_pmp(&self) {
        // Simply set all of the user-region pmpcfg octets to OFF:
        let mut user_region_pmpcfg_octet_pairs = (DBG::TOR_USER_ENTRIES_OFFSET / 2)
            ..((DBG::TOR_USER_ENTRIES_OFFSET / 2) + DBG::TOR_USER_REGIONS);

        while let Some(first_region_idx) = user_region_pmpcfg_octet_pairs.next() {
            let second_region_opt = if first_region_idx % 2 == 0 {
                user_region_pmpcfg_octet_pairs.next()
            } else {
                None
            };

            if let Some(_second_region_idx) = second_region_opt {
                // We're at an even index and have two regions to configure, so
                // do that with a single CSR write:
                csr::CSR.pmpconfig_set(
                    first_region_idx / 2,
                    u32::from_be_bytes([
                        TORUserPMPCFG::OFF.get(),
                        TORUserPMPCFG::OFF.get(),
                        TORUserPMPCFG::OFF.get(),
                        TORUserPMPCFG::OFF.get(),
                    ]) as usize,
                );
            } else if first_region_idx % 2 == 0 {
                // This is a single region at an even index. Thus, modify the
                // first two pmpcfgX octets for this region.
                csr::CSR.pmpconfig_modify(
                    first_region_idx / 2,
                    FieldValue::<usize, csr::pmpconfig::pmpcfg::Register>::new(
                        0x0000FFFF,
                        0, // lower two octets
                        u32::from_be_bytes([
                            0,
                            0,
                            TORUserPMPCFG::OFF.get(),
                            TORUserPMPCFG::OFF.get(),
                        ]) as usize,
                    ),
                );
            } else {
                // This is a single region at an odd index. Thus, modify the
                // latter two pmpcfgX octets for this region.
                csr::CSR.pmpconfig_modify(
                    first_region_idx / 2,
                    FieldValue::<usize, csr::pmpconfig::pmpcfg::Register>::new(
                        0x0000FFFF,
                        16, // higher two octets
                        u32::from_be_bytes([
                            0,
                            0,
                            TORUserPMPCFG::OFF.get(),
                            TORUserPMPCFG::OFF.get(),
                        ]) as usize,
                    ),
                );
            }
        }

        self.user_pmp_enabled.set(false);
    }
}

impl<const HANDOVER_CONFIG_CHECK: bool> TORUserPMP<{ TOR_USER_REGIONS_DEBUG_ENABLE }>
    for EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, EPMPDebugEnable>
{
    // Don't require any const-assertions in the EarlGreyEPMP.
    const CONST_ASSERT_CHECK: () = ();

    fn available_regions(&self) -> usize {
        self.user_available_regions::<TOR_USER_REGIONS_DEBUG_ENABLE>()
    }

    fn configure_pmp(
        &self,
        regions: &[(TORUserPMPCFG, *const u8, *const u8); TOR_USER_REGIONS_DEBUG_ENABLE],
    ) -> Result<(), ()> {
        self.user_configure_pmp::<TOR_USER_REGIONS_DEBUG_ENABLE>(regions)
    }

    fn enable_user_pmp(&self) -> Result<(), ()> {
        self.user_enable_user_pmp()
    }

    fn disable_user_pmp(&self) {
        // Technically, the `disable_user_pmp` can be implemented as a no-op in
        // the debug-mode ePMP, as machine-mode lockdown (MML) is not enabled.
        // However, we still execercise these routines to stay as close to the
        // non-debug ePMP configuration as possible:
        self.user_disable_user_pmp()
    }
}

impl<const HANDOVER_CONFIG_CHECK: bool> TORUserPMP<{ TOR_USER_REGIONS_DEBUG_DISABLE }>
    for EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, EPMPDebugDisable>
{
    // Don't require any const-assertions in the EarlGreyEPMP.
    const CONST_ASSERT_CHECK: () = ();

    fn available_regions(&self) -> usize {
        self.user_available_regions::<TOR_USER_REGIONS_DEBUG_DISABLE>()
    }

    fn configure_pmp(
        &self,
        regions: &[(TORUserPMPCFG, *const u8, *const u8); TOR_USER_REGIONS_DEBUG_DISABLE],
    ) -> Result<(), ()> {
        self.user_configure_pmp::<TOR_USER_REGIONS_DEBUG_DISABLE>(regions)
    }

    fn enable_user_pmp(&self) -> Result<(), ()> {
        self.user_enable_user_pmp()
    }

    fn disable_user_pmp(&self) {
        self.user_disable_user_pmp()
    }
}

impl<const HANDOVER_CONFIG_CHECK: bool, DBG: EPMPDebugConfig> fmt::Display
    for EarlGreyEPMP<{ HANDOVER_CONFIG_CHECK }, DBG>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use kernel::utilities::registers::interfaces::Readable;

        write!(f, " EarlGrey ePMP configuration:\r\n")?;
        write!(
            f,
            "  mseccfg: {:#08X}, user-mode PMP active: {:?}\r\n",
            csr::CSR.mseccfg.get(),
            self.user_pmp_enabled.get()
        )?;
        unsafe { format_pmp_entries::<PMP_ENTRIES>(f) }?;

        write!(f, "  Shadow PMP entries for user-mode:\r\n")?;
        for (i, shadowed_pmpcfg) in self.shadow_user_pmpcfgs[..DBG::TOR_USER_REGIONS]
            .iter()
            .enumerate()
        {
            let (start_pmpaddr_label, startaddr_pmpaddr, endaddr, mode) =
                if shadowed_pmpcfg.get() == TORUserPMPCFG::OFF {
                    (
                        "pmpaddr",
                        csr::CSR.pmpaddr_get(DBG::TOR_USER_ENTRIES_OFFSET + (i * 2)),
                        0,
                        "OFF",
                    )
                } else {
                    (
                        "  start",
                        csr::CSR
                            .pmpaddr_get(DBG::TOR_USER_ENTRIES_OFFSET + (i * 2))
                            .overflowing_shl(2)
                            .0,
                        csr::CSR
                            .pmpaddr_get(DBG::TOR_USER_ENTRIES_OFFSET + (i * 2) + 1)
                            .overflowing_shl(2)
                            .0
                            | 0b11,
                        "TOR",
                    )
                };

            write!(
                f,
                "  [{:02}]: {}={:#010X}, end={:#010X}, cfg={:#04X} ({}) ({}{}{}{})\r\n",
                DBG::TOR_USER_ENTRIES_OFFSET + (i * 2) + 1,
                start_pmpaddr_label,
                startaddr_pmpaddr,
                endaddr,
                shadowed_pmpcfg.get().get(),
                mode,
                if shadowed_pmpcfg.get().get_reg().is_set(pmpcfg_octet::l) {
                    "l"
                } else {
                    "-"
                },
                if shadowed_pmpcfg.get().get_reg().is_set(pmpcfg_octet::r) {
                    "r"
                } else {
                    "-"
                },
                if shadowed_pmpcfg.get().get_reg().is_set(pmpcfg_octet::w) {
                    "w"
                } else {
                    "-"
                },
                if shadowed_pmpcfg.get().get_reg().is_set(pmpcfg_octet::x) {
                    "x"
                } else {
                    "-"
                },
            )?;
        }

        Ok(())
    }
}