blob: f7e81702fdbf419be9eb0f0e12a856c10ee850c2 [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
Alice Wang807fa592023-06-02 09:54:43 +000019use crate::console::BASE_ADDRESS;
Andrew Walbranb88eb012023-07-05 13:58:26 +000020use crate::linker::__stack_chk_guard;
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;
24
Alice Wang63f4c9e2023-06-12 09:36:43 +000025/// First address that can't be translated by a level 1 TTBR0_EL1.
26pub const MAX_VIRT_ADDR: usize = 1 << 40;
27
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010028/// Get an address from a linker-defined symbol.
29#[macro_export]
30macro_rules! linker_addr {
31 ($symbol:ident) => {{
Andrew Walbranc06e7342023-07-05 14:00:51 +000032 // SAFETY: We're just getting the address of an extern static symbol provided by the linker,
33 // not dereferencing it.
Alice Wang8b097042023-07-06 14:56:58 +000034 let addr = unsafe { addr_of!($crate::linker::$symbol) as usize };
35 VirtualAddress(addr)
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010036 }};
37}
38
Alice Wanga3931aa2023-07-05 12:52:09 +000039/// Gets the virtual address range between a pair of linker-defined symbols.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010040#[macro_export]
41macro_rules! linker_region {
42 ($begin:ident,$end:ident) => {{
43 let start = linker_addr!($begin);
44 let end = linker_addr!($end);
45
Alice Wang8b097042023-07-06 14:56:58 +000046 start..end
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010047 }};
48}
49
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010050/// Memory reserved for the DTB.
Alice Wanga3931aa2023-07-05 12:52:09 +000051pub fn dtb_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010052 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010053}
54
55/// Executable code.
Alice Wanga3931aa2023-07-05 12:52:09 +000056pub fn text_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010057 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010058}
59
60/// Read-only data.
Alice Wanga3931aa2023-07-05 12:52:09 +000061pub fn rodata_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010062 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010063}
64
65/// Initialised writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000066pub fn data_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010067 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010068}
69
Alice Wangeff58392023-07-04 13:32:09 +000070/// Zero-initialized writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000071pub fn bss_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010072 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010073}
74
75/// Writable data region for the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000076pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010077 let end = linker_addr!(init_stack_pointer);
Alice Wang8b097042023-07-06 14:56:58 +000078 let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap());
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010079 assert!(start >= linker_addr!(stack_limit));
80
Alice Wang8b097042023-07-06 14:56:58 +000081 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010082}
83
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010084/// All writable sections, excluding the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000085pub fn scratch_range() -> Range<VirtualAddress> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010086 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010087}
88
Alice Wang807fa592023-06-02 09:54:43 +000089/// UART console range.
Alice Wanga3931aa2023-07-05 12:52:09 +000090pub fn console_uart_range() -> Range<VirtualAddress> {
Alice Wang807fa592023-06-02 09:54:43 +000091 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
92
Alice Wanga3931aa2023-07-05 12:52:09 +000093 VirtualAddress(BASE_ADDRESS)..VirtualAddress(BASE_ADDRESS + CONSOLE_LEN)
Alice Wang807fa592023-06-02 09:54:43 +000094}
95
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010096/// Read-write data (original).
Alice Wang8b097042023-07-06 14:56:58 +000097pub fn data_load_address() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010098 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010099}
100
101/// End of the binary image.
Alice Wang8b097042023-07-06 14:56:58 +0000102pub fn binary_end() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100103 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100104}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000105
106/// Value of __stack_chk_guard.
107pub 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}