Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Memory management. |
| 16 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame^] | 17 | use crate::helpers; |
Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 18 | use aarch64_paging::idmap::IdMap; |
| 19 | use aarch64_paging::paging::Attributes; |
| 20 | use aarch64_paging::paging::MemoryRegion; |
| 21 | use aarch64_paging::MapError; |
| 22 | use core::ops::Range; |
| 23 | use vmbase::layout; |
| 24 | |
| 25 | // We assume that: |
| 26 | // - MAIR_EL1.Attr0 = "Device-nGnRE memory" (0b0000_0100) |
| 27 | // - MAIR_EL1.Attr1 = "Normal memory, Outer & Inner WB Non-transient, R/W-Allocate" (0b1111_1111) |
| 28 | const MEMORY: Attributes = Attributes::NORMAL.union(Attributes::NON_GLOBAL); |
| 29 | const DEVICE: Attributes = Attributes::DEVICE_NGNRE.union(Attributes::EXECUTE_NEVER); |
| 30 | const CODE: Attributes = MEMORY.union(Attributes::READ_ONLY); |
| 31 | const DATA: Attributes = MEMORY.union(Attributes::EXECUTE_NEVER); |
| 32 | const RODATA: Attributes = DATA.union(Attributes::READ_ONLY); |
| 33 | |
| 34 | /// High-level API for managing MMU mappings. |
| 35 | pub struct PageTable { |
| 36 | idmap: IdMap, |
| 37 | } |
| 38 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame^] | 39 | fn appended_payload_range() -> Range<usize> { |
| 40 | let start = helpers::align_up(layout::binary_end(), helpers::SIZE_4KB).unwrap(); |
| 41 | // pvmfw is contained in a 2MiB region so the payload can't be larger than the 2MiB alignment. |
| 42 | let end = helpers::align_up(start, helpers::SIZE_2MB).unwrap(); |
| 43 | |
| 44 | start..end |
| 45 | } |
| 46 | |
Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 47 | impl PageTable { |
| 48 | const ASID: usize = 1; |
| 49 | const ROOT_LEVEL: usize = 1; |
| 50 | |
| 51 | /// Creates an instance pre-populated with pvmfw's binary layout. |
| 52 | pub fn from_static_layout() -> Result<Self, MapError> { |
| 53 | let mut page_table = Self { idmap: IdMap::new(Self::ASID, Self::ROOT_LEVEL) }; |
| 54 | |
| 55 | page_table.map_code(&layout::text_range())?; |
| 56 | page_table.map_data(&layout::writable_region())?; |
| 57 | page_table.map_rodata(&layout::rodata_range())?; |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame^] | 58 | page_table.map_data(&appended_payload_range())?; |
Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 59 | |
| 60 | Ok(page_table) |
| 61 | } |
| 62 | |
| 63 | pub unsafe fn activate(&mut self) { |
| 64 | self.idmap.activate() |
| 65 | } |
| 66 | |
| 67 | pub fn map_device(&mut self, range: &Range<usize>) -> Result<(), MapError> { |
| 68 | self.map_range(range, DEVICE) |
| 69 | } |
| 70 | |
| 71 | pub fn map_data(&mut self, range: &Range<usize>) -> Result<(), MapError> { |
| 72 | self.map_range(range, DATA) |
| 73 | } |
| 74 | |
| 75 | pub fn map_code(&mut self, range: &Range<usize>) -> Result<(), MapError> { |
| 76 | self.map_range(range, CODE) |
| 77 | } |
| 78 | |
| 79 | pub fn map_rodata(&mut self, range: &Range<usize>) -> Result<(), MapError> { |
| 80 | self.map_range(range, RODATA) |
| 81 | } |
| 82 | |
| 83 | fn map_range(&mut self, range: &Range<usize>, attr: Attributes) -> Result<(), MapError> { |
| 84 | self.idmap.map_range(&MemoryRegion::new(range.start, range.end), attr) |
| 85 | } |
| 86 | } |