blob: 463738e897109fdab286688693b2ca8c06e2172f [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;
20use vmbase::println;
21
22/// The first 1 GiB of memory are used for MMIO.
23pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000);
24
25/// Memory reserved for the DTB.
26pub 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.
34pub 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.
42pub 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.
50pub 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.
58pub 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.
66pub 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.
74pub 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
80fn data_load_address() -> VirtualAddress {
81 unsafe { VirtualAddress(&data_lma as *const u8 as usize) }
82}
83
84pub fn print_addresses() {
85 let dtb = dtb_range();
86 println!("dtb: {}..{} ({} bytes)", dtb.start, dtb.end, dtb.end - dtb.start);
87 let text = text_range();
88 println!("text: {}..{} ({} bytes)", text.start, text.end, text.end - text.start);
89 let rodata = rodata_range();
90 println!("rodata: {}..{} ({} bytes)", rodata.start, rodata.end, rodata.end - rodata.start);
91 let data = data_range();
92 println!(
93 "data: {}..{} ({} bytes, loaded at {})",
94 data.start,
95 data.end,
96 data.end - data.start,
97 data_load_address(),
98 );
99 let bss = bss_range();
100 println!("bss: {}..{} ({} bytes)", bss.start, bss.end, bss.end - bss.start);
101 let boot_stack = boot_stack_range();
102 println!(
103 "boot_stack: {}..{} ({} bytes)",
104 boot_stack.start,
105 boot_stack.end,
106 boot_stack.end - boot_stack.start
107 );
108}
109
David Brazdila51c6f02022-10-12 09:51:48 +0000110/// Bionic-compatible thread-local storage entry, at the given offset from TPIDR_EL0.
111pub fn bionic_tls(off: usize) -> u64 {
112 let mut base: usize;
113 unsafe {
114 asm!("mrs {base}, tpidr_el0", base = out(reg) base);
115 let ptr = (base + off) as *const u64;
116 *ptr
117 }
118}
119
120/// Value of __stack_chk_guard.
121pub fn stack_chk_guard() -> u64 {
122 unsafe { __stack_chk_guard }
123}
124
Andrew Walbran153aad92022-06-28 15:51:13 +0000125extern "C" {
126 static dtb_begin: u8;
127 static dtb_end: u8;
128 static text_begin: u8;
129 static text_end: u8;
130 static rodata_begin: u8;
131 static rodata_end: u8;
132 static data_begin: u8;
133 static data_end: u8;
134 static data_lma: u8;
135 static bss_begin: u8;
136 static bss_end: u8;
137 static boot_stack_begin: u8;
138 static boot_stack_end: u8;
David Brazdila51c6f02022-10-12 09:51:48 +0000139 static __stack_chk_guard: u64;
Andrew Walbran153aad92022-06-28 15:51:13 +0000140}