blob: 993141d607d5497fe6a8e260f3737fdd9234c99d [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
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010019use crate::console;
Andrew Walbranb88eb012023-07-05 13:58:26 +000020use crate::linker::__stack_chk_guard;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010021use crate::memory::{page_4kb_of, PAGE_SIZE};
Alice Wanga3931aa2023-07-05 12:52:09 +000022use aarch64_paging::paging::VirtualAddress;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010023use core::ops::Range;
24use core::ptr::addr_of;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010025use static_assertions::const_assert_eq;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010026
Alice Wang63f4c9e2023-06-12 09:36:43 +000027/// First address that can't be translated by a level 1 TTBR0_EL1.
28pub const MAX_VIRT_ADDR: usize = 1 << 40;
29
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010030/// Address of the single page containing all the UART devices.
31pub const UART_PAGE_ADDR: usize = 0;
32const_assert_eq!(UART_PAGE_ADDR, page_4kb_of(console::BASE_ADDRESS));
33
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010034/// Get an address from a linker-defined symbol.
35#[macro_export]
36macro_rules! linker_addr {
37 ($symbol:ident) => {{
Andrew Walbranc06e7342023-07-05 14:00:51 +000038 // SAFETY: We're just getting the address of an extern static symbol provided by the linker,
39 // not dereferencing it.
Alice Wang8b097042023-07-06 14:56:58 +000040 let addr = unsafe { addr_of!($crate::linker::$symbol) as usize };
41 VirtualAddress(addr)
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010042 }};
43}
44
Alice Wanga3931aa2023-07-05 12:52:09 +000045/// Gets the virtual address range between a pair of linker-defined symbols.
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010046#[macro_export]
47macro_rules! linker_region {
48 ($begin:ident,$end:ident) => {{
49 let start = linker_addr!($begin);
50 let end = linker_addr!($end);
51
Alice Wang8b097042023-07-06 14:56:58 +000052 start..end
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010053 }};
54}
55
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010056/// Memory reserved for the DTB.
Alice Wanga3931aa2023-07-05 12:52:09 +000057pub fn dtb_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010058 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010059}
60
61/// Executable code.
Alice Wanga3931aa2023-07-05 12:52:09 +000062pub fn text_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010063 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010064}
65
66/// Read-only data.
Alice Wanga3931aa2023-07-05 12:52:09 +000067pub fn rodata_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010068 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010069}
70
71/// Initialised writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000072pub fn data_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010073 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010074}
75
Alice Wangeff58392023-07-04 13:32:09 +000076/// Zero-initialized writable data.
Alice Wanga3931aa2023-07-05 12:52:09 +000077pub fn bss_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010078 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010079}
80
81/// Writable data region for the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000082pub fn stack_range(stack_size: usize) -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010083 let end = linker_addr!(init_stack_pointer);
Alice Wang8b097042023-07-06 14:56:58 +000084 let start = VirtualAddress(end.0.checked_sub(stack_size).unwrap());
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010085 assert!(start >= linker_addr!(stack_limit));
86
Alice Wang8b097042023-07-06 14:56:58 +000087 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010088}
89
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010090/// All writable sections, excluding the stack.
Alice Wanga3931aa2023-07-05 12:52:09 +000091pub fn scratch_range() -> Range<VirtualAddress> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010092 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010093}
94
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010095/// Range of the page at UART_PAGE_ADDR of PAGE_SIZE.
96pub fn console_uart_page() -> Range<VirtualAddress> {
97 VirtualAddress(UART_PAGE_ADDR)..VirtualAddress(UART_PAGE_ADDR + PAGE_SIZE)
Alice Wang807fa592023-06-02 09:54:43 +000098}
99
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100100/// Read-write data (original).
Alice Wang8b097042023-07-06 14:56:58 +0000101pub fn data_load_address() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100102 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100103}
104
105/// End of the binary image.
Alice Wang8b097042023-07-06 14:56:58 +0000106pub fn binary_end() -> VirtualAddress {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +0100107 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +0100108}
Andrew Walbranb88eb012023-07-05 13:58:26 +0000109
110/// Value of __stack_chk_guard.
111pub fn stack_chk_guard() -> u64 {
112 // SAFETY: __stack_chk_guard shouldn't have any mutable aliases unless the stack overflows. If
113 // it does, then there could be undefined behaviour all over the program, but we want to at
114 // least have a chance at catching it.
115 unsafe { addr_of!(__stack_chk_guard).read_volatile() }
116}