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
//! AArch64 (VMSAv8-64 long-descriptor) MMU implementation, run at EL1 --
//! see the parent [`mmu`](super) module's doc comment for the overall
//! design.
//!
//! The AArch32 table uses 1MB sections in a single flat 4GB table.
//! AArch64 has no equivalent single-level format, so this builds a small
//! two-level tree with a 4KB granule and 39-bit input addresses
//! (`T0SZ`=25), covering the same low 4GB:
//!
//! ```text
//! level 1 (1GB/entry) -> the low four entries, one per 1GB region:
//! [0] -> level-2 table: 0x0000_0000 ..= 0x3FFF_FFFF
//! [1] -> level-2 table: 0x4000_0000 ..= 0x7FFF_FFFF
//! [2] -> level-2 table: 0x8000_0000 ..= 0xBFFF_FFFF
//! [3] -> level-2 table: 0xC000_0000 ..= 0xFFFF_FFFF
//! level 2 (2MB/block): RAM below PERIPHERAL_BASE as Normal, the
//! peripheral block and the ARM-local peripheral block as Device.
//! ```
//!
//! Every region gets its own level-2 table rather than special-casing the
//! ones a peripheral block happens to fall in, because where those blocks
//! land varies by chip: on BCM2836/2837 they sit in regions 0
//! (`0x3F00_0000`) and 1 (`0x4000_0000`), but on BCM2711 both are in
//! region 3 (`0xFE00_0000` and `0xFF80_0000`). Populating a fixed pair of
//! level-1 entries is what an earlier version of this file did, and on
//! BCM2711 that left the whole peripheral block unmapped, so the first
//! register access after enabling translation faulted -- hanging boot
//! inside this module, before anything could report why. Covering all four
//! regions uniformly costs 16KB of `.bss` and removes the class of bug.
//!
//! Anything above 4GB, and anything within it not covered by a rule above,
//! is left as an invalid (zero) descriptor, so touching genuinely unbacked
//! address space faults -- same intent as the AArch32 map.
use ;
use asm;
use UnsafeCell;
/// Number of descriptors in a 4KB-granule translation table.
const ENTRIES: usize = 512;
/// Bytes spanned by one level-2 descriptor (a 2MB block).
const BLOCK_2MB: u64 = 2 * 1024 * 1024;
/// Bytes spanned by one level-1 descriptor (a 1GB region).
const BLOCK_1GB: u64 = 1024 * 1024 * 1024;
/// Level-1 entries populated, covering the low 4GB -- the same span as the
/// AArch32 table, and enough for every physical address this crate can
/// reach (see `TCR_EL1`'s `IPS` below). The rest of the 39-bit input range
/// stays invalid.
const L1_REGIONS: usize = 4;
// Descriptor bits (see the ARMv8-A Architecture Reference Manual's
// VMSAv8-64 translation table descriptor formats).
/// Bits[1:0] = `0b11`: a table descriptor (points at the next level).
const DESC_TABLE: u64 = 0b11;
/// Bits[1:0] = `0b01`: a block descriptor (a leaf mapping at L1/L2).
const DESC_BLOCK: u64 = 0b01;
/// Bit[10] (AF, Access Flag): set so the first access doesn't fault.
const AF: u64 = 1 << 10;
/// Bits[9:8] (SH) = `0b11`: Inner Shareable -- see the parent module's
/// doc comment on why cacheable, shareable RAM matters for exclusive
/// accesses.
const SH_INNER: u64 = 0b11 << 8;
/// Bits[4:2] (AttrIndx) = 0: index into `MAIR_EL1` for Normal memory.
const ATTR_NORMAL: u64 = 0 << 2;
/// Bits[4:2] (AttrIndx) = 1: index into `MAIR_EL1` for Device memory.
const ATTR_DEVICE: u64 = 1 << 2;
/// Bits[4:2] (AttrIndx) = 2: index into `MAIR_EL1` for Normal
/// Non-cacheable memory.
const ATTR_NORMAL_UNCACHED: u64 = 2 << 2;
/// Bit[54] (UXN) and bit[53] (PXN): execute-never at every privilege --
/// nothing should fetch instructions from MMIO space.
const EXEC_NEVER: u64 = | ;
/// Flags for a Normal, cacheable, inner-shareable, executable RAM block.
/// `AP` left `0b00` (bits[7:6]): read/write at EL1, no EL0 access.
const RAM_BLOCK_FLAGS: u64 = DESC_BLOCK | ATTR_NORMAL | SH_INNER | AF;
/// Flags for a Device, execute-never block. `SH` left `0b00` -- Device
/// memory is not cached, so shareability is not meaningful.
const DEVICE_BLOCK_FLAGS: u64 = DESC_BLOCK | ATTR_DEVICE | AF | EXEC_NEVER;
/// Flags for a RAM block with the caches taken out of the picture: still
/// Normal memory (so unaligned accesses and the compiler's usual load/store
/// merging remain legal), just non-cacheable. Installed by
/// [`set_uncached_block`] over a region shared with the VideoCore; see
/// [`crate::mmu::set_uncached`] for why that is necessary rather than a
/// performance choice.
const UNCACHED_BLOCK_FLAGS: u64 = DESC_BLOCK | ATTR_NORMAL_UNCACHED | SH_INNER | AF;
/// A single 4KB-granule translation table. `UnsafeCell` because every
/// entry is filled in at runtime by [`rpi_hal_mmu_init`] (the level-1
/// entries hold the addresses of the level-2 tables, which are link-time
/// values rather than compile-time constants), and because the MMU's
/// hardware walker reads all of them behind the compiler's back.
;
// SAFETY: these tables are mutated only once, during the single-threaded
// early-boot MMU setup, and thereafter read only by the hardware walker.
unsafe
/// An all-invalid table, giving the statics below a zero initializer so
/// they land in `.bss` instead of adding 20KB of descriptors to the binary
/// image. `boot64.s` zeroes `.bss` before calling [`rpi_hal_mmu_init`],
/// which then fills them in.
///
/// A `const` rather than a `static` because it is only ever an initializer
/// for the two items below -- including [`L2`]'s array-repeat, which a
/// `static` can't provide. `clippy::declare_interior_mutable_const` warns
/// that each use is a fresh copy of the `UnsafeCell` rather than shared
/// state, which is exactly the intent here: it names the all-zero
/// descriptor pattern, and the `static`s built from it are the things with
/// identity.
const EMPTY_TABLE: Table = Table;
/// The level-1 table: one entry per 1GB region, the low [`L1_REGIONS`] of
/// which are pointed at [`L2`]'s tables.
static L1: Table = EMPTY_TABLE;
/// One level-2 table per covered 1GB region, indexed by region number.
static L2: = ;
/// Fills `table` with 2MB identity-mapping blocks for the 1GB region
/// starting at `region * 1GB`, applying the same per-block rule as the
/// AArch32 table: RAM below the peripheral base as Normal, the peripheral
/// block and the ARM-local peripheral block as Device, everything else left
/// invalid.
///
/// A block is classified by its base address, so a peripheral block whose
/// declared end isn't 2MB-aligned is rounded up to the enclosing block --
/// BCM2836/2837's 1MB ARM-local block maps 2MB, for instance. Over-mapping
/// MMIO space as Device is harmless; the alternative is a level-3 table for
/// no benefit.
/// Builds the identity-mapped translation tables and enables the MMU.
///
/// Must be called at EL1 (a consumer's boot stub is responsible for the
/// EL2->EL1 drop first). Safe to call once per core: `TTBR0_EL1`/
/// `TCR_EL1`/`MAIR_EL1`/`SCTLR_EL1` and the TLB are per-core state, and
/// the tables are rebuilt identically every time.
///
/// # Safety
///
/// Must only be called early in boot, at EL1, on each core before any
/// code on that core relies on today's MMU-off memory ordering guarantees
/// changing underneath it.
pub unsafe extern "C"
/// Bytes covered by one level-2 descriptor -- see
/// [`crate::mmu::UNCACHED_GRANULE`].
pub const UNCACHED_GRANULE: usize = BLOCK_2MB as usize;
/// Rewrites the level-2 block descriptor covering `base` with
/// [`UNCACHED_BLOCK_FLAGS`], then makes the change take effect everywhere.
///
/// No cache maintenance on the descriptor itself: `TCR_EL1` programs table
/// walks as Write-Back cacheable and Inner Shareable (see
/// [`rpi_hal_mmu_init`]), so the walker reads through the same coherent
/// caches this store lands in, and a `dsb` is all that is needed to order
/// it before the TLB operation. That operation is the inner-shareable,
/// by-address form (`tlbi vaae1is`, whose operand is the virtual address
/// shifted right by 12), so secondary cores walking these same tables drop
/// their stale entry for this block too.
///
/// # Safety
///
/// `base` must be 2MB-aligned and within the RAM these tables map as Normal
/// memory; see [`crate::mmu::set_uncached`], which checks both and is the
/// only caller.
pub unsafe