blob: bca5115acd3af71828daa72cae54b4879369bee2 [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) => {{
Andrew Walbranc06e7342023-07-05 14:00:51 +000031 // SAFETY: We're just getting the address of an extern static symbol provided by the linker,
32 // not dereferencing it.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010033 unsafe { addr_of!($crate::linker::$symbol) as usize }
34 }};
35}
36
37/// Get the address range between a pair of linker-defined symbols.
38#[macro_export]
39macro_rules! linker_region {
40 ($begin:ident,$end:ident) => {{
41 let start = linker_addr!($begin);
42 let end = linker_addr!($end);
43
44 start..end
45 }};
46}
47
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010048/// Memory reserved for the DTB.
49pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010050 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010051}
52
53/// Executable code.
54pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010055 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010056}
57
58/// Read-only data.
59pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010060 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010061}
62
63/// Initialised writable data.
64pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010065 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010066}
67
Alice Wangeff58392023-07-04 13:32:09 +000068/// Zero-initialized writable data.
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010069pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010070 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010071}
72
73/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010074pub fn stack_range(stack_size: usize) -> Range<usize> {
75 let end = linker_addr!(init_stack_pointer);
76 let start = end.checked_sub(stack_size).unwrap();
77 assert!(start >= linker_addr!(stack_limit));
78
79 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010080}
81
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010082/// All writable sections, excluding the stack.
83pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010084 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010085}
86
Alice Wang807fa592023-06-02 09:54:43 +000087/// UART console range.
88pub fn console_uart_range() -> Range<usize> {
89 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
90
91 BASE_ADDRESS..(BASE_ADDRESS + CONSOLE_LEN)
92}
93
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010094/// Read-write data (original).
95pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010096 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010097}
98
99/// End of the binary image.
100pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100101 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100102}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000103
104/// Value of __stack_chk_guard.
105pub fn stack_chk_guard() -> u64 {
106 // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If
107 // it does, then there could be undefined behaviour all over the program, but we want to at
108 // least have a chance at catching it.
109 unsafe { addr_of!(__stack_chk_guard).read_volatile() }
110}