cortex-ar 0.3.0

CPU support for AArch32 Arm Cortex-R and Arm Cortex-A
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
//! Support for the PMSAv7 MPU
//!
//! See Chapter 14: Protected Memory System Architecture in [Arm
//! Architecture Reference Manual ARMv7-A and ARMv7-R edition][armv7]
//!
//! [armv7]: https://developer.arm.com/documentation/ddi0406/latest

use crate::register;

use arbitrary_int::{u2, u3};
#[doc(inline)]
pub use register::drsr::RegionSize;

/// Ways this API can fail
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
    /// Found too many regions
    TooManyRegions,
    /// Found a region with invalid alignment
    UnalignedRegion(*mut u8),
}

/// Represents our PMSAv7 MPU
pub struct Mpu();

impl Mpu {
    /// Create an MPU handle
    ///
    /// # Safety
    ///
    /// Only create one of these at any given time, as they access shared
    /// mutable state within the processor and do read-modify-writes on that state.
    pub unsafe fn new() -> Mpu {
        Mpu()
    }

    /// How many MPU instruction regions are there?
    pub fn num_iregions(&self) -> u8 {
        register::Mpuir::read().iregions()
    }

    /// How many MPU data/unified regions are there?
    pub fn num_dregions(&self) -> u8 {
        register::Mpuir::read().dregions()
    }

    /// Do we have a unified MPU?
    pub fn is_unified(&self) -> bool {
        !register::Mpuir::read().non_unified()
    }

    /// Get an instruction region
    pub fn get_iregion(&mut self, idx: u8) -> Option<Region> {
        if idx >= self.num_iregions() {
            return None;
        }
        register::Rgnr::write(register::Rgnr(idx as u32));
        let base = register::Irbar::read().0;
        let rsr = register::Irsr::read();
        let racr = register::Iracr::read();

        let mem_attr_bits = MemAttrBits {
            tex: racr.tex(),
            c: racr.c(),
            b: racr.b(),
            s: racr.s(),
        };

        let mem_attr = mem_attr_bits.decode()?;

        Some(Region {
            base,
            size: rsr.region_size(),
            subregion_mask: rsr.subregion_mask(),
            enabled: rsr.enabled(),
            no_exec: racr.nx(),
            mem_attr,
        })
    }

    /// Get a data/unified region
    pub fn get_dregion(&mut self, idx: u8) -> Option<Region> {
        if idx >= self.num_dregions() {
            return None;
        }
        register::Rgnr::write(register::Rgnr(idx as u32));
        let base = register::Drbar::read().0;
        let rsr = register::Drsr::read();
        let racr = register::Dracr::read();

        let mem_attr_bits = MemAttrBits {
            tex: racr.tex(),
            c: racr.c(),
            b: racr.b(),
            s: racr.s(),
        };
        let mem_attr = mem_attr_bits.decode()?;

        Some(Region {
            base,
            size: rsr.region_size(),
            subregion_mask: rsr.subregion_mask(),
            enabled: rsr.enabled(),
            no_exec: racr.nx(),
            mem_attr,
        })
    }

    /// Configure the EL1 MPU
    pub fn configure(&mut self, config: &Config) -> Result<(), Error> {
        if config.iregions.len() > self.num_iregions() as usize {
            return Err(Error::TooManyRegions);
        }
        if config.dregions.len() > self.num_dregions() as usize {
            return Err(Error::TooManyRegions);
        }
        for (idx, region) in config.iregions.iter().enumerate() {
            register::Rgnr::write(register::Rgnr(idx as u32));
            if !region.size.is_aligned(region.base) {
                return Err(Error::UnalignedRegion(region.base));
            }
            register::Irbar::write(register::Irbar(region.base));
            register::Irsr::write({
                let mut out = register::Irsr::new_with_raw_value(0);
                out.set_enabled(region.enabled);
                out.set_region_size(region.size);
                out.set_subregion_mask(region.subregion_mask);
                out
            });
            register::Iracr::write({
                let mut out = register::Iracr::new_with_raw_value(0);
                let mem_attr_bits = region.mem_attr.to_bits();
                out.set_tex(mem_attr_bits.tex);
                out.set_c(mem_attr_bits.c);
                out.set_b(mem_attr_bits.b);
                out.set_s(mem_attr_bits.s);
                out.set_nx(region.no_exec);
                // out.with_ap(region.access_perms);
                out
            });
        }
        for (idx, region) in config.dregions.iter().enumerate() {
            if !region.size.is_aligned(region.base) {
                return Err(Error::UnalignedRegion(region.base));
            }
            register::Rgnr::write(register::Rgnr(idx as u32));
            register::Drbar::write(register::Drbar(region.base));
            register::Drsr::write({
                let mut out = register::Drsr::new_with_raw_value(0);
                out.set_enabled(region.enabled);
                out.set_region_size(region.size);
                out.set_subregion_mask(region.subregion_mask);
                out
            });
            register::Dracr::write({
                let mut out = register::Dracr::new_with_raw_value(0);
                let mem_attr_bits = region.mem_attr.to_bits();
                out.set_tex(mem_attr_bits.tex);
                out.set_c(mem_attr_bits.c);
                out.set_b(mem_attr_bits.b);
                out.set_s(mem_attr_bits.s);
                out.set_nx(region.no_exec);
                // out.with_ap(region.access_perms);
                out
            });
        }
        register::Sctlr::modify(|r| {
            r.set_br(config.background_config);
        });
        Ok(())
    }

