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
//! AArch32 (short-descriptor) MMU implementation -- see the parent
//! [`mmu`](super) module's doc comment for the overall design.
//!
//! A single flat first-level table of 1MB sections covering all 4GB,
//! CP15-programmed, run at PL1.
//!
//! One file for both 32-bit architectures, because the table itself is
//! the same on each: ARMv6 with `SCTLR.XP` set uses the descriptor format
//! ARMv7-A made the only one, down to the bit positions of `TEX`, `APX`
//! and `XN`. What differs is the CP15 programming around it, and each
//! difference is marked `cfg(armv6)` below with its reason -- the short
//! version being that ARMv6 has no Snoop Control Unit to enable, no
//! inner-shareable operations, nothing shareable worth marking on a
//! uniprocessor, and caches that come out of reset holding rubbish.
use ;
use crate;
use crateclean_range;
use asm;
use UnsafeCell;
/// Entries in a full first-level short-descriptor translation table:
/// one 1MB section per entry, covering the whole 4GB address space.
const SECTION_COUNT: usize = 4096;
/// Shift from a section index to its base physical address (each
/// section covers `1 << 20` = 1MB).
const SECTION_SHIFT: u32 = 20;
// Section descriptor fields (ARMv7-A VMSA short-descriptor format --
// see the ARM Architecture Reference Manual's translation table
// descriptor and memory region attribute tables).
/// Bits[1:0]: marks this entry as a Section descriptor with PXN
/// (bit 0) clear, i.e. privileged execution is allowed.
const DESCRIPTOR_SECTION: u32 = 0b10;
/// Bits[11:10] (AP\[1:0\]) with APX (bit 15) left clear: full
/// read/write access at any privilege level.
const AP_FULL_ACCESS: u32 = 0b11 << 10;
/// Bit[4]: execute-never.
const XN: u32 = 1 << 4;
/// Bit[2] (B) -- meaning depends on which `TEX`/`C` it's paired with;
/// see `SECTION_RAM`/`SECTION_DEVICE`'s comments.
const B_BIT: u32 = 1 << 2;
/// Bits[14:12] (TEX) = `0b001`: Normal memory, with C and B then selecting
/// its cacheability -- C=1/B=1 for Outer and Inner Write-Back
/// Write-Allocate, both clear for Outer and Inner Non-cacheable. See the
/// parent module's doc comment on why RAM needs to be Cacheable, not just
/// Shareable, for `ldrex`/`strex` to actually succeed on this core.
const TEX_NORMAL: u32 = 0b001 << 12;
/// Bit[3] (C), paired with `TEX_NORMAL`/`B_BIT` above.
const C_BIT: u32 = 1 << 3;
/// Bit[16] (S): Shareable. Necessary but not sufficient on its own on
/// this core (Cortex-A7) -- see the parent module's doc comment.
const S_BIT: u32 = 1 << 16;
/// Zero on ARMv6, so RAM is mapped Normal Cacheable **Non-shareable**.
///
/// The one descriptor field that does not carry over from the ARMv7
/// constants, and the reasoning is the opposite of the ARMv7 one above.
/// The ARM1176JZF-S is a uniprocessor part with no coherent interconnect
/// and no external global monitor: there is nothing for a shareable
/// mapping to be shared *with*, and marking cacheable memory shareable on
/// a core that cannot coherently cache it is how a region quietly stops
/// being cached at all -- which would take `ldrex`/`strex` down with it,
/// this map's whole purpose (see the parent module's doc comment).
/// Non-shareable leaves the core's own local exclusive monitor to do the
/// job, which is what it is for.
///
/// Linux reaches the same arrangement from the other direction: its
/// ARMv6 section flags are `PMD_FLAGS_UP = PMD_SECT_WB` for
/// uniprocessor, against `PMD_FLAGS_SMP` which adds `PMD_SECT_S`, and it
/// forces the S bit on only when it knows it is running SMP.
const S_BIT: u32 = 0;
/// RAM: Normal, Write-Back Write-Allocate Cacheable, Shareable, full
/// access, executable -- covers every address this crate's own code,
/// data, and stacks can occupy.
const SECTION_RAM: u32 = DESCRIPTOR_SECTION | AP_FULL_ACCESS | TEX_NORMAL | C_BIT | B_BIT | S_BIT;
/// RAM with the caches taken out of the picture: the same `TEX=001` Normal
/// memory as [`SECTION_RAM`] but with C and B clear, i.e. Outer and Inner
/// Non-cacheable. Still Shareable and still Normal (not Device), so
/// unaligned accesses and the compiler's usual load/store merging remain
/// legal -- only the caching is gone. 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 SECTION_RAM_UNCACHED: u32 = DESCRIPTOR_SECTION | AP_FULL_ACCESS | TEX_NORMAL | S_BIT;
/// Peripherals: TEX=000/C=0/B=1 is "Shareable Device" memory -- the
/// same ordering/no-caching guarantees every raw register access in
/// this crate already depends on with the MMU off, now made explicit.
/// Execute-never, since nothing should ever jump into MMIO space.
const SECTION_DEVICE: u32 = DESCRIPTOR_SECTION | AP_FULL_ACCESS | B_BIT | XN;
/// Builds the identity map: RAM below the peripheral base as
/// [`SECTION_RAM`], the peripheral block and the ARM-local peripheral
/// block (on a chip that has one) as [`SECTION_DEVICE`], and everything
/// else left as an invalid descriptor (bits\[1:0\] = `00`) -- touching
/// genuinely unbacked address space still faults instead of being
/// silently redefined as valid.
const
/// The first-level translation table itself, built entirely at compile
/// time (not populated by any runtime loop). `TTBR0` with `N=0` (a single
/// top-level table, no split) requires 16KB alignment --
/// `#[repr(align(16384))]` on a wrapper struct, since `align` can't be
/// attached directly to a `static`'s type when that type is a plain array.
///
/// `UnsafeCell` because the hardware page-table walker reads all of it
/// behind the compiler's back, and because [`set_uncached_block`] rewrites
/// individual descriptors after boot.
;
// SAFETY: the table is written only during early-boot MMU setup and by
// `set_uncached_block`, whose own safety contract requires the region it
// covers to be quiescent; otherwise it is read only by the hardware walker.
unsafe
static PAGE_TABLE: PageTable = PageTable;
/// Bytes covered by one descriptor -- see [`crate::mmu::UNCACHED_GRANULE`].
pub const UNCACHED_GRANULE: usize = 1 << SECTION_SHIFT;
/// Rewrites the section descriptor covering `base` as
/// [`SECTION_RAM_UNCACHED`], then makes the change take effect everywhere.
///
/// The descriptor write is cleaned out of this core's cache before the TLB
/// invalidation because `TTBR0` is programmed for non-cacheable table walks
/// (see [`rpi_hal_mmu_init`]) -- the walker reads RAM directly, so a
/// descriptor sitting dirty in the D-cache is one the hardware would never
/// see. On ARMv7 the TLB operation is the inner-shareable variant
/// (`TLBIALLIS`), so secondary cores walking this same table drop their
/// stale entries too.
///
/// # Safety
///
/// `base` must be 1MB-aligned and within the RAM this table maps as Normal
/// memory; see [`crate::mmu::set_uncached`], which checks both and is the
/// only caller.
pub unsafe
/// Clears the section descriptor covering `base`, leaving that megabyte
/// with no translation.
///
/// The counterpart to [`set_uncached_block`] above, and deliberately much
/// less than it: no cache maintenance and no TLB invalidation, because
/// [`crate::mmu::install_stack_guard`] is its only caller and runs before
/// the MMU is enabled — the caches are off, so the write reaches RAM on
/// its own, and there is no translation cached for an address that has
/// never been translated.
///
/// # Safety
///
/// `base` must be 1MB-aligned, and the megabyte it names must hold
/// nothing: it becomes unreachable. Must be called before the MMU is
/// enabled, per the reasoning above.
///
/// `rt`-gated because `crate::mmu::install_stack_guard` is its only
/// caller and needs that feature's linker script for the symbols naming
/// the region. Without the gate a `mmu`-without-`rt` build carries this
/// as dead code and says so, which rpi-hal's own checks do not see --
/// every one of them has `rt` on.
pub unsafe
/// Builds the identity-mapped page table (above) and enables the MMU.
/// Called from `boot.s`, after `VBAR`/`SCTLR.V` setup and before `.bss`
/// zeroing/`kmain`: a fault during this sequence is at least catchable
/// once `VBAR` is live, and nothing before `kmain` needs the MMU already
/// on.
///
/// Safe to call once per core, not just once overall: with the
/// `multicore` feature on, every secondary core calls this again as part
/// of its own bring-up (see `boot.s`'s `__secondary_core_entry`).
/// TTBR0/DACR/SCTLR and the TLB are all per-core banked state, so each
/// call only ever reprograms the calling core's own copy against the
/// single, already-built [`PAGE_TABLE`] -- never a second build of the
/// table itself.
///
/// # Safety
///
/// Must only be called early in boot, by `rt`'s own boot sequence, 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"