blob: 00d7f9a50ca9d4eae5eb532cc16a2f0220a8ad94 [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;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010021use core::ops::Range;
22use core::ptr::addr_of;
23
Alice Wang63f4c9e2023-06-12 09:36:43 +000024/// First address that can't be translated by a level 1 TTBR0_EL1.
25pub const MAX_VIRT_ADDR: usize = 1 << 40;
26
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010027/// Get an address from a linker-defined symbol.
28#[macro_export]
29macro_rules! linker_addr {
30 ($symbol:ident) => {{
31 unsafe { addr_of!($crate::linker::$symbol) as usize }
32 }};
33}
34
35/// Get the address range between a pair of linker-defined symbols.
36#[macro_export]
37macro_rules! linker_region {
38 ($begin:ident,$end:ident) => {{
39 let start = linker_addr!($begin);
40 let end = linker_addr!($end);
41
42 start..end
43 }};
44}
45
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010046/// Memory reserved for the DTB.
47pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010048 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010049}
50
51/// Executable code.
52pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010053 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010054}
55
56/// Read-only data.
57pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010058 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010059}
60
61/// Initialised writable data.
62pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010063 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010064}
65
66/// Zero-initialised writable data.
67pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010068 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010069}
70
71/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010072pub fn stack_range(stack_size: usize) -> Range<usize> {
73 let end = linker_addr!(init_stack_pointer);
74 let start = end.checked_sub(stack_size).unwrap();
75 assert!(start >= linker_addr!(stack_limit));
76
77 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010078}
79
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010080/// All writable sections, excluding the stack.
81pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010082 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010083}
84
Alice Wang807fa592023-06-02 09:54:43 +000085/// UART console range.
86pub fn console_uart_range() -> Range<usize> {
87 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
88
89 BASE_ADDRESS..(BASE_ADDRESS + CONSOLE_LEN)
90}
91
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010092/// Read-write data (original).
93pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010094 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010095}
96
97/// End of the binary image.
98pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010099 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100100}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000101
102/// Value of __stack_chk_guard.
103pub fn stack_chk_guard() -> u64 {
104 // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If
105 // it does, then there could be undefined behaviour all over the program, but we want to at
106 // least have a chance at catching it.
107 unsafe { addr_of!(__stack_chk_guard).read_volatile() }
108}