    /// Enable the MPU
    pub fn enable(&mut self) {
        register::Sctlr::modify(|r| {
            r.set_m(true);
        });
    }

    /// Disable the MPU
    pub fn disable(&mut self) {
        register::Sctlr::modify(|r| {
            r.set_m(false);
        });
    }
}

/// Configuration for the PMSAv7 MPU
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Config<'a> {
    /// Background Config Enable
    ///
    /// If true, use the default MMU config if no other region matches an address
    pub background_config: bool,
    /// List of instruction regions
    pub iregions: &'a [Region],
    /// List of data/unified regions
    pub dregions: &'a [Region],
}

/// Configuration for a region in the PMSAv7 MPU
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Region {
    /// The base address of this region.
    ///
    /// Must be aligned to the size of the region.
    pub base: *mut u8,
    /// The size of this region
    pub size: RegionSize,
    /// Sub-region bitmask
    ///
    /// The region is divided into exactly eight equal sized subregions.
    /// Subregion 0 is the subregion at the least significant address.
    ///
    /// A 1 bit means that sub-region is disabled.
    ///
    /// Only applies to regions sized 256 bytes or larger.
    pub subregion_mask: u8,
    /// Is this region enabled?
    pub enabled: bool,
    /// No-Execute in this region
    pub no_exec: bool,
    /// Attributes for this region
    pub mem_attr: MemAttr,
}

// Creating a static Region is fine - the pointers within it
// only go to the MPU and aren't accessed via Rust code
unsafe impl Sync for Region {}

/// Describes the memory ordering and cacheability of a region
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MemAttr {
    /// Strongly-ordered memory
    StronglyOrdered,
    /// Device (shareable or non-shareable)
    Device { shareable: bool },
    /// Outer and Inner Write-Through, no Write-Allocate
    WriteThroughNoWriteAllocate { shareable: bool },
    /// Outer and Inner Write-Back, no Write-Allocate
    WriteBackNoWriteAllocate { shareable: bool },
    /// Outer and Inner Non-cacheable
    NonCacheable { shareable: bool },
    /// Implementation Defined
    ImplementationDefined { shareable: bool },
    /// Outer and Inner Write-Back, Write-Allocate
    WriteBackWriteAllocate { shareable: bool },
    /// Cacheable memory
    Cacheable {
        outer: CacheablePolicy,
        inner: CacheablePolicy,
        shareable: bool,
    },
}

