aarch64-rt 0.4.3

Startup code and exception vector for aarch64 Cortex-A processors.
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
// Copyright 2025 The aarch64-rt Authors.
// This project is dual-licensed under Apache 2.0 and MIT terms.
// See LICENSE-APACHE and LICENSE-MIT for details.

//! Code to set up an initial pagetable.

use core::arch::naked_asm;

const MAIR_DEV_NGNRE: u64 = 0x04;
const MAIR_MEM_WBWA: u64 = 0xff;
/// The default value used for MAIR_ELx.
pub const DEFAULT_MAIR: u64 = MAIR_DEV_NGNRE | MAIR_MEM_WBWA << 8;

/// 4 KiB granule size for TTBR1_ELx.
const TCR_TG1_4KB: u64 = 0x2 << 30;
/// Disable translation table walk for TTBR1_ELx, generating a translation fault instead.
const TCR_EPD1: u64 = 0x1 << 23;
/// 40 bits, 1 TiB.
const TCR_EL1_IPS_1TB: u64 = 0x2 << 32;
/// 40 bits, 1 TiB.
const TCR_EL2_PS_1TB: u64 = 0x2 << 16;
/// 4 KiB granule size for TTBR0_ELx.
const TCR_TG0_4KB: u64 = 0x0 << 14;
/// Translation table walks for TTBR0_ELx are inner sharable.
const TCR_SH_INNER: u64 = 0x3 << 12;
/// Translation table walks for TTBR0_ELx are outer write-back read-allocate write-allocate
/// cacheable.
const TCR_RGN_OWB: u64 = 0x1 << 10;
/// Translation table walks for TTBR0_ELx are inner write-back read-allocate write-allocate
/// cacheable.
const TCR_RGN_IWB: u64 = 0x1 << 8;
/// Size offset for TTBR0_ELx is 2**39 bytes (512 GiB).
const TCR_T0SZ_512: u64 = 64 - 39;
/// The default value used for TCR_EL1.
pub const DEFAULT_TCR_EL1: u64 = TCR_EL1_IPS_1TB
    | TCR_TG1_4KB
    | TCR_EPD1
    | TCR_TG0_4KB
    | TCR_SH_INNER
    | TCR_RGN_OWB
    | TCR_RGN_IWB
    | TCR_T0SZ_512;
/// The default value used for TCR_EL2.
pub const DEFAULT_TCR_EL2: u64 =
    TCR_EL2_PS_1TB | TCR_TG0_4KB | TCR_SH_INNER | TCR_RGN_OWB | TCR_RGN_IWB | TCR_T0SZ_512;
/// The default value used for TCR_EL3.
pub const DEFAULT_TCR_EL3: u64 =
    TCR_TG0_4KB | TCR_RGN_OWB | TCR_RGN_IWB | TCR_SH_INNER | TCR_T0SZ_512;

/// Stage 1 instruction access cacheability is unaffected.
const SCTLR_ELX_I: u64 = 0x1 << 12;
/// SP alignment fault if SP is not aligned to a 16 byte boundary.
const SCTLR_ELX_SA: u64 = 0x1 << 3;
/// Stage 1 data access cacheability is unaffected.
const SCTLR_ELX_C: u64 = 0x1 << 2;
/// EL0 and EL1 stage 1 MMU enabled.
const SCTLR_ELX_M: u64 = 0x1 << 0;
/// Privileged Access Never is unchanged on taking an exception to ELx.
const SCTLR_ELX_SPAN: u64 = 0x1 << 23;
/// SETEND instruction disabled at EL0 in aarch32 mode.
const SCTLR_ELX_SED: u64 = 0x1 << 8;
/// Various IT instructions are disabled at EL0 in aarch32 mode.
const SCTLR_ELX_ITD: u64 = 0x1 << 7;
const SCTLR_ELX_RES1: u64 = (0x1 << 11) | (0x1 << 20) | (0x1 << 22) | (0x1 << 28) | (0x1 << 29);
/// The default value used for SCTLR_ELx.
pub const DEFAULT_SCTLR: u64 = SCTLR_ELX_M
    | SCTLR_ELX_C
    | SCTLR_ELX_SA
    | SCTLR_ELX_ITD
    | SCTLR_ELX_SED
    | SCTLR_ELX_I
    | SCTLR_ELX_SPAN
    | SCTLR_ELX_RES1;

