Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 1 | // 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 | |
| 17 | use aarch64_paging::paging::{MemoryRegion, VirtualAddress}; |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 18 | use core::arch::asm; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 19 | use core::ops::Range; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 20 | use log::info; |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 21 | use vmbase::layout; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 22 | |
| 23 | /// The first 1 GiB of memory are used for MMIO. |
| 24 | pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000); |
| 25 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 26 | /// Writable data region for the stack. |
| 27 | pub fn boot_stack_range() -> Range<VirtualAddress> { |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 28 | const PAGE_SIZE: usize = 4 << 10; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 29 | layout::stack_range(40 * PAGE_SIZE) |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | fn data_load_address() -> VirtualAddress { |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 33 | VirtualAddress(layout::data_load_address()) |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Pierre-Clément Tosi | 35e9c1e | 2022-10-14 18:42:38 +0100 | [diff] [blame] | 36 | fn binary_end() -> VirtualAddress { |
Pierre-Clément Tosi | 9a658f7 | 2022-10-10 15:18:54 +0100 | [diff] [blame] | 37 | VirtualAddress(layout::binary_end()) |
Pierre-Clément Tosi | 35e9c1e | 2022-10-14 18:42:38 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 40 | pub fn print_addresses() { |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 41 | let dtb = layout::dtb_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 42 | info!("dtb: {}..{} ({} bytes)", dtb.start, dtb.end, dtb.end - dtb.start); |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 43 | let text = layout::text_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 44 | info!("text: {}..{} ({} bytes)", text.start, text.end, text.end - text.start); |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 45 | let rodata = layout::rodata_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 46 | info!("rodata: {}..{} ({} bytes)", rodata.start, rodata.end, rodata.end - rodata.start); |
| 47 | info!("binary end: {}", binary_end()); |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 48 | let data = layout::data_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 49 | info!( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 50 | "data: {}..{} ({} bytes, loaded at {})", |
| 51 | data.start, |
| 52 | data.end, |
| 53 | data.end - data.start, |
| 54 | data_load_address(), |
| 55 | ); |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 56 | let bss = layout::bss_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 57 | info!("bss: {}..{} ({} bytes)", bss.start, bss.end, bss.end - bss.start); |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 58 | let boot_stack = boot_stack_range(); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 59 | info!( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 60 | "boot_stack: {}..{} ({} bytes)", |
| 61 | boot_stack.start, |
| 62 | boot_stack.end, |
| 63 | boot_stack.end - boot_stack.start |
| 64 | ); |
| 65 | } |
| 66 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 67 | /// Bionic-compatible thread-local storage entry, at the given offset from TPIDR_EL0. |
| 68 | pub fn bionic_tls(off: usize) -> u64 { |
| 69 | let mut base: usize; |
| 70 | unsafe { |
| 71 | asm!("mrs {base}, tpidr_el0", base = out(reg) base); |
| 72 | let ptr = (base + off) as *const u64; |
| 73 | *ptr |
| 74 | } |
| 75 | } |