impl MemAttr {
    /// Convert this memory attribute to an 8-bit value we can write to MAIRx
    const fn to_bits(&self) -> MemAttrBits {
        match self {
            MemAttr::StronglyOrdered => MemAttrBits {
                tex: u3::from_u8(0b000),
                c: false,
                b: false,
                s: true,
            },
            MemAttr::Device { shareable: true } => MemAttrBits {
                tex: u3::from_u8(0b000),
                c: false,
                b: true,
                s: true,
            },
            MemAttr::Device { shareable: false } => MemAttrBits {
                tex: u3::from_u8(0b010),
                c: false,
                b: false,
                s: false,
            },
            MemAttr::WriteThroughNoWriteAllocate { shareable } => MemAttrBits {
                tex: u3::from_u8(0b000),
                c: true,
                b: false,
                s: *shareable,
            },
            MemAttr::WriteBackNoWriteAllocate { shareable } => MemAttrBits {
                tex: u3::from_u8(0b000),
                c: true,
                b: true,
                s: *shareable,
            },
            MemAttr::NonCacheable { shareable } => MemAttrBits {
                tex: u3::from_u8(0b001),
                c: false,
                b: false,
                s: *shareable,
            },
            MemAttr::ImplementationDefined { shareable } => MemAttrBits {
                tex: u3::from_u8(0b001),
                c: true,
                b: false,
                s: *shareable,
            },
            MemAttr::WriteBackWriteAllocate { shareable } => MemAttrBits {
                tex: u3::from_u8(0b000),
                c: true,
                b: true,
                s: *shareable,
            },
            MemAttr::Cacheable {
                outer,
                inner,
                shareable,
            } => {
                let outer = *outer as u8;
                let inner = *inner as u8;
                MemAttrBits {
                    tex: u3::from_u8(0b100 | outer),
                    c: (inner & 0b10) != 0,
                    b: (inner & 0b01) != 0,
                    s: *shareable,
                }
            }
        }
    }
}

/// A representation of Memory Attributes suitable for sticking into the RACR register
#[derive(Debug, Clone, PartialEq, Eq)]
struct MemAttrBits {
    tex: u3,
    c: bool,
    b: bool,
    s: bool,
}

impl MemAttrBits {
    const fn decode(&self) -> Option<MemAttr> {
        match (self.tex.value(), self.c, self.b) {
            (0b000, false, false) => Some(MemAttr::StronglyOrdered),
            (0b000, false, true) => Some(MemAttr::Device { shareable: true }),
            (0b000, true, false) => {
                Some(MemAttr::WriteThroughNoWriteAllocate { shareable: self.s })
            }
            (0b000, true, true) => Some(MemAttr::WriteBackNoWriteAllocate { shareable: self.s }),
            (0b001, false, false) => Some(MemAttr::NonCacheable { shareable: self.s }),
            (0b001, true, false) => Some(MemAttr::ImplementationDefined { shareable: self.s }),
            (0b001, true, true) => Some(MemAttr::WriteBackWriteAllocate { shareable: self.s }),
            (0b010, false, false) => Some(MemAttr::Device { shareable: false }),
            (tex, c, b) if tex >= 0b100 => {
                let outer = tex & 0b11;
                let inner = ((c as u8) << 1) | (b as u8);
                Some(MemAttr::Cacheable {
                    outer: CacheablePolicy::new_with_raw_value(u2::from_u8(outer)),
                    inner: CacheablePolicy::new_with_raw_value(u2::from_u8(inner)),
                    shareable: self.s,
                })
            }
            _ => {
                // failed to decode
                None
            }
        }
    }
}

/// Describes the cache policy of a region
#[derive(Debug, PartialEq, Eq)]
#[bitbybit::bitenum(u2, exhaustive = true)]
pub enum CacheablePolicy {
    NonCacheable = 0b00,
    WriteBackWriteAllocate = 0b01,
    WriteThroughNoWriteAllocate = 0b10,
    WriteBackNoWriteAllocate = 0b11,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn mem_attr_strong() {
        let mem_attr = MemAttr::StronglyOrdered;
        let mem_attr_bits = mem_attr.to_bits();
        assert_eq!(
            mem_attr_bits,
            MemAttrBits {
                tex: u3::from_u8(0),
                c: false,
                b: false,
                s: true
            }
        );
        let mem_attr2 = mem_attr_bits.decode();
        assert_eq!(Some(mem_attr), mem_attr2);
    }

    #[test]
    fn mem_attr_complex() {
        let mem_attr = MemAttr::Cacheable {
            // 0b01
            outer: CacheablePolicy::WriteBackWriteAllocate,
            // 0b10
            inner: CacheablePolicy::WriteThroughNoWriteAllocate,
            shareable: true,
        };
        let mem_attr_bits = mem_attr.to_bits();
        assert_eq!(
            mem_attr_bits,
            MemAttrBits {
                tex: u3::from_u8(0b101),
                c: true,
                b: false,
                s: true
            }
        );
        let mem_attr2 = mem_attr_bits.decode();
        assert_eq!(Some(mem_attr), mem_attr2);
    }
}