/// Provides an initial pagetable which can be used before any Rust code is run.
///
/// The `initial-pagetable` feature must be enabled for this to be used.
#[cfg(any(feature = "el1", feature = "el2", feature = "el3"))]
#[macro_export]
macro_rules! initial_pagetable {
    ($value:expr, $mair:expr, $sctlr:expr, $tcr:expr) => {
        static INITIAL_PAGETABLE: $crate::InitialPagetable = $value;

        $crate::enable_mmu!(INITIAL_PAGETABLE, $mair, $sctlr, $tcr);
    };
    ($value:expr, $mair:expr) => {
        $crate::initial_pagetable!($value, $mair, $crate::DEFAULT_SCTLR, $crate::DEFAULT_TCR);
    };
    ($value:expr) => {
        $crate::initial_pagetable!(
            $value,
            $crate::DEFAULT_MAIR,
            $crate::DEFAULT_SCTLR,
            $crate::DEFAULT_TCR
        );
    };
}

/// Provides an initial pagetable which can be used before any Rust code is run.
///
/// The `initial-pagetable` feature must be enabled for this to be used.
#[cfg(not(any(feature = "el1", feature = "el2", feature = "el3")))]
#[macro_export]
macro_rules! initial_pagetable {
    ($value:expr, $mair:expr, $sctlr:expr, $tcr_el1:expr, $tcr_el2:expr, $tcr_el3:expr) => {
        static INITIAL_PAGETABLE: $crate::InitialPagetable = $value;

        $crate::enable_mmu!(
            INITIAL_PAGETABLE,
            $mair,
            $sctlr,
            $tcr_el1,
            $tcr_el2,
            $tcr_el3
        );
    };
    ($value:expr, $mair:expr) => {
        initial_pagetable!(
            $value,
            $mair,
            $crate::DEFAULT_SCTLR,
            $crate::DEFAULT_TCR_EL1,
            $crate::DEFAULT_TCR_EL2,
            $crate::DEFAULT_TCR_EL3
        );
    };
    ($value:expr) => {
        initial_pagetable!(
            $value,
            $crate::DEFAULT_MAIR,
            $crate::DEFAULT_SCTLR,
            $crate::DEFAULT_TCR_EL1,
            $crate::DEFAULT_TCR_EL2,
            $crate::DEFAULT_TCR_EL3
        );
    };
}

/// Enables the MMU and caches, assuming that we are running at EL1.
///
/// # Safety
///
/// This function doesn't follow the standard aarch64 calling convention. It must only be called
/// from assembly code, early in the boot process.
///
/// Expects the MAIR value in x8, the SCTLR value in x9, the TCR value in x10 and the root pagetable
/// address in x11.
///
/// Clobbers x8-x9.
#[doc(hidden)]
#[unsafe(naked)]
pub unsafe extern "C" fn __enable_mmu_el1() {
    naked_asm!(
        // Load and apply the memory management configuration, ready to enable MMU and
        // caches.
        "msr mair_el1, x8",
        "msr ttbr0_el1, x11",
        // Copy the supported PA range into TCR_EL1.IPS.
        "mrs x8, id_aa64mmfr0_el1",
        "bfi x10, x8, #32, #4",
        "msr tcr_el1, x10",
        // Ensure everything before this point has completed, then invalidate any
        // potentially stale local TLB entries before they start being used.
        "isb",
        "tlbi vmalle1",
        "ic iallu",
        "dsb nsh",
        "isb",
        // Configure SCTLR_EL1 to enable MMU and cache and don't proceed until this has
        // completed.
        "msr sctlr_el1, x9",
        "isb",
        "ret"
    );
}

/// Enables the MMU and caches, assuming that we are running at EL2.
///
/// # Safety
///
/// This function doesn't follow the standard aarch64 calling convention. It must only be called
/// from assembly code, early in the boot process.
///
/// Expects the MAIR value in x8, the SCTLR value in x9, the TCR value in x10 and the root pagetable
/// address in x11.
///
/// Clobbers x8-x9.
#[doc(hidden)]
#[unsafe(naked)]
pub unsafe extern "C" fn __enable_mmu_el2() {
    naked_asm!(
        // Load and apply the memory management configuration, ready to enable MMU and
        // caches.
        "msr mair_el2, x8",
        "msr ttbr0_el2, x11",
        // Copy the supported PA range into TCR_EL2.IPS.
        "mrs x8, id_aa64mmfr0_el1",
        "bfi x10, x8, #32, #4",
        "msr tcr_el2, x10",
        // Ensure everything before this point has completed, then invalidate any
        // potentially stale local TLB entries before they start being used.
        "isb",
        "tlbi vmalle1",
        "ic iallu",
        "dsb nsh",
        "isb",
        // Configure SCTLR_EL2 to enable MMU and cache and don't proceed until this has
        // completed.
        "msr sctlr_el2, x9",
        "isb",
        "ret"
    );
}

