blob: 99aca1df8342c1ca2f130eafaffc9b1cd0a07373 [file] [log] [blame]
Andrew Walbraneef98202022-04-27 16:23:06 +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//! VM bootloader example.
16
17#![no_main]
18#![no_std]
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000019#![feature(default_alloc_error_handler)]
Andrew Walbraneef98202022-04-27 16:23:06 +000020
21mod exceptions;
22
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000023extern crate alloc;
24
Andrew Walbran13564542022-04-20 16:29:45 +000025use aarch64_paging::{
26 idmap::IdMap,
27 paging::{Attributes, MemoryRegion},
28};
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000029use alloc::{vec, vec::Vec};
30use buddy_system_allocator::LockedHeap;
Andrew Walbraneef98202022-04-27 16:23:06 +000031use vmbase::{main, println};
32
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000033static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4];
34static mut ZEROED_DATA: [u32; 10] = [0; 10];
35static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4];
36
Andrew Walbran13564542022-04-20 16:29:45 +000037const ASID: usize = 1;
38const ROOT_LEVEL: usize = 1;
39
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000040#[global_allocator]
41static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
42
43static mut HEAP: [u8; 65536] = [0; 65536];
44
Andrew Walbran13564542022-04-20 16:29:45 +000045/// The first 1 GiB of memory are used for MMIO.
46const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000);
47
Andrew Walbraneef98202022-04-27 16:23:06 +000048main!(main);
49
50/// Entry point for VM bootloader.
Andrew Walbrane03395a2022-04-29 15:15:49 +000051pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbraneef98202022-04-27 16:23:06 +000052 println!("Hello world");
Andrew Walbrane03395a2022-04-29 15:15:49 +000053 println!("x0={:#010x}, x1={:#010x}, x2={:#010x}, x3={:#010x}", arg0, arg1, arg2, arg3);
Andrew Walbran6261cf42022-04-12 13:26:52 +000054 print_addresses();
Andrew Walbran5d4e1c92022-04-12 14:30:54 +000055 check_data();
Andrew Walbranf7b6dc82022-04-20 16:24:30 +000056
57 unsafe {
58 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
59 }
60
61 check_alloc();
Andrew Walbran13564542022-04-20 16:29:45 +000062
63 let mut idmap = IdMap::new(ASID, ROOT_LEVEL);
64 idmap.map_range(&DEVICE_REGION, Attributes::DEVICE_NGNRE | Attributes::EXECUTE_NEVER).unwrap();
65 idmap
66 .map_range(
67 &text_region(),
68 Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY,
69 )
70 .unwrap();
71 idmap
72 .map_range(
73 &rodata_region(),
74 Attributes::NORMAL
75 | Attributes::NON_GLOBAL
76 | Attributes::READ_ONLY
77 | Attributes::EXECUTE_NEVER,
78 )
79 .unwrap();
80 idmap
81 .map_range(
82 &writable_region(),
83 Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::EXECUTE_NEVER,
84 )
85 .unwrap();
86
87 println!("Activating IdMap...");
88 println!("{:?}", idmap);
89 idmap.activate();
90 println!("Activated.");
91
92 check_data();
93}
94
95/// Executable code.
96fn text_region() -> MemoryRegion {
97 unsafe { MemoryRegion::new(&text_begin as *const u8 as usize, &text_end as *const u8 as usize) }
98}
99
100/// Read-only data.
101fn rodata_region() -> MemoryRegion {
102 unsafe {
103 MemoryRegion::new(&rodata_begin as *const u8 as usize, &rodata_end as *const u8 as usize)
104 }
105}
106
107/// Writable data, including the stack.
108fn writable_region() -> MemoryRegion {
109 unsafe {
110 MemoryRegion::new(&data_begin as *const u8 as usize, &boot_stack_end as *const u8 as usize)
111 }
Andrew Walbran6261cf42022-04-12 13:26:52 +0000112}
113
114fn print_addresses() {
115 unsafe {
116 println!(
117 "dtb: {:#010x}-{:#010x} ({} bytes)",
118 &dtb_begin as *const u8 as usize,
119 &dtb_end as *const u8 as usize,
120 &dtb_end as *const u8 as usize - &dtb_begin as *const u8 as usize,
121 );
122 println!(
123 "text: {:#010x}-{:#010x} ({} bytes)",
124 &text_begin as *const u8 as usize,
125 &text_end as *const u8 as usize,
126 &text_end as *const u8 as usize - &text_begin as *const u8 as usize,
127 );
128 println!(
129 "rodata: {:#010x}-{:#010x} ({} bytes)",
130 &rodata_begin as *const u8 as usize,
131 &rodata_end as *const u8 as usize,
132 &rodata_end as *const u8 as usize - &rodata_begin as *const u8 as usize,
133 );
134 println!(
135 "data: {:#010x}-{:#010x} ({} bytes, loaded at {:#010x})",
136 &data_begin as *const u8 as usize,
137 &data_end as *const u8 as usize,
138 &data_end as *const u8 as usize - &data_begin as *const u8 as usize,
139 &data_lma as *const u8 as usize,
140 );
141 println!(
142 "bss: {:#010x}-{:#010x} ({} bytes)",
143 &bss_begin as *const u8 as usize,
144 &bss_end as *const u8 as usize,
145 &bss_end as *const u8 as usize - &bss_begin as *const u8 as usize,
146 );
147 println!(
148 "boot_stack: {:#010x}-{:#010x} ({} bytes)",
149 &boot_stack_begin as *const u8 as usize,
150 &boot_stack_end as *const u8 as usize,
151 &boot_stack_end as *const u8 as usize - &boot_stack_begin as *const u8 as usize,
152 );
153 }
154}
155
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000156fn check_data() {
157 println!("INITIALISED_DATA: {:#010x}", &INITIALISED_DATA as *const u32 as usize);
158 unsafe {
159 println!("ZEROED_DATA: {:#010x}", &ZEROED_DATA as *const u32 as usize);
160 println!("MUTABLE_DATA: {:#010x}", &MUTABLE_DATA as *const u32 as usize);
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000161 println!("HEAP: {:#010x}", &HEAP as *const u8 as usize);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000162 }
163
164 assert_eq!(INITIALISED_DATA[0], 1);
165 assert_eq!(INITIALISED_DATA[1], 2);
166 assert_eq!(INITIALISED_DATA[2], 3);
167 assert_eq!(INITIALISED_DATA[3], 4);
168
169 unsafe {
170 for element in ZEROED_DATA.iter() {
171 assert_eq!(*element, 0);
172 }
173 ZEROED_DATA[0] = 13;
174 assert_eq!(ZEROED_DATA[0], 13);
175 ZEROED_DATA[0] = 0;
176 assert_eq!(ZEROED_DATA[0], 0);
177
178 assert_eq!(MUTABLE_DATA[0], 1);
179 assert_eq!(MUTABLE_DATA[1], 2);
180 assert_eq!(MUTABLE_DATA[2], 3);
181 assert_eq!(MUTABLE_DATA[3], 4);
182 MUTABLE_DATA[0] += 41;
183 assert_eq!(MUTABLE_DATA[0], 42);
Andrew Walbran13564542022-04-20 16:29:45 +0000184 MUTABLE_DATA[0] -= 41;
185 assert_eq!(MUTABLE_DATA[0], 1);
Andrew Walbran5d4e1c92022-04-12 14:30:54 +0000186 }
187 println!("Data looks good");
188}
189
Andrew Walbranf7b6dc82022-04-20 16:24:30 +0000190fn check_alloc() {
191 println!("Allocating a Vec...");
192 let mut vector: Vec<u32> = vec![1, 2, 3, 4];
193 assert_eq!(vector[0], 1);
194 assert_eq!(vector[1], 2);
195 assert_eq!(vector[2], 3);
196 assert_eq!(vector[3], 4);
197 vector[2] = 42;
198 assert_eq!(vector[2], 42);
199 println!("Vec seems to work.");
200}
201
Andrew Walbran6261cf42022-04-12 13:26:52 +0000202extern "C" {
203 static dtb_begin: u8;
204 static dtb_end: u8;
205 static text_begin: u8;
206 static text_end: u8;
207 static rodata_begin: u8;
208 static rodata_end: u8;
209 static data_begin: u8;
210 static data_end: u8;
211 static data_lma: u8;
212 static bss_begin: u8;
213 static bss_end: u8;
214 static boot_stack_begin: u8;
215 static boot_stack_end: u8;
Andrew Walbraneef98202022-04-27 16:23:06 +0000216}