Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +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 | //! VM bootloader example. |
| 16 | |
| 17 | #![no_main] |
| 18 | #![no_std] |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 19 | #![feature(default_alloc_error_handler)] |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 20 | |
| 21 | mod exceptions; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 22 | mod layout; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 23 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 24 | extern crate alloc; |
| 25 | |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 26 | use crate::layout::{ |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 27 | bionic_tls, dtb_range, print_addresses, rodata_range, stack_chk_guard, text_range, |
| 28 | writable_region, DEVICE_REGION, |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 29 | }; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 30 | use aarch64_paging::{ |
| 31 | idmap::IdMap, |
| 32 | paging::{Attributes, MemoryRegion}, |
| 33 | }; |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 34 | use alloc::{vec, vec::Vec}; |
| 35 | use buddy_system_allocator::LockedHeap; |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 36 | use core::ffi::CStr; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 37 | use libfdt::Fdt; |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 38 | use log::{info, LevelFilter}; |
| 39 | use vmbase::{logger, main, println}; |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 40 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 41 | static INITIALISED_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 42 | static mut ZEROED_DATA: [u32; 10] = [0; 10]; |
| 43 | static mut MUTABLE_DATA: [u32; 4] = [1, 2, 3, 4]; |
| 44 | |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 45 | const ASID: usize = 1; |
| 46 | const ROOT_LEVEL: usize = 1; |
| 47 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 48 | #[global_allocator] |
| 49 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 50 | |
| 51 | static mut HEAP: [u8; 65536] = [0; 65536]; |
| 52 | |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 53 | main!(main); |
| 54 | |
| 55 | /// Entry point for VM bootloader. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 56 | pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 57 | logger::init(LevelFilter::Debug).unwrap(); |
| 58 | |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 59 | println!("Hello world"); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 60 | info!("x0={:#018x}, x1={:#018x}, x2={:#018x}, x3={:#018x}", arg0, arg1, arg2, arg3); |
Andrew Walbran | 6261cf4 | 2022-04-12 13:26:52 +0000 | [diff] [blame] | 61 | print_addresses(); |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 62 | assert_eq!(arg0, dtb_range().start.0 as u64); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 63 | check_data(); |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 64 | check_stack_guard(); |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 65 | check_fdt(); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 66 | |
| 67 | unsafe { |
Pierre-Clément Tosi | 581e9b6 | 2022-10-27 16:21:13 +0100 | [diff] [blame] | 68 | HEAP_ALLOCATOR.lock().init(HEAP.as_mut_ptr() as usize, HEAP.len()); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | check_alloc(); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 72 | |
| 73 | let mut idmap = IdMap::new(ASID, ROOT_LEVEL); |
| 74 | idmap.map_range(&DEVICE_REGION, Attributes::DEVICE_NGNRE | Attributes::EXECUTE_NEVER).unwrap(); |
| 75 | idmap |
| 76 | .map_range( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 77 | &text_range().into(), |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 78 | Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::READ_ONLY, |
| 79 | ) |
| 80 | .unwrap(); |
| 81 | idmap |
| 82 | .map_range( |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 83 | &rodata_range().into(), |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 84 | Attributes::NORMAL |
| 85 | | Attributes::NON_GLOBAL |
| 86 | | Attributes::READ_ONLY |
| 87 | | Attributes::EXECUTE_NEVER, |
| 88 | ) |
| 89 | .unwrap(); |
| 90 | idmap |
| 91 | .map_range( |
| 92 | &writable_region(), |
| 93 | Attributes::NORMAL | Attributes::NON_GLOBAL | Attributes::EXECUTE_NEVER, |
| 94 | ) |
| 95 | .unwrap(); |
| 96 | |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 97 | info!("Activating IdMap..."); |
| 98 | info!("{:?}", idmap); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 99 | idmap.activate(); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 100 | info!("Activated."); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 101 | |
| 102 | check_data(); |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 103 | check_dice(); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 106 | fn check_stack_guard() { |
| 107 | const BIONIC_TLS_STACK_GRD_OFF: usize = 40; |
| 108 | |
| 109 | info!("Testing stack guard"); |
| 110 | assert_eq!(bionic_tls(BIONIC_TLS_STACK_GRD_OFF), stack_chk_guard()); |
| 111 | } |
| 112 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 113 | fn check_data() { |
Pierre-Clément Tosi | 581e9b6 | 2022-10-27 16:21:13 +0100 | [diff] [blame] | 114 | info!("INITIALISED_DATA: {:?}", INITIALISED_DATA.as_ptr()); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 115 | unsafe { |
Pierre-Clément Tosi | 581e9b6 | 2022-10-27 16:21:13 +0100 | [diff] [blame] | 116 | info!("ZEROED_DATA: {:?}", ZEROED_DATA.as_ptr()); |
| 117 | info!("MUTABLE_DATA: {:?}", MUTABLE_DATA.as_ptr()); |
| 118 | info!("HEAP: {:?}", HEAP.as_ptr()); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | assert_eq!(INITIALISED_DATA[0], 1); |
| 122 | assert_eq!(INITIALISED_DATA[1], 2); |
| 123 | assert_eq!(INITIALISED_DATA[2], 3); |
| 124 | assert_eq!(INITIALISED_DATA[3], 4); |
| 125 | |
| 126 | unsafe { |
| 127 | for element in ZEROED_DATA.iter() { |
| 128 | assert_eq!(*element, 0); |
| 129 | } |
| 130 | ZEROED_DATA[0] = 13; |
| 131 | assert_eq!(ZEROED_DATA[0], 13); |
| 132 | ZEROED_DATA[0] = 0; |
| 133 | assert_eq!(ZEROED_DATA[0], 0); |
| 134 | |
| 135 | assert_eq!(MUTABLE_DATA[0], 1); |
| 136 | assert_eq!(MUTABLE_DATA[1], 2); |
| 137 | assert_eq!(MUTABLE_DATA[2], 3); |
| 138 | assert_eq!(MUTABLE_DATA[3], 4); |
| 139 | MUTABLE_DATA[0] += 41; |
| 140 | assert_eq!(MUTABLE_DATA[0], 42); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 141 | MUTABLE_DATA[0] -= 41; |
| 142 | assert_eq!(MUTABLE_DATA[0], 1); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 143 | } |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 144 | info!("Data looks good"); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 147 | fn check_fdt() { |
| 148 | info!("Checking FDT..."); |
| 149 | let fdt = MemoryRegion::from(layout::dtb_range()); |
| 150 | let fdt = unsafe { core::slice::from_raw_parts_mut(fdt.start().0 as *mut u8, fdt.len()) }; |
| 151 | |
| 152 | let reader = Fdt::from_slice(fdt).unwrap(); |
| 153 | info!("FDT passed verification."); |
| 154 | for reg in reader.memory().unwrap() { |
| 155 | info!("memory @ {reg:#x?}"); |
| 156 | } |
| 157 | |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 158 | let compatible = CStr::from_bytes_with_nul(b"ns16550a\0").unwrap(); |
| 159 | |
| 160 | for c in reader.compatible_nodes(compatible).unwrap() { |
| 161 | let reg = c.reg().unwrap().next().unwrap(); |
| 162 | info!("node compatible with '{}' at {reg:?}", compatible.to_str().unwrap()); |
| 163 | } |
| 164 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame^] | 165 | let writer = Fdt::from_mut_slice(fdt).unwrap(); |
| 166 | writer.unpack().unwrap(); |
| 167 | info!("FDT successfully unpacked."); |
| 168 | |
| 169 | let path = CStr::from_bytes_with_nul(b"/memory\0").unwrap(); |
| 170 | let mut node = writer.node_mut(path).unwrap(); |
| 171 | let name = CStr::from_bytes_with_nul(b"child\0").unwrap(); |
| 172 | let mut child = node.add_subnode(name).unwrap(); |
| 173 | info!("Created subnode '{}/{}'.", path.to_str().unwrap(), name.to_str().unwrap()); |
| 174 | |
| 175 | let name = CStr::from_bytes_with_nul(b"str-property\0").unwrap(); |
| 176 | child.appendprop(name, b"property-value\0").unwrap(); |
| 177 | info!("Appended property '{}'.", name.to_str().unwrap()); |
| 178 | |
| 179 | let name = CStr::from_bytes_with_nul(b"pair-property\0").unwrap(); |
| 180 | let addr = 0x0123_4567u64; |
| 181 | let size = 0x89ab_cdefu64; |
| 182 | child.appendprop_addrrange(name, addr, size).unwrap(); |
| 183 | info!("Appended property '{}'.", name.to_str().unwrap()); |
| 184 | |
| 185 | let writer = child.fdt(); |
| 186 | writer.pack().unwrap(); |
| 187 | info!("FDT successfully packed."); |
| 188 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 189 | info!("FDT checks done."); |
| 190 | } |
| 191 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 192 | fn check_alloc() { |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 193 | info!("Allocating a Vec..."); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 194 | let mut vector: Vec<u32> = vec![1, 2, 3, 4]; |
| 195 | assert_eq!(vector[0], 1); |
| 196 | assert_eq!(vector[1], 2); |
| 197 | assert_eq!(vector[2], 3); |
| 198 | assert_eq!(vector[3], 4); |
| 199 | vector[2] = 42; |
| 200 | assert_eq!(vector[2], 42); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 201 | info!("Vec seems to work."); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 202 | } |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 203 | |
| 204 | fn check_dice() { |
| 205 | info!("Testing DICE integration..."); |
| 206 | let hash = dice::hash("hello world".as_bytes()).expect("DiceHash failed"); |
| 207 | assert_eq!( |
| 208 | hash, |
| 209 | [ |
| 210 | 0x30, 0x9e, 0xcc, 0x48, 0x9c, 0x12, 0xd6, 0xeb, 0x4c, 0xc4, 0x0f, 0x50, 0xc9, 0x02, |
| 211 | 0xf2, 0xb4, 0xd0, 0xed, 0x77, 0xee, 0x51, 0x1a, 0x7c, 0x7a, 0x9b, 0xcd, 0x3c, 0xa8, |
| 212 | 0x6d, 0x4c, 0xd8, 0x6f, 0x98, 0x9d, 0xd3, 0x5b, 0xc5, 0xff, 0x49, 0x96, 0x70, 0xda, |
| 213 | 0x34, 0x25, 0x5b, 0x45, 0xb0, 0xcf, 0xd8, 0x30, 0xe8, 0x1f, 0x60, 0x5d, 0xcf, 0x7d, |
| 214 | 0xc5, 0x54, 0x2e, 0x93, 0xae, 0x9c, 0xd7, 0x6f |
| 215 | ] |
| 216 | ); |
| 217 | } |