/// Enables the MMU and caches, assuming that we are running at EL3.
///
/// # Safety
///
/// This function doesn't follow the standard aarch64 calling convention. It must only be called
/// from assembly code, early in the boot process.
///
/// Expects the MAIR value in x8, the SCTLR value in x9, the TCR value in x10 and the root pagetable
/// address in x11.
///
/// Clobbers x8-x9.
#[doc(hidden)]
#[unsafe(naked)]
pub unsafe extern "C" fn __enable_mmu_el3() {
    naked_asm!(
        // Load and apply the memory management configuration, ready to enable MMU and
        // caches.
        "msr mair_el3, x8",
        "msr ttbr0_el3, x11",
        // Copy the supported PA range into TCR_EL3.IPS.
        "mrs x8, id_aa64mmfr0_el1",
        "bfi x10, x8, #32, #4",
        "msr tcr_el3, x10",
        // Ensure everything before this point has completed, then invalidate any
        // potentially stale local TLB entries before they start being used.
        "isb",
        "tlbi vmalle1",
        "ic iallu",
        "dsb nsh",
        "isb",
        // Configure SCTLR_EL3 to enable MMU and cache and don't proceed until this has
        // completed.
        "msr sctlr_el3, x9",
        "isb",
        "ret"
    );
}

/// Generates assembly code to enable the MMU and caches with the given initial pagetable before any
/// Rust code is run.
///
/// This may be used indirectly via the [`initial_pagetable!`] macro.
#[cfg(feature = "el1")]
#[macro_export]
macro_rules! enable_mmu {
    ($pagetable:path, $mair:expr, $sctlr:expr, $tcr:expr) => {
        core::arch::global_asm!(
            r".macro mov_i, reg:req, imm:req",
                r"movz \reg, :abs_g3:\imm",
                r"movk \reg, :abs_g2_nc:\imm",
                r"movk \reg, :abs_g1_nc:\imm",
                r"movk \reg, :abs_g0_nc:\imm",
            r".endm",

            ".section .init, \"ax\"",
            ".global enable_mmu",
            "enable_mmu:",
                "mov_i x8, {MAIR_VALUE}",
                "mov_i x9 {SCTLR_VALUE}",
                "mov_i x10, {TCR_VALUE}",
                "adrp x11, {pagetable}",

                "b {enable_mmu_el1}",

            ".purgem mov_i",
            MAIR_VALUE = const $mair,
            SCTLR_VALUE = const $sctlr,
            TCR_VALUE = const $tcr,
            pagetable = sym $pagetable,
            enable_mmu_el1 = sym $crate::__private::__enable_mmu_el1,
        );
    };
    ($pagetable:path) => {
        $crate::enable_mmu!($pagetable, $crate::DEFAULT_MAIR, $crate::DEFAULT_SCTLR, $crate::DEFAULT_TCR_EL1);
    };
}

/// Generates assembly code to enable the MMU and caches with the given initial pagetable before any
/// Rust code is run.
///
/// This may be used indirectly via the [`initial_pagetable!`] macro.
#[cfg(feature = "el2")]
#[macro_export]
macro_rules! enable_mmu {
    ($pagetable:path, $mair:expr, $sctlr:expr, $tcr:expr) => {
        core::arch::global_asm!(
            r".macro mov_i, reg:req, imm:req",
                r"movz \reg, :abs_g3:\imm",
                r"movk \reg, :abs_g2_nc:\imm",
                r"movk \reg, :abs_g1_nc:\imm",
                r"movk \reg, :abs_g0_nc:\imm",
            r".endm",

            ".section .init, \"ax\"",
            ".global enable_mmu",
            "enable_mmu:",
                "mov_i x8, {MAIR_VALUE}",
                "mov_i x9, {SCTLR_VALUE}",
                "mov_i x10, {TCR_VALUE}",
                "adrp x11, {pagetable}",

                "b {enable_mmu_el2}",

            ".purgem mov_i",
            MAIR_VALUE = const $mair,
            SCTLR_VALUE = const $sctlr,
            TCR_VALUE = const $tcr,
            pagetable = sym $pagetable,
            enable_mmu_el2 = sym $crate::__private::__enable_mmu_el2,
        );
    };
    ($pagetable:path) => {
        $crate::enable_mmu!($pagetable, $crate::DEFAULT_MAIR, $crate::DEFAULT_SCTLR, $crate::DEFAULT_TCR_EL2);
    };
}

