Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +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 | //! Low-level allocation and tracking of main memory. |
| 16 | |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 17 | #![deny(unsafe_op_in_unsafe_fn)] |
| 18 | |
Alice Wang | 4be4dd0 | 2023-06-07 07:50:40 +0000 | [diff] [blame] | 19 | use crate::helpers::PVMFW_PAGE_SIZE; |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 20 | use aarch64_paging::MapError; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 21 | use core::result; |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame^] | 22 | use log::error; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 23 | use vmbase::{ |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame^] | 24 | layout, |
| 25 | memory::{MemoryRange, PageTable, SIZE_2MB, SIZE_4KB}, |
| 26 | util::align_up, |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 27 | }; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 28 | |
Jiyong Park | 0ee6539 | 2023-03-27 20:52:45 +0900 | [diff] [blame] | 29 | /// First address that can't be translated by a level 1 TTBR0_EL1. |
| 30 | pub const MAX_ADDR: usize = 1 << 40; |
| 31 | |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 32 | /// Returns memory range reserved for the appended payload. |
Alice Wang | 446146a | 2023-06-07 08:18:46 +0000 | [diff] [blame] | 33 | pub fn appended_payload_range() -> MemoryRange { |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 34 | let start = align_up(layout::binary_end(), SIZE_4KB).unwrap(); |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 35 | // pvmfw is contained in a 2MiB region so the payload can't be larger than the 2MiB alignment. |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 36 | let end = align_up(start, SIZE_2MB).unwrap(); |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 37 | start..end |
| 38 | } |
| 39 | |
| 40 | /// Region allocated for the stack. |
Alice Wang | 446146a | 2023-06-07 08:18:46 +0000 | [diff] [blame] | 41 | pub fn stack_range() -> MemoryRange { |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 42 | const STACK_PAGES: usize = 8; |
| 43 | |
| 44 | layout::stack_range(STACK_PAGES * PVMFW_PAGE_SIZE) |
| 45 | } |
| 46 | |
| 47 | pub fn init_page_table() -> result::Result<PageTable, MapError> { |
Alice Wang | ee5b180 | 2023-06-07 07:41:54 +0000 | [diff] [blame] | 48 | let mut page_table = PageTable::default(); |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 49 | |
| 50 | // Stack and scratch ranges are explicitly zeroed and flushed before jumping to payload, |
| 51 | // so dirty state management can be omitted. |
| 52 | page_table.map_data(&layout::scratch_range())?; |
| 53 | page_table.map_data(&stack_range())?; |
| 54 | page_table.map_code(&layout::text_range())?; |
| 55 | page_table.map_rodata(&layout::rodata_range())?; |
| 56 | page_table.map_data_dbm(&appended_payload_range())?; |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 57 | if let Err(e) = page_table.map_device(&layout::console_uart_range()) { |
| 58 | error!("Failed to remap the UART as a dynamic page table entry: {e}"); |
| 59 | return Err(e); |
| 60 | } |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 61 | Ok(page_table) |
| 62 | } |