blob: adcb2fa8ee1e4b372f661cdd37b5712b85306613 [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
Alice Wange243d462023-06-06 15:18:12 +000017pub mod crosvm;
18
Andrew Walbranb88eb012023-07-05 13:58:26 +000019use crate::linker::__stack_chk_guard;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010020use crate::memory::{page_4kb_of, PAGE_SIZE};
Alice Wanga3931aa2023-07-05 12:52:09 +000021use aarch64_paging::paging::VirtualAddress;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010022use core::ops::Range;
23use core::ptr::addr_of;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010024use static_assertions::const_assert_eq;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010025
Alice Wang63f4c9e2023-06-12 09:36:43 +000026/// First address that can't be translated by a level 1 TTBR0_EL1.
27pub const MAX_VIRT_ADDR: usize = 1 << 40;
28
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010029/// Base memory-mapped addresses of the UART devices.
Pierre-Clément Tosi8e92d1a2024-06-18 16:15:14 +010030///
31/// See SERIAL_ADDR in https://crosvm.dev/book/appendix/memory_layout.html#common-layout.
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010032pub const UART_ADDRESSES: [usize; 4] = [0x3f8, 0x2f8, 0x3e8, 0x2e8];
Pierre-Clément Tosi8e92d1a2024-06-18 16:15:14 +010033
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010034/// Address of the single page containing all the UART devices.
35pub const UART_PAGE_ADDR: usize = 0;
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010036const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[0]));
37const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[1]));
38const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[2]));
39const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(UART_ADDRESSES[3]));
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010040
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010041/// Get an address from a linker-defined symbol.
42#[macro_export]
43macro_rules! linker_addr {
44 ($symbol:ident) => {{
Andrew Walbranc06e7342023-07-05 14:00:51 +000045 // SAFETY: We're just getting the address of an extern static symbol provided by the linker,
46 // not dereferencing it.
Alice Wang8b097042023-07-06 14:56:58 +000047 let addr = unsafe { addr_of!($crate::linker::$symbol) as usize };
48 VirtualAddress(addr)
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010049 }};
50}
51
Alice Wanga3931aa2023-07-05 12:52:09 +000052/// Gets the virtual address range between a pair of linker-defined symbols.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010053#[macro_export]
54macro_rules! linker_region {
55 ($begin:ident,$end:ident) => {{
56 let start = linker_addr!($begin);
57 let end = linker_addr!($end);
58
Alice Wang8b097042023-07-06 14:56:58 +000059 start..end
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010060 }};
61}
62
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010063/// Executable code.
Alice Wanga3931aa2023-07-05 12:52:09 +000064pub fn text_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010065 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010066}
67
68/// Read-only data.
Alice Wanga3931aa2023-07-05 12:52:09 +000069pub fn rodata_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010070 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010071}
72
73/// Initialised writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000074pub fn data_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010075 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010076}
77
Alice Wangeff58392023-07-04 13:32:09 +000078/// Zero-initialized writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000079pub fn bss_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010080 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010081}
82
83/// Writable data region for the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000084pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010085 let end = linker_addr!(init_stack_pointer);
Alice Wang8b097042023-07-06 14:56:58 +000086 let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap());
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010087 assert!(start >= linker_addr!(stack_limit));
88
Alice Wang8b097042023-07-06 14:56:58 +000089 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010090}
91
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010092/// All writable sections, excluding the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000093pub fn scratch_range() -> Range<VirtualAddress> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010094 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010095}
96
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010097/// Range of the page at UART_PAGE_ADDR of PAGE_SIZE.
98pub fn console_uart_page() -> Range<VirtualAddress> {
99 VirtualAddress(UART_PAGE_ADDR)..VirtualAddress(UART_PAGE_ADDR + PAGE_SIZE)
Alice Wang807fa592023-06-02 09:54:43 +0000100}
101
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100102/// Read-write data (original).
Alice Wang8b097042023-07-06 14:56:58 +0000103pub fn data_load_address() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100104 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100105}
106
107/// End of the binary image.
Alice Wang8b097042023-07-06 14:56:58 +0000108pub fn binary_end() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100109 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100110}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000111
112/// Value of __stack_chk_guard.
113pub 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}