/// Generates assembly code to enable the MMU and caches with the given initial pagetable before any
/// Rust code is run.
///
/// This may be used indirectly via the [`initial_pagetable!`] macro.
#[cfg(feature = "el3")]
#[macro_export]
macro_rules! enable_mmu {
    ($pagetable:path, $mair:expr, $sctlr:expr, $tcr:expr) => {
        core::arch::global_asm!(
            r".macro mov_i, reg:req, imm:req",
                r"movz \reg, :abs_g3:\imm",
                r"movk \reg, :abs_g2_nc:\imm",
                r"movk \reg, :abs_g1_nc:\imm",
                r"movk \reg, :abs_g0_nc:\imm",
            r".endm",

            ".section .init, \"ax\"",
            ".global enable_mmu",
            "enable_mmu:",
                "mov_i x8, {MAIR_VALUE}",
                "mov_i x9, {SCTLR_VALUE}",
                "mov_i x10, {TCR_VALUE}",
                "adrp x11, {pagetable}",

                "b {enable_mmu_el3}",

            ".purgem mov_i",
            MAIR_VALUE = const $mair,
            SCTLR_VALUE = const $sctlr,
            TCR_VALUE = const $tcr,
            pagetable = sym $pagetable,
            enable_mmu_el3 = sym $crate::__private::__enable_mmu_el3,
        );
    };
    ($pagetable:path) => {
        $crate::enable_mmu!($pagetable, $crate::DEFAULT_MAIR, $crate::DEFAULT_SCTLR, $crate::DEFAULT_TCR_EL3);
    };
}

/// Generates assembly code to enable the MMU and caches with the given initial pagetable before any
/// Rust code is run.
///
/// This may be used indirectly via the [`initial_pagetable!`] macro.
#[cfg(not(any(feature = "el1", feature = "el2", feature = "el3")))]
#[macro_export]
macro_rules! enable_mmu {
    ($pagetable:path, $mair:expr, $sctlr:expr, $tcr_el1:expr, $tcr_el2:expr, $tcr_el3:expr) => {
        core::arch::global_asm!(
            r".macro mov_i, reg:req, imm:req",
                r"movz \reg, :abs_g3:\imm",
                r"movk \reg, :abs_g2_nc:\imm",
                r"movk \reg, :abs_g1_nc:\imm",
                r"movk \reg, :abs_g0_nc:\imm",
            r".endm",

            ".section .init, \"ax\"",
            ".global enable_mmu",
            "enable_mmu:",
                "mov_i x8, {MAIR_VALUE}",
                "mov_i x9, {SCTLR_VALUE}",
                "adrp x11, {pagetable}",

                "mrs x12, CurrentEL",
                "ubfx x12, x12, #2, #2",

                "cmp x12, #3",
                "b.ne 0f",
                "mov_i x10, {TCR_EL3_VALUE}",
                "b {enable_mmu_el3}",
            "0:",
                "cmp x12, #2",
                "b.ne 1f",
                "mov_i x10, {TCR_EL2_VALUE}",
                "b {enable_mmu_el2}",
            "1:",
                "mov_i x10, {TCR_EL1_VALUE}",
                "b {enable_mmu_el1}",

            ".purgem mov_i",
            MAIR_VALUE = const $mair,
            SCTLR_VALUE = const $sctlr,
            TCR_EL1_VALUE = const $tcr_el1,
            TCR_EL2_VALUE = const $tcr_el2,
            TCR_EL3_VALUE = const $tcr_el3,
            pagetable = sym $pagetable,
            enable_mmu_el1 = sym $crate::__private::__enable_mmu_el1,
            enable_mmu_el2 = sym $crate::__private::__enable_mmu_el2,
            enable_mmu_el3 = sym $crate::__private::__enable_mmu_el3,
        );
    };
    ($pagetable:path) => {
        $crate::enable_mmu!(
            $pagetable,
            $crate::DEFAULT_MAIR,
            $crate::DEFAULT_SCTLR,
            $crate::DEFAULT_TCR_EL1,
            $crate::DEFAULT_TCR_EL2,
            $crate::DEFAULT_TCR_EL3
        );
    };
}

/// A hardcoded pagetable.
#[repr(C, align(4096))]
pub struct InitialPagetable(pub [usize; 512]);