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 | |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 19 | use crate::linker::__stack_chk_guard; |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 20 | use crate::memory::{page_4kb_of, PAGE_SIZE}; |
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; |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 24 | use static_assertions::const_assert_eq; |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 25 | |
Alice Wang | 63f4c9e | 2023-06-12 09:36:43 +0000 | [diff] [blame] | 26 | /// First address that can't be translated by a level 1 TTBR0_EL1. |
| 27 | pub const MAX_VIRT_ADDR: usize = 1 << 40; |
| 28 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 29 | /// Base memory-mapped addresses of the UART devices. |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 30 | /// |
| 31 | /// See SERIAL_ADDR in https://crosvm.dev/book/appendix/memory_layout.html#common-layout. |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 32 | pub const UART_ADDRESSES: [usize; 4] = [0x3f8, 0x2f8, 0x3e8, 0x2e8]; |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 33 | |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 34 | /// Address of the single page containing all the UART devices. |
| 35 | pub const UART_PAGE_ADDR: usize = 0; |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 36 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[0])); |
| 37 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[1])); |
| 38 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[2])); |
| 39 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[3])); |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 40 | |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 41 | /// Get an address from a linker-defined symbol. |
| 42 | #[macro_export] |
| 43 | macro_rules! linker_addr { |
| 44 | ($symbol:ident) => {{ |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 45 | // SAFETY: We're just getting the address of an extern static symbol provided by the linker, |
| 46 | // not dereferencing it. |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 47 | let addr = unsafe { addr_of!($crate::linker::$symbol) as usize }; |
| 48 | VirtualAddress(addr) |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 49 | }}; |
| 50 | } |
| 51 | |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 52 | /// 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] | 53 | #[macro_export] |
| 54 | macro_rules! linker_region { |
| 55 | ($begin:ident,$end:ident) => {{ |
| 56 | let start = linker_addr!($begin); |
| 57 | let end = linker_addr!($end); |
| 58 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 59 | start..end |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 60 | }}; |
| 61 | } |
| 62 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 63 | /// Executable code. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 64 | pub fn text_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 65 | linker_region!(text_begin, text_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// Read-only data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 69 | pub fn rodata_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 70 | linker_region!(rodata_begin, rodata_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// Initialised writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 74 | pub fn data_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 75 | linker_region!(data_begin, data_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 78 | /// Zero-initialized writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 79 | pub fn bss_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 80 | linker_region!(bss_begin, bss_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// Writable data region for the stack. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 84 | pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 85 | let end = linker_addr!(init_stack_pointer); |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 86 | let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap()); |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 87 | assert!(start >= linker_addr!(stack_limit)); |
| 88 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 89 | start..end |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Pierre-Clément Tosi | 8bb3d72 | 2023-04-21 16:10:56 +0100 | [diff] [blame] | 92 | /// All writable sections, excluding the stack. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 93 | pub fn scratch_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | ccc1138 | 2023-04-21 16:44:53 +0100 | [diff] [blame] | 94 | linker_region!(eh_stack_limit, bss_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 97 | /// Range of the page at UART_PAGE_ADDR of PAGE_SIZE. |
| 98 | pub fn console_uart_page() -> Range<VirtualAddress> { |
| 99 | VirtualAddress(UART_PAGE_ADDR)..VirtualAddress(UART_PAGE_ADDR + PAGE_SIZE) |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 102 | /// Read-write data (original). |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 103 | pub fn data_load_address() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 104 | linker_addr!(data_lma) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /// End of the binary image. |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 108 | pub fn binary_end() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 109 | linker_addr!(bin_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 110 | } |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 111 | |
| 112 | /// Value of __stack_chk_guard. |
| 113 | pub fn stack_chk_guard() -> u64 { |
| 114 | // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If |
| 115 | // it does, then there could be undefined behaviour all over the program, but we want to at |
| 116 | // least have a chance at catching it. |
| 117 | unsafe { addr_of!(__stack_chk_guard).read_volatile() } |
| 118 | } |