blob: e7a645b0a4bdce605b46de2ce870dbbefd74706a [file] [log] [blame]
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +01001// 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 Wailes4cdd19f2024-12-04 11:13:57 -080017#![allow(unused_unsafe)]
18
Alice Wange243d462023-06-06 15:18:12 +000019pub mod crosvm;
20
Andrew Walbranb88eb012023-07-05 13:58:26 +000021use crate::linker::__stack_chk_guard;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010022use crate::memory::{page_4kb_of, PAGE_SIZE};
Alice Wanga3931aa2023-07-05 12:52:09 +000023use aarch64_paging::paging::VirtualAddress;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010024use core::ops::Range;
25use core::ptr::addr_of;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010026use static_assertions::const_assert_eq;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010027
Alice Wang63f4c9e2023-06-12 09:36:43 +000028/// First address that can't be translated by a level 1 TTBR0_EL1.
29pub const MAX_VIRT_ADDR: usize = 1 << 40;
30
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010031/// Base memory-mapped addresses of the UART devices.
Pierre-Clément Tosi8e92d1a2024-06-18 16:15:14 +010032///
33/// See SERIAL_ADDR in https://crosvm.dev/book/appendix/memory_layout.html#common-layout.
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010034pub const UART_ADDRESSES: [usize; 4] = [0x3f8, 0x2f8, 0x3e8, 0x2e8];
Pierre-Clément Tosi8e92d1a2024-06-18 16:15:14 +010035
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010036/// Address of the single page containing all the UART devices.
37pub const UART_PAGE_ADDR: usize = 0;
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010038const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[0]));
39const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[1]));
40const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[2]));
41const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[3]));
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010042
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010043/// Get an address from a linker-defined symbol.
44#[macro_export]
45macro_rules! linker_addr {
46 ($symbol:ident) => {{
Andrew Walbranc06e7342023-07-05 14:00:51 +000047 // SAFETY: We're just getting the address of an extern static symbol provided by the linker,
48 // not dereferencing it.
Alice Wang8b097042023-07-06 14:56:58 +000049 let addr = unsafe { addr_of!($crate::linker::$symbol) as usize };
50 VirtualAddress(addr)
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010051 }};
52}
53
Alice Wanga3931aa2023-07-05 12:52:09 +000054/// Gets the virtual address range between a pair of linker-defined symbols.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010055#[macro_export]
56macro_rules! linker_region {
57 ($begin:ident,$end:ident) => {{
58 let start = linker_addr!($begin);
59 let end = linker_addr!($end);
60
Alice Wang8b097042023-07-06 14:56:58 +000061 start..end
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010062 }};
63}
64
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010065/// Executable code.
Alice Wanga3931aa2023-07-05 12:52:09 +000066pub fn text_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010067 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010068}
69
70/// Read-only data.
Alice Wanga3931aa2023-07-05 12:52:09 +000071pub fn rodata_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010072 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010073}
74
Pierre-Clément Tosib9963992024-11-28 22:53:02 +000075/// Region which may contain a footer appended to the binary at load time.
76pub fn image_footer_range() -> Range<VirtualAddress> {
77 linker_region!(image_footer_begin, image_footer_end)
78}
79
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010080/// Initialised writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000081pub fn data_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010082 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010083}
84
Alice Wangeff58392023-07-04 13:32:09 +000085/// Zero-initialized writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000086pub fn bss_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010087 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010088}
89
Pierre-Clément Tosi0b02a2b2024-11-28 22:48:27 +000090/// Writable data region for .data and .bss.
91pub fn data_bss_range() -> Range<VirtualAddress> {
92 linker_region!(data_begin, bss_end)
93}
94
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010095/// Writable data region for the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000096pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010097 let end = linker_addr!(init_stack_pointer);
Alice Wang8b097042023-07-06 14:56:58 +000098 let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap());
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010099 assert!(start >= linker_addr!(stack_limit));
100
Alice Wang8b097042023-07-06 14:56:58 +0000101 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100102}
103
Pierre-Clément Tosi0b02a2b2024-11-28 22:48:27 +0000104/// Writable data region for the exception handler stack.
105pub fn eh_stack_range() -> Range<VirtualAddress> {
106 linker_region!(eh_stack_limit, init_eh_stack_pointer)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100107}
108
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +0100109/// Range of the page at UART_PAGE_ADDR of PAGE_SIZE.
110pub fn console_uart_page() -> Range<VirtualAddress> {
111 VirtualAddress(UART_PAGE_ADDR)..VirtualAddress(UART_PAGE_ADDR + PAGE_SIZE)
Alice Wang807fa592023-06-02 09:54:43 +0000112}
113
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100114/// Read-write data (original).
Alice Wang8b097042023-07-06 14:56:58 +0000115pub fn data_load_address() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100116 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100117}
118
119/// End of the binary image.
Alice Wang8b097042023-07-06 14:56:58 +0000120pub fn binary_end() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100121 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100122}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000123
124/// Value of __stack_chk_guard.
125pub fn stack_chk_guard() -> u64 {
126 // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If
127 // it does, then there could be undefined behaviour all over the program, but we want to at
128 // least have a chance at catching it.
129 unsafe { addr_of!(__stack_chk_guard).read_volatile() }
130}