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 | |
Alice Wang | 4be4dd0 | 2023-06-07 07:50:40 +0000 | [diff] [blame] | 17 | use crate::helpers::PVMFW_PAGE_SIZE; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 18 | use aarch64_paging::paging::VirtualAddress; |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 19 | use aarch64_paging::MapError; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 20 | use core::ops::Range; |
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, |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 25 | memory::{PageTable, SIZE_2MB, SIZE_4KB}, |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame] | 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 | |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 29 | /// Returns memory range reserved for the appended payload. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 30 | pub fn appended_payload_range() -> Range<VirtualAddress> { |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 31 | let start = align_up(layout::binary_end().0, SIZE_4KB).unwrap(); |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 32 | // 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] | 33 | let end = align_up(start, SIZE_2MB).unwrap(); |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 34 | VirtualAddress(start)..VirtualAddress(end) |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /// Region allocated for the stack. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 38 | pub fn stack_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 9ddfab5 | 2024-04-24 23:03:46 +0100 | [diff] [blame] | 39 | const STACK_PAGES: usize = 12; |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 40 | |
| 41 | layout::stack_range(STACK_PAGES * PVMFW_PAGE_SIZE) |
| 42 | } |
| 43 | |
| 44 | pub fn init_page_table() -> result::Result<PageTable, MapError> { |
Alice Wang | ee5b180 | 2023-06-07 07:41:54 +0000 | [diff] [blame] | 45 | let mut page_table = PageTable::default(); |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 46 | |
| 47 | // Stack and scratch ranges are explicitly zeroed and flushed before jumping to payload, |
| 48 | // so dirty state management can be omitted. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 49 | page_table.map_data(&layout::scratch_range().into())?; |
| 50 | page_table.map_data(&stack_range().into())?; |
| 51 | page_table.map_code(&layout::text_range().into())?; |
| 52 | page_table.map_rodata(&layout::rodata_range().into())?; |
| 53 | page_table.map_data_dbm(&appended_payload_range().into())?; |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame^] | 54 | if let Err(e) = page_table.map_device(&layout::console_uart_page().into()) { |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 55 | error!("Failed to remap the UART as a dynamic page table entry: {e}"); |
| 56 | return Err(e); |
| 57 | } |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 58 | Ok(page_table) |
| 59 | } |