blob: fa94e85c853f55f5e01806b7cd38709a912bd704 [file] [log] [blame]
Pierre-Clément Tosia8a4a202022-11-03 14:16:46 +00001// 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 Tosi20b60962022-10-17 13:35:27 +010017use crate::helpers;
Pierre-Clément Tosia8a4a202022-11-03 14:16:46 +000018use aarch64_paging::idmap::IdMap;
19use aarch64_paging::paging::Attributes;
20use aarch64_paging::paging::MemoryRegion;
21use aarch64_paging::MapError;
22use core::ops::Range;
23use 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)
28const MEMORY: Attributes = Attributes::NORMAL.union(Attributes::NON_GLOBAL);
29const DEVICE: Attributes = Attributes::DEVICE_NGNRE.union(Attributes::EXECUTE_NEVER);
30const CODE: Attributes = MEMORY.union(Attributes::READ_ONLY);
31const DATA: Attributes = MEMORY.union(Attributes::EXECUTE_NEVER);
32const RODATA: Attributes = DATA.union(Attributes::READ_ONLY);
33
34/// High-level API for managing MMU mappings.
35pub struct PageTable {
36 idmap: IdMap,
37}
38
Pierre-Clément Tosi20b60962022-10-17 13:35:27 +010039fn 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 Tosia8a4a202022-11-03 14:16:46 +000047impl 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 Tosi20b60962022-10-17 13:35:27 +010058 page_table.map_data(&appended_payload_range())?;
Pierre-Clément Tosia8a4a202022-11-03 14:16:46 +000059
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}