Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [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 layout. |
| 16 | |
Alice Wang | e243d46 | 2023-06-06 15:18:12 +0000 | [diff] [blame] | 17 | pub mod crosvm; |
| 18 | |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 19 | use crate::console::BASE_ADDRESS; |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 20 | use crate::linker::__stack_chk_guard; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 21 | use aarch64_paging::paging::VirtualAddress; |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 22 | use core::ops::Range; |
| 23 | use core::ptr::addr_of; |
| 24 | |
Alice Wang | 63f4c9e | 2023-06-12 09:36:43 +0000 | [diff] [blame] | 25 | /// First address that can't be translated by a level 1 TTBR0_EL1. |
| 26 | pub const MAX_VIRT_ADDR: usize = 1 << 40; |
| 27 | |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 28 | /// Get an address from a linker-defined symbol. |
| 29 | #[macro_export] |
| 30 | macro_rules! linker_addr { |
| 31 | ($symbol:ident) => {{ |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 32 | // SAFETY: We're just getting the address of an extern static symbol provided by the linker, |
| 33 | // not dereferencing it. |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 34 | let addr = unsafe { addr_of!($crate::linker::$symbol) as usize }; |
| 35 | VirtualAddress(addr) |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 36 | }}; |
| 37 | } |
| 38 | |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 39 | /// Gets the virtual address range between a pair of linker-defined symbols. |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 40 | #[macro_export] |
| 41 | macro_rules! linker_region { |
| 42 | ($begin:ident,$end:ident) => {{ |
| 43 | let start = linker_addr!($begin); |
| 44 | let end = linker_addr!($end); |
| 45 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 46 | start..end |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 47 | }}; |
| 48 | } |
| 49 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 50 | /// Memory reserved for the DTB. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 51 | pub fn dtb_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 52 | linker_region!(dtb_begin, dtb_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// Executable code. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 56 | pub fn text_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 57 | linker_region!(text_begin, text_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /// Read-only data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 61 | pub fn rodata_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 62 | linker_region!(rodata_begin, rodata_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /// Initialised writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 66 | pub fn data_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 67 | linker_region!(data_begin, data_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 70 | /// Zero-initialized writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 71 | pub fn bss_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 72 | linker_region!(bss_begin, bss_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /// Writable data region for the stack. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 76 | pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 77 | let end = linker_addr!(init_stack_pointer); |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 78 | let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap()); |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 79 | assert!(start >= linker_addr!(stack_limit)); |
| 80 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 81 | start..end |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Pierre-Clément Tosi | 8bb3d72 | 2023-04-21 16:10:56 +0100 | [diff] [blame] | 84 | /// All writable sections, excluding the stack. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 85 | pub fn scratch_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | ccc1138 | 2023-04-21 16:44:53 +0100 | [diff] [blame] | 86 | linker_region!(eh_stack_limit, bss_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 89 | /// UART console range. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 90 | pub fn console_uart_range() -> Range<VirtualAddress> { |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 91 | const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register. |
| 92 | |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 93 | VirtualAddress(BASE_ADDRESS)..VirtualAddress(BASE_ADDRESS + CONSOLE_LEN) |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 96 | /// Read-write data (original). |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 97 | pub fn data_load_address() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 98 | linker_addr!(data_lma) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// End of the binary image. |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame^] | 102 | pub fn binary_end() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 103 | linker_addr!(bin_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 104 | } |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 105 | |
| 106 | /// Value of __stack_chk_guard. |
| 107 | pub fn stack_chk_guard() -> u64 { |
| 108 | // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If |
| 109 | // it does, then there could be undefined behaviour all over the program, but we want to at |
| 110 | // least have a chance at catching it. |
| 111 | unsafe { addr_of!(__stack_chk_guard).read_volatile() } |
| 112 | } |