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; |
| 20 | use vmbase::println; |
| 21 | |
| 22 | /// The first 1 GiB of memory are used for MMIO. |
| 23 | pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000); |
| 24 | |
| 25 | /// Memory reserved for the DTB. |
| 26 | pub fn dtb_range() -> Range<VirtualAddress> { |
| 27 | unsafe { |
| 28 | VirtualAddress(&dtb_begin as *const u8 as usize) |
| 29 | ..VirtualAddress(&dtb_end as *const u8 as usize) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Executable code. |
| 34 | pub fn text_range() -> Range<VirtualAddress> { |
| 35 | unsafe { |
| 36 | VirtualAddress(&text_begin as *const u8 as usize) |
| 37 | ..VirtualAddress(&text_end as *const u8 as usize) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Read-only data. |
| 42 | pub fn rodata_range() -> Range<VirtualAddress> { |
| 43 | unsafe { |
| 44 | VirtualAddress(&rodata_begin as *const u8 as usize) |
| 45 | ..VirtualAddress(&rodata_end as *const u8 as usize) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Initialised writable data. |
| 50 | pub fn data_range() -> Range<VirtualAddress> { |
| 51 | unsafe { |
| 52 | VirtualAddress(&data_begin as *const u8 as usize) |
| 53 | ..VirtualAddress(&data_end as *const u8 as usize) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Zero-initialised writable data. |
| 58 | pub fn bss_range() -> Range<VirtualAddress> { |
| 59 | unsafe { |
| 60 | VirtualAddress(&bss_begin as *const u8 as usize) |
| 61 | ..VirtualAddress(&bss_end as *const u8 as usize) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// Writable data region for the stack. |
| 66 | pub fn boot_stack_range() -> Range<VirtualAddress> { |
| 67 | unsafe { |
| 68 | VirtualAddress(&boot_stack_begin as *const u8 as usize) |
| 69 | ..VirtualAddress(&boot_stack_end as *const u8 as usize) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Writable data, including the stack. |
| 74 | pub fn writable_region() -> MemoryRegion { |
| 75 | unsafe { |
| 76 | MemoryRegion::new(&data_begin as *const u8 as usize, &boot_stack_end as *const u8 as usize) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn data_load_address() -> VirtualAddress { |
| 81 | unsafe { VirtualAddress(&data_lma as *const u8 as usize) } |
| 82 | } |
| 83 | |
Pierre-Clément Tosi | 35e9c1e | 2022-10-14 18:42:38 +0100 | [diff] [blame] | 84 | fn binary_end() -> VirtualAddress { |
| 85 | unsafe { VirtualAddress(&bin_end as *const u8 as usize) } |
| 86 | } |
| 87 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 88 | pub fn print_addresses() { |
| 89 | let dtb = dtb_range(); |
| 90 | println!("dtb: {}..{} ({} bytes)", dtb.start, dtb.end, dtb.end - dtb.start); |
| 91 | let text = text_range(); |
| 92 | println!("text: {}..{} ({} bytes)", text.start, text.end, text.end - text.start); |
| 93 | let rodata = rodata_range(); |
| 94 | println!("rodata: {}..{} ({} bytes)", rodata.start, rodata.end, rodata.end - rodata.start); |
Pierre-Clément Tosi | 35e9c1e | 2022-10-14 18:42:38 +0100 | [diff] [blame] | 95 | println!("binary end: {}", binary_end()); |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 96 | let data = data_range(); |
| 97 | println!( |
| 98 | "data: {}..{} ({} bytes, loaded at {})", |
| 99 | data.start, |
| 100 | data.end, |
| 101 | data.end - data.start, |
| 102 | data_load_address(), |
| 103 | ); |
| 104 | let bss = bss_range(); |
| 105 | println!("bss: {}..{} ({} bytes)", bss.start, bss.end, bss.end - bss.start); |
| 106 | let boot_stack = boot_stack_range(); |
| 107 | println!( |
| 108 | "boot_stack: {}..{} ({} bytes)", |
| 109 | boot_stack.start, |
| 110 | boot_stack.end, |
| 111 | boot_stack.end - boot_stack.start |
| 112 | ); |
| 113 | } |
| 114 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 115 | /// Bionic-compatible thread-local storage entry, at the given offset from TPIDR_EL0. |
| 116 | pub fn bionic_tls(off: usize) -> u64 { |
| 117 | let mut base: usize; |
| 118 | unsafe { |
| 119 | asm!("mrs {base}, tpidr_el0", base = out(reg) base); |
| 120 | let ptr = (base + off) as *const u64; |
| 121 | *ptr |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// Value of __stack_chk_guard. |
| 126 | pub fn stack_chk_guard() -> u64 { |
| 127 | unsafe { __stack_chk_guard } |
| 128 | } |
| 129 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 130 | extern "C" { |
| 131 | static dtb_begin: u8; |
| 132 | static dtb_end: u8; |
| 133 | static text_begin: u8; |
| 134 | static text_end: u8; |
| 135 | static rodata_begin: u8; |
| 136 | static rodata_end: u8; |
| 137 | static data_begin: u8; |
| 138 | static data_end: u8; |
| 139 | static data_lma: u8; |
Pierre-Clément Tosi | 35e9c1e | 2022-10-14 18:42:38 +0100 | [diff] [blame] | 140 | static bin_end: u8; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 141 | static bss_begin: u8; |
| 142 | static bss_end: u8; |
| 143 | static boot_stack_begin: u8; |
| 144 | static boot_stack_end: u8; |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 145 | static __stack_chk_guard: u64; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 146 | } |