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 | |
Chris Wailes | 4cdd19f | 2024-12-04 11:13:57 -0800 | [diff] [blame] | 17 | #![allow(unused_unsafe)] |
| 18 | |
Alice Wang | e243d46 | 2023-06-06 15:18:12 +0000 | [diff] [blame] | 19 | pub mod crosvm; |
| 20 | |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 21 | use crate::linker::__stack_chk_guard; |
Pierre-Clément Tosi | eba8316 | 2024-11-02 12:11:48 +0000 | [diff] [blame] | 22 | use crate::memory::{max_stack_size, page_4kb_of, PAGE_SIZE}; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 23 | use aarch64_paging::paging::VirtualAddress; |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 24 | use core::ops::Range; |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 25 | use static_assertions::const_assert_eq; |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 26 | |
Alice Wang | 63f4c9e | 2023-06-12 09:36:43 +0000 | [diff] [blame] | 27 | /// First address that can't be translated by a level 1 TTBR0_EL1. |
| 28 | pub const MAX_VIRT_ADDR: usize = 1 << 40; |
| 29 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 30 | /// Base memory-mapped addresses of the UART devices. |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 31 | /// |
| 32 | /// 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] | 33 | pub const UART_ADDRESSES: [usize; 4] = [0x3f8, 0x2f8, 0x3e8, 0x2e8]; |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 34 | |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 35 | /// Address of the single page containing all the UART devices. |
| 36 | pub const UART_PAGE_ADDR: usize = 0; |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 37 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[0])); |
| 38 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[1])); |
| 39 | const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[2])); |
| 40 | 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] | 41 | |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 42 | /// Get an address from a linker-defined symbol. |
| 43 | #[macro_export] |
| 44 | macro_rules! linker_addr { |
| 45 | ($symbol:ident) => {{ |
Andrew Walbran | 72e4e12 | 2025-01-15 12:12:33 +0000 | [diff] [blame] | 46 | let addr = (&raw const $crate::linker::$symbol) as usize; |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 47 | VirtualAddress(addr) |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 48 | }}; |
| 49 | } |
| 50 | |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 51 | /// 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] | 52 | #[macro_export] |
| 53 | macro_rules! linker_region { |
| 54 | ($begin:ident,$end:ident) => {{ |
| 55 | let start = linker_addr!($begin); |
| 56 | let end = linker_addr!($end); |
| 57 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 58 | start..end |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 59 | }}; |
| 60 | } |
| 61 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 62 | /// Executable code. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 63 | pub fn text_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 64 | linker_region!(text_begin, text_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /// Read-only data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 68 | pub fn rodata_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 69 | linker_region!(rodata_begin, rodata_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Pierre-Clément Tosi | b996399 | 2024-11-28 22:53:02 +0000 | [diff] [blame] | 72 | /// Region which may contain a footer appended to the binary at load time. |
| 73 | pub fn image_footer_range() -> Range<VirtualAddress> { |
| 74 | linker_region!(image_footer_begin, image_footer_end) |
| 75 | } |
| 76 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 77 | /// Initialised writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 78 | pub fn data_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 79 | linker_region!(data_begin, data_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 82 | /// Zero-initialized writable data. |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 83 | pub fn bss_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 84 | linker_region!(bss_begin, bss_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Pierre-Clément Tosi | 0b02a2b | 2024-11-28 22:48:27 +0000 | [diff] [blame] | 87 | /// Writable data region for .data and .bss. |
| 88 | pub fn data_bss_range() -> Range<VirtualAddress> { |
| 89 | linker_region!(data_begin, bss_end) |
| 90 | } |
| 91 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 92 | /// Writable data region for the stack. |
Pierre-Clément Tosi | eba8316 | 2024-11-02 12:11:48 +0000 | [diff] [blame] | 93 | pub fn stack_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 94 | let end = linker_addr!(init_stack_pointer); |
Pierre-Clément Tosi | eba8316 | 2024-11-02 12:11:48 +0000 | [diff] [blame] | 95 | let start = if let Some(stack_size) = max_stack_size() { |
| 96 | assert_eq!(stack_size % PAGE_SIZE, 0); |
| 97 | let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap()); |
| 98 | assert!(start >= linker_addr!(stack_limit)); |
| 99 | start |
| 100 | } else { |
| 101 | linker_addr!(stack_limit) |
| 102 | }; |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 103 | |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 104 | start..end |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Pierre-Clément Tosi | 0b02a2b | 2024-11-28 22:48:27 +0000 | [diff] [blame] | 107 | /// Writable data region for the exception handler stack. |
| 108 | pub fn eh_stack_range() -> Range<VirtualAddress> { |
| 109 | linker_region!(eh_stack_limit, init_eh_stack_pointer) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 112 | /// Range of the page at UART_PAGE_ADDR of PAGE_SIZE. |
| 113 | pub fn console_uart_page() -> Range<VirtualAddress> { |
| 114 | VirtualAddress(UART_PAGE_ADDR)..VirtualAddress(UART_PAGE_ADDR + PAGE_SIZE) |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 117 | /// Read-write data (original). |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 118 | pub fn data_load_address() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 119 | linker_addr!(data_lma) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /// End of the binary image. |
Alice Wang | 8b09704 | 2023-07-06 14:56:58 +0000 | [diff] [blame] | 123 | pub fn binary_end() -> VirtualAddress { |
Pierre-Clément Tosi | 5835d34 | 2023-04-21 18:02:32 +0100 | [diff] [blame] | 124 | linker_addr!(bin_end) |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 125 | } |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 126 | |
| 127 | /// Value of __stack_chk_guard. |
| 128 | pub fn stack_chk_guard() -> u64 { |
| 129 | // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If |
| 130 | // it does, then there could be undefined behaviour all over the program, but we want to at |
| 131 | // least have a chance at catching it. |
Andrew Walbran | 72e4e12 | 2025-01-15 12:12:33 +0000 | [diff] [blame] | 132 | unsafe { (&raw const __stack_chk_guard).read_volatile() } |
Andrew Walbran | b88eb01 | 2023-07-05 13:58:26 +0000 | [diff] [blame] | 133 | } |