blob: b032a3041197051c690396063ce8e8bae40f9574 [file] [log] [blame]
Andrew Walbran153aad92022-06-28 15:51:13 +00001// 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
17use aarch64_paging::paging::{MemoryRegion, VirtualAddress};
David Brazdila51c6f02022-10-12 09:51:48 +000018use core::arch::asm;
Andrew Walbran153aad92022-06-28 15:51:13 +000019use core::ops::Range;
Jakob Vukalovicef996292023-04-13 14:28:34 +000020use log::info;
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010021use vmbase::layout;
Andrew Walbran153aad92022-06-28 15:51:13 +000022
23/// The first 1 GiB of memory are used for MMIO.
24pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000);
25
Andrew Walbran153aad92022-06-28 15:51:13 +000026/// Writable data region for the stack.
27pub fn boot_stack_range() -> Range<VirtualAddress> {
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010028 const PAGE_SIZE: usize = 4 << 10;
Alice Wanga3931aa2023-07-05 12:52:09 +000029 layout::stack_range(40 * PAGE_SIZE)
Andrew Walbran153aad92022-06-28 15:51:13 +000030}
31
Andrew Walbran153aad92022-06-28 15:51:13 +000032pub fn print_addresses() {
Alice Wanga3931aa2023-07-05 12:52:09 +000033 let dtb = layout::dtb_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000034 info!("dtb: {}..{} ({} bytes)", dtb.start, dtb.end, dtb.end - dtb.start);
Alice Wanga3931aa2023-07-05 12:52:09 +000035 let text = layout::text_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000036 info!("text: {}..{} ({} bytes)", text.start, text.end, text.end - text.start);
Alice Wanga3931aa2023-07-05 12:52:09 +000037 let rodata = layout::rodata_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000038 info!("rodata: {}..{} ({} bytes)", rodata.start, rodata.end, rodata.end - rodata.start);
Alice Wang8b097042023-07-06 14:56:58 +000039 info!("binary end: {}", layout::binary_end());
Alice Wanga3931aa2023-07-05 12:52:09 +000040 let data = layout::data_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000041 info!(
Andrew Walbran153aad92022-06-28 15:51:13 +000042 "data: {}..{} ({} bytes, loaded at {})",
43 data.start,
44 data.end,
45 data.end - data.start,
Alice Wang8b097042023-07-06 14:56:58 +000046 layout::data_load_address(),
Andrew Walbran153aad92022-06-28 15:51:13 +000047 );
Alice Wanga3931aa2023-07-05 12:52:09 +000048 let bss = layout::bss_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000049 info!("bss: {}..{} ({} bytes)", bss.start, bss.end, bss.end - bss.start);
Andrew Walbran153aad92022-06-28 15:51:13 +000050 let boot_stack = boot_stack_range();
Jakob Vukalovicef996292023-04-13 14:28:34 +000051 info!(
Andrew Walbran153aad92022-06-28 15:51:13 +000052 "boot_stack: {}..{} ({} bytes)",
53 boot_stack.start,
54 boot_stack.end,
55 boot_stack.end - boot_stack.start
56 );
57}
58
David Brazdila51c6f02022-10-12 09:51:48 +000059/// Bionic-compatible thread-local storage entry, at the given offset from TPIDR_EL0.
60pub fn bionic_tls(off: usize) -> u64 {
61 let mut base: usize;
62 unsafe {
63 asm!("mrs {base}, tpidr_el0", base = out(reg) base);
64 let ptr = (base + off) as *const u64;
65 *ptr
66 }
67}