blob: dd3e0e05b233adb0cae0534e040a1d48e3792ef2 [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
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010023/// Get an address from a linker-defined symbol.
24#[macro_export]
25macro_rules! linker_addr {
26 ($symbol:ident) => {{
27 unsafe { addr_of!($crate::linker::$symbol) as usize }
28 }};
29}
30
31/// Get the address range between a pair of linker-defined symbols.
32#[macro_export]
33macro_rules! linker_region {
34 ($begin:ident,$end:ident) => {{
35 let start = linker_addr!($begin);
36 let end = linker_addr!($end);
37
38 start..end
39 }};
40}
41
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010042/// Memory reserved for the DTB.
43pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010044 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010045}
46
47/// Executable code.
48pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010049 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010050}
51
52/// Read-only data.
53pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010054 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010055}
56
57/// Initialised writable data.
58pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010059 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010060}
61
62/// Zero-initialised writable data.
63pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010064 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010065}
66
67/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010068pub fn stack_range(stack_size: usize) -> Range<usize> {
69 let end = linker_addr!(init_stack_pointer);
70 let start = end.checked_sub(stack_size).unwrap();
71 assert!(start >= linker_addr!(stack_limit));
72
73 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010074}
75
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010076/// All writable sections, excluding the stack.
77pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010078 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010079}
80
Alice Wang807fa592023-06-02 09:54:43 +000081/// UART console range.
82pub fn console_uart_range() -> Range<usize> {
83 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
84
85 BASE_ADDRESS..(BASE_ADDRESS + CONSOLE_LEN)
86}
87
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010088/// Read-write data (original).
89pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010090 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010091}
92
93/// End of the binary image.
94pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010095 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010096}