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] |
| 19 | |
| 20 | mod exceptions; |
Andrew Walbran | 153aad9 | 2022-06-28 15:51:13 +0000 | [diff] [blame] | 21 | mod layout; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 22 | mod pci; |
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 | |
Pierre-Clément Tosi | eba8316 | 2024-11-02 12:11:48 +0000 | [diff] [blame] | 26 | use crate::layout::print_addresses; |
Pierre-Clément Tosi | d2c5472 | 2024-11-02 12:54:40 +0000 | [diff] [blame] | 27 | use crate::pci::check_pci; |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 28 | use alloc::{vec, vec::Vec}; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 29 | use libfdt::Fdt; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 30 | use log::{debug, error, info, trace, warn, LevelFilter}; |
Andrew Walbran | fb49a41 | 2025-01-15 12:14:24 +0000 | [diff] [blame] | 31 | use spin::mutex::SpinMutex; |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 32 | use vmbase::{ |
Pierre-Clément Tosi | f2c19d4 | 2024-10-01 17:42:04 +0100 | [diff] [blame] | 33 | bionic, configure_heap, |
| 34 | fdt::pci::PciInfo, |
| 35 | generate_image_header, |
Pierre-Clément Tosi | ae07161 | 2024-11-02 13:13:34 +0000 | [diff] [blame] | 36 | layout::crosvm::FDT_MAX_SIZE, |
Pierre-Clément Tosi | 67108c3 | 2023-06-30 11:04:02 +0000 | [diff] [blame] | 37 | linker, logger, main, |
Pierre-Clément Tosi | ae07161 | 2024-11-02 13:13:34 +0000 | [diff] [blame] | 38 | memory::{deactivate_dynamic_page_tables, map_data, SIZE_64KB}, |
Alice Wang | a3931aa | 2023-07-05 12:52:09 +0000 | [diff] [blame] | 39 | }; |
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]; |
Andrew Walbran | fb49a41 | 2025-01-15 12:14:24 +0000 | [diff] [blame] | 42 | static ZEROED_DATA: SpinMutex<[u32; 10]> = SpinMutex::new([0; 10]); |
| 43 | static MUTABLE_DATA: SpinMutex<[u32; 4]> = SpinMutex::new([1, 2, 3, 4]); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 44 | |
Pierre-Clément Tosi | 1f7c523 | 2024-08-13 19:51:25 +0100 | [diff] [blame] | 45 | generate_image_header!(); |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 46 | main!(main); |
Pierre-Clément Tosi | 6a4808c | 2023-06-29 09:19:38 +0000 | [diff] [blame] | 47 | configure_heap!(SIZE_64KB); |
Andrew Walbran | eef9820 | 2022-04-27 16:23:06 +0000 | [diff] [blame] | 48 | |
| 49 | /// Entry point for VM bootloader. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 50 | pub fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 51 | log::set_max_level(LevelFilter::Debug); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 52 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 53 | info!("Hello world"); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 54 | 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] | 55 | print_addresses(); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 56 | check_data(); |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 57 | check_stack_guard(); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 58 | |
| 59 | info!("Checking FDT..."); |
Pierre-Clément Tosi | 9d9cf18 | 2024-08-07 16:28:53 +0100 | [diff] [blame] | 60 | let fdt_addr = usize::try_from(arg0).unwrap(); |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 61 | // SAFETY: The DTB range is valid, writable memory, and we don't construct any aliases to it. |
Pierre-Clément Tosi | 9d9cf18 | 2024-08-07 16:28:53 +0100 | [diff] [blame] | 62 | let fdt = unsafe { core::slice::from_raw_parts_mut(fdt_addr as *mut u8, FDT_MAX_SIZE) }; |
Pierre-Clément Tosi | d2c5472 | 2024-11-02 12:54:40 +0000 | [diff] [blame] | 63 | map_data(fdt_addr, FDT_MAX_SIZE.try_into().unwrap()).unwrap(); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 64 | let fdt = Fdt::from_mut_slice(fdt).unwrap(); |
| 65 | info!("FDT passed verification."); |
| 66 | check_fdt(fdt); |
| 67 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 68 | let pci_info = PciInfo::from_fdt(fdt).unwrap(); |
| 69 | debug!("Found PCI CAM at {:#x}-{:#x}", pci_info.cam_range.start, pci_info.cam_range.end); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 70 | |
| 71 | modify_fdt(fdt); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 72 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 73 | check_alloc(); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 74 | check_data(); |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 75 | check_dice(); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 76 | |
Pierre-Clément Tosi | d2c5472 | 2024-11-02 12:54:40 +0000 | [diff] [blame] | 77 | let mut pci_root = vmbase::virtio::pci::initialize(pci_info).unwrap(); |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 78 | check_pci(&mut pci_root); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 79 | |
| 80 | emit_suppressed_log(); |
Pierre-Clément Tosi | 3d1c2e4 | 2024-08-13 22:29:46 +0100 | [diff] [blame] | 81 | |
| 82 | info!("De-activating IdMap..."); |
Pierre-Clément Tosi | d2c5472 | 2024-11-02 12:54:40 +0000 | [diff] [blame] | 83 | deactivate_dynamic_page_tables(); |
Pierre-Clément Tosi | 3d1c2e4 | 2024-08-13 22:29:46 +0100 | [diff] [blame] | 84 | info!("De-activated."); |
Andrew Walbran | 1356454 | 2022-04-20 16:29:45 +0000 | [diff] [blame] | 85 | } |
| 86 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 87 | fn check_stack_guard() { |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 88 | info!("Testing stack guard"); |
Pierre-Clément Tosi | 67108c3 | 2023-06-30 11:04:02 +0000 | [diff] [blame] | 89 | // SAFETY: No concurrency issue should occur when running these tests. |
| 90 | let stack_guard = unsafe { bionic::TLS.stack_guard }; |
| 91 | assert_ne!(stack_guard, 0); |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 92 | // Check that a NULL-terminating value is added for C functions consuming strings from stack. |
| 93 | assert_eq!(stack_guard.to_ne_bytes().last(), Some(&0)); |
Pierre-Clément Tosi | 67108c3 | 2023-06-30 11:04:02 +0000 | [diff] [blame] | 94 | // Check that the TLS and guard are properly accessible from the dedicated register. |
| 95 | assert_eq!(stack_guard, bionic::__get_tls().stack_guard); |
| 96 | // Check that the LLVM __stack_chk_guard alias is also properly set up. |
| 97 | assert_eq!( |
| 98 | stack_guard, |
| 99 | // SAFETY: No concurrency issue should occur when running these tests. |
| 100 | unsafe { linker::__stack_chk_guard }, |
| 101 | ); |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 104 | fn check_data() { |
Pierre-Clément Tosi | 581e9b6 | 2022-10-27 16:21:13 +0100 | [diff] [blame] | 105 | info!("INITIALISED_DATA: {:?}", INITIALISED_DATA.as_ptr()); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 106 | |
| 107 | assert_eq!(INITIALISED_DATA[0], 1); |
| 108 | assert_eq!(INITIALISED_DATA[1], 2); |
| 109 | assert_eq!(INITIALISED_DATA[2], 3); |
| 110 | assert_eq!(INITIALISED_DATA[3], 4); |
| 111 | |
Andrew Walbran | fb49a41 | 2025-01-15 12:14:24 +0000 | [diff] [blame] | 112 | let zeroed_data = &mut *ZEROED_DATA.lock(); |
| 113 | let mutable_data = &mut *MUTABLE_DATA.lock(); |
| 114 | info!("ZEROED_DATA: {:?}", zeroed_data.as_ptr()); |
| 115 | info!("MUTABLE_DATA: {:?}", mutable_data.as_ptr()); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 116 | |
Pierre-Clément Tosi | 70b580c | 2023-07-07 16:26:45 +0000 | [diff] [blame] | 117 | for element in zeroed_data.iter() { |
| 118 | assert_eq!(*element, 0); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 119 | } |
Charisee | e5b7834 | 2024-04-08 17:59:31 +0000 | [diff] [blame] | 120 | |
Pierre-Clément Tosi | 70b580c | 2023-07-07 16:26:45 +0000 | [diff] [blame] | 121 | zeroed_data[0] = 13; |
| 122 | assert_eq!(zeroed_data[0], 13); |
| 123 | zeroed_data[0] = 0; |
| 124 | assert_eq!(zeroed_data[0], 0); |
| 125 | |
| 126 | assert_eq!(mutable_data[0], 1); |
| 127 | assert_eq!(mutable_data[1], 2); |
| 128 | assert_eq!(mutable_data[2], 3); |
| 129 | assert_eq!(mutable_data[3], 4); |
| 130 | mutable_data[0] += 41; |
| 131 | assert_eq!(mutable_data[0], 42); |
| 132 | mutable_data[0] -= 41; |
| 133 | assert_eq!(mutable_data[0], 1); |
| 134 | |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 135 | info!("Data looks good"); |
Andrew Walbran | 5d4e1c9 | 2022-04-12 14:30:54 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 138 | fn check_fdt(reader: &Fdt) { |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 139 | for reg in reader.memory().unwrap() { |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 140 | info!("memory @ {reg:#x?}"); |
| 141 | } |
| 142 | |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 143 | let compatible = c"ns16550a"; |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 144 | |
| 145 | for c in reader.compatible_nodes(compatible).unwrap() { |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 146 | let reg = c.reg().unwrap().unwrap().next().unwrap(); |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 147 | info!("node compatible with '{}' at {reg:?}", compatible.to_str().unwrap()); |
| 148 | } |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 149 | } |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 150 | |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 151 | fn modify_fdt(writer: &mut Fdt) { |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 152 | writer.unpack().unwrap(); |
| 153 | info!("FDT successfully unpacked."); |
| 154 | |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 155 | let path = c"/memory"; |
Pierre-Clément Tosi | a3c4ec3 | 2024-02-15 20:05:15 +0000 | [diff] [blame] | 156 | let node = writer.node_mut(path).unwrap().unwrap(); |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 157 | let name = c"child"; |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 158 | let mut child = node.add_subnode(name).unwrap(); |
| 159 | info!("Created subnode '{}/{}'.", path.to_str().unwrap(), name.to_str().unwrap()); |
| 160 | |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 161 | let name = c"str-property"; |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 162 | child.appendprop(name, b"property-value\0").unwrap(); |
| 163 | info!("Appended property '{}'.", name.to_str().unwrap()); |
| 164 | |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 165 | let name = c"pair-property"; |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 166 | let addr = 0x0123_4567u64; |
| 167 | let size = 0x89ab_cdefu64; |
| 168 | child.appendprop_addrrange(name, addr, size).unwrap(); |
| 169 | info!("Appended property '{}'.", name.to_str().unwrap()); |
| 170 | |
| 171 | let writer = child.fdt(); |
| 172 | writer.pack().unwrap(); |
| 173 | info!("FDT successfully packed."); |
| 174 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 175 | info!("FDT checks done."); |
| 176 | } |
| 177 | |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 178 | fn check_alloc() { |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 179 | info!("Allocating a Vec..."); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 180 | let mut vector: Vec<u32> = vec![1, 2, 3, 4]; |
| 181 | assert_eq!(vector[0], 1); |
| 182 | assert_eq!(vector[1], 2); |
| 183 | assert_eq!(vector[2], 3); |
| 184 | assert_eq!(vector[3], 4); |
| 185 | vector[2] = 42; |
| 186 | assert_eq!(vector[2], 42); |
David Brazdil | b41aa8f | 2022-07-05 12:41:00 +0100 | [diff] [blame] | 187 | info!("Vec seems to work."); |
Andrew Walbran | f7b6dc8 | 2022-04-20 16:24:30 +0000 | [diff] [blame] | 188 | } |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 189 | |
| 190 | fn check_dice() { |
| 191 | info!("Testing DICE integration..."); |
Alice Wang | cb9d2f9 | 2023-02-06 10:29:00 +0000 | [diff] [blame] | 192 | let hash = diced_open_dice::hash("hello world".as_bytes()).expect("DiceHash failed"); |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 193 | assert_eq!( |
| 194 | hash, |
| 195 | [ |
| 196 | 0x30, 0x9e, 0xcc, 0x48, 0x9c, 0x12, 0xd6, 0xeb, 0x4c, 0xc4, 0x0f, 0x50, 0xc9, 0x02, |
| 197 | 0xf2, 0xb4, 0xd0, 0xed, 0x77, 0xee, 0x51, 0x1a, 0x7c, 0x7a, 0x9b, 0xcd, 0x3c, 0xa8, |
| 198 | 0x6d, 0x4c, 0xd8, 0x6f, 0x98, 0x9d, 0xd3, 0x5b, 0xc5, 0xff, 0x49, 0x96, 0x70, 0xda, |
| 199 | 0x34, 0x25, 0x5b, 0x45, 0xb0, 0xcf, 0xd8, 0x30, 0xe8, 0x1f, 0x60, 0x5d, 0xcf, 0x7d, |
| 200 | 0xc5, 0x54, 0x2e, 0x93, 0xae, 0x9c, 0xd7, 0x6f |
| 201 | ] |
| 202 | ); |
| 203 | } |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 204 | |
| 205 | macro_rules! log_all_levels { |
| 206 | ($msg:literal) => {{ |
| 207 | error!($msg); |
| 208 | warn!($msg); |
| 209 | info!($msg); |
| 210 | debug!($msg); |
| 211 | trace!($msg); |
| 212 | }}; |
| 213 | } |
| 214 | |
| 215 | fn emit_suppressed_log() { |
| 216 | { |
| 217 | let _guard = logger::suppress(); |
| 218 | log_all_levels!("Suppressed message"); |
| 219 | } |
| 220 | log_all_levels!("Unsuppressed message"); |
| 221 | } |