blob: ffa29e78392e8eb3d16191bd822c84bfb7c52793 [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.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010034 unsafe { addr_of!($crate::linker::$symbol) as usize }
35 }};
36}
37
Alice Wanga3931aa2023-07-05 12:52:09 +000038/// Gets the virtual address range between a pair of linker-defined symbols.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010039#[macro_export]
40macro_rules! linker_region {
41 ($begin:ident,$end:ident) => {{
42 let start = linker_addr!($begin);
43 let end = linker_addr!($end);
44
Alice Wanga3931aa2023-07-05 12:52:09 +000045 VirtualAddress(start)..VirtualAddress(end)
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010046 }};
47}
48
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010049/// Memory reserved for the DTB.
Alice Wanga3931aa2023-07-05 12:52:09 +000050pub fn dtb_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010051 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010052}
53
54/// Executable code.
Alice Wanga3931aa2023-07-05 12:52:09 +000055pub fn text_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010056 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010057}
58
59/// Read-only data.
Alice Wanga3931aa2023-07-05 12:52:09 +000060pub fn rodata_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010061 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010062}
63
64/// Initialised writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000065pub fn data_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010066 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010067}
68
Alice Wangeff58392023-07-04 13:32:09 +000069/// Zero-initialized writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000070pub fn bss_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010071 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010072}
73
74/// Writable data region for the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000075pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010076 let end = linker_addr!(init_stack_pointer);
77 let start = end.checked_sub(stack_size).unwrap();
78 assert!(start >= linker_addr!(stack_limit));
79
Alice Wanga3931aa2023-07-05 12:52:09 +000080 VirtualAddress(start)..VirtualAddress(end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010081}
82
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010083/// All writable sections, excluding the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000084pub fn scratch_range() -> Range<VirtualAddress> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010085 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010086}
87
Alice Wang807fa592023-06-02 09:54:43 +000088/// UART console range.
Alice Wanga3931aa2023-07-05 12:52:09 +000089pub fn console_uart_range() -> Range<VirtualAddress> {
Alice Wang807fa592023-06-02 09:54:43 +000090 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
91
Alice Wanga3931aa2023-07-05 12:52:09 +000092 VirtualAddress(BASE_ADDRESS)..VirtualAddress(BASE_ADDRESS + CONSOLE_LEN)
Alice Wang807fa592023-06-02 09:54:43 +000093}
94
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010095/// Read-write data (original).
96pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010097 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010098}
99
100/// End of the binary image.
101pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100102 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100103}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000104
105/// Value of __stack_chk_guard.
106pub fn stack_chk_guard() -> u64 {
107 // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If
108 // it does, then there could be undefined behaviour all over the program, but we want to at
109 // least have a chance at catching it.
110 unsafe { addr_of!(__stack_chk_guard).read_volatile() }
111}