blob: f67e5189411cdc437cec3bc4520af775e3f0f6f5 [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;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010020use core::ops::Range;
21use core::ptr::addr_of;
22
Alice Wang63f4c9e2023-06-12 09:36:43 +000023/// First address that can't be translated by a level 1 TTBR0_EL1.
24pub const MAX_VIRT_ADDR: usize = 1 << 40;
25
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010026/// Get an address from a linker-defined symbol.
27#[macro_export]
28macro_rules! linker_addr {
29 ($symbol:ident) => {{
30 unsafe { addr_of!($crate::linker::$symbol) as usize }
31 }};
32}
33
34/// Get the address range between a pair of linker-defined symbols.
35#[macro_export]
36macro_rules! linker_region {
37 ($begin:ident,$end:ident) => {{
38 let start = linker_addr!($begin);
39 let end = linker_addr!($end);
40
41 start..end
42 }};
43}
44
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010045/// Memory reserved for the DTB.
46pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010047 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010048}
49
50/// Executable code.
51pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010052 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010053}
54
55/// Read-only data.
56pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010057 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010058}
59
60/// Initialised writable data.
61pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010062 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010063}
64
Alice Wangeff58392023-07-04 13:32:09 +000065/// Zero-initialized writable data.
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010066pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010067 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010068}
69
70/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010071pub fn stack_range(stack_size: usize) -> Range<usize> {
72 let end = linker_addr!(init_stack_pointer);
73 let start = end.checked_sub(stack_size).unwrap();
74 assert!(start >= linker_addr!(stack_limit));
75
76 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010077}
78
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010079/// All writable sections, excluding the stack.
80pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010081 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010082}
83
Alice Wang807fa592023-06-02 09:54:43 +000084/// UART console range.
85pub fn console_uart_range() -> Range<usize> {
86 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
87
88 BASE_ADDRESS..(BASE_ADDRESS + CONSOLE_LEN)
89}
90
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010091/// Read-write data (original).
92pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010093 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010094}
95
96/// End of the binary image.
97pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010098 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010099}