blob: 43bcb2c2cbfaea7d4effd84bd598eecaf7d58a80 [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 Wang807fa592023-06-02 09:54:43 +000017use crate::console::BASE_ADDRESS;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010018use core::ops::Range;
19use core::ptr::addr_of;
20
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010021/// Get an address from a linker-defined symbol.
22#[macro_export]
23macro_rules! linker_addr {
24 ($symbol:ident) => {{
25 unsafe { addr_of!($crate::linker::$symbol) as usize }
26 }};
27}
28
29/// Get the address range between a pair of linker-defined symbols.
30#[macro_export]
31macro_rules! linker_region {
32 ($begin:ident,$end:ident) => {{
33 let start = linker_addr!($begin);
34 let end = linker_addr!($end);
35
36 start..end
37 }};
38}
39
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010040/// Memory reserved for the DTB.
41pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010042 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010043}
44
45/// Executable code.
46pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010047 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010048}
49
50/// Read-only data.
51pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010052 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010053}
54
55/// Initialised writable data.
56pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010057 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010058}
59
60/// Zero-initialised writable data.
61pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010062 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010063}
64
65/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010066pub fn stack_range(stack_size: usize) -> Range<usize> {
67 let end = linker_addr!(init_stack_pointer);
68 let start = end.checked_sub(stack_size).unwrap();
69 assert!(start >= linker_addr!(stack_limit));
70
71 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010072}
73
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010074/// All writable sections, excluding the stack.
75pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010076 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010077}
78
Alice Wang807fa592023-06-02 09:54:43 +000079/// UART console range.
80pub fn console_uart_range() -> Range<usize> {
81 const CONSOLE_LEN: usize = 1; // `uart::Uart` only uses one u8 register.
82
83 BASE_ADDRESS..(BASE_ADDRESS + CONSOLE_LEN)
84}
85
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010086/// Read-write data (original).
87pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010088 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010089}
90
91/// End of the binary image.
92pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010093 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010094}