blob: 9cf1a698f62c3fb73a9dd608c073897d1c59d732 [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};
18use core::ops::Range;
19use vmbase::println;
20
21/// The first 1 GiB of memory are used for MMIO.
22pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000);
23
24/// Memory reserved for the DTB.
25pub fn dtb_range() -> Range<VirtualAddress> {
26 unsafe {
27 VirtualAddress(&dtb_begin as *const u8 as usize)
28 ..VirtualAddress(&dtb_end as *const u8 as usize)
29 }
30}
31
32/// Executable code.
33pub fn text_range() -> Range<VirtualAddress> {
34 unsafe {
35 VirtualAddress(&text_begin as *const u8 as usize)
36 ..VirtualAddress(&text_end as *const u8 as usize)
37 }
38}
39
40/// Read-only data.
41pub fn rodata_range() -> Range<VirtualAddress> {
42 unsafe {
43 VirtualAddress(&rodata_begin as *const u8 as usize)
44 ..VirtualAddress(&rodata_end as *const u8 as usize)
45 }
46}
47
48/// Initialised writable data.
49pub fn data_range() -> Range<VirtualAddress> {
50 unsafe {
51 VirtualAddress(&data_begin as *const u8 as usize)
52 ..VirtualAddress(&data_end as *const u8 as usize)
53 }
54}
55
56/// Zero-initialised writable data.
57pub fn bss_range() -> Range<VirtualAddress> {
58 unsafe {
59 VirtualAddress(&bss_begin as *const u8 as usize)
60 ..VirtualAddress(&bss_end as *const u8 as usize)
61 }
62}
63
64/// Writable data region for the stack.
65pub fn boot_stack_range() -> Range<VirtualAddress> {
66 unsafe {
67 VirtualAddress(&boot_stack_begin as *const u8 as usize)
68 ..VirtualAddress(&boot_stack_end as *const u8 as usize)
69 }
70}
71
72/// Writable data, including the stack.
73pub fn writable_region() -> MemoryRegion {
74 unsafe {
75 MemoryRegion::new(&data_begin as *const u8 as usize, &boot_stack_end as *const u8 as usize)
76 }
77}
78
79fn data_load_address() -> VirtualAddress {
80 unsafe { VirtualAddress(&data_lma as *const u8 as usize) }
81}
82
83pub fn print_addresses() {
84 let dtb = dtb_range();
85 println!("dtb: {}..{} ({} bytes)", dtb.start, dtb.end, dtb.end - dtb.start);
86 let text = text_range();
87 println!("text: {}..{} ({} bytes)", text.start, text.end, text.end - text.start);
88 let rodata = rodata_range();
89 println!("rodata: {}..{} ({} bytes)", rodata.start, rodata.end, rodata.end - rodata.start);
90 let data = data_range();
91 println!(
92 "data: {}..{} ({} bytes, loaded at {})",
93 data.start,
94 data.end,
95 data.end - data.start,
96 data_load_address(),
97 );
98 let bss = bss_range();
99 println!("bss: {}..{} ({} bytes)", bss.start, bss.end, bss.end - bss.start);
100 let boot_stack = boot_stack_range();
101 println!(
102 "boot_stack: {}..{} ({} bytes)",
103 boot_stack.start,
104 boot_stack.end,
105 boot_stack.end - boot_stack.start
106 );
107}
108
109extern "C" {
110 static dtb_begin: u8;
111 static dtb_end: u8;
112 static text_begin: u8;
113 static text_end: u8;
114 static rodata_begin: u8;
115 static rodata_end: u8;
116 static data_begin: u8;
117 static data_end: u8;
118 static data_lma: u8;
119 static bss_begin: u8;
120 static bss_end: u8;
121 static boot_stack_begin: u8;
122 static boot_stack_end: u8;
123}