David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [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 | //! Project Rialto main source file. |
| 16 | |
| 17 | #![no_main] |
| 18 | #![no_std] |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 19 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 20 | mod error; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 21 | mod exceptions; |
| 22 | |
| 23 | extern crate alloc; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 24 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 25 | use crate::error::{Error, Result}; |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 26 | use core::num::NonZeroUsize; |
Alice Wang | b6d2c64 | 2023-06-13 13:07:06 +0000 | [diff] [blame] | 27 | use core::result; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 28 | use core::slice; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 29 | use fdtpci::PciInfo; |
Alice Wang | b6d2c64 | 2023-06-13 13:07:06 +0000 | [diff] [blame] | 30 | use hyp::{get_hypervisor, HypervisorCap, KvmError}; |
| 31 | use libfdt::FdtError; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 32 | use log::{debug, error, info}; |
Alice Wang | 4b3cc11 | 2023-06-06 12:22:53 +0000 | [diff] [blame] | 33 | use vmbase::{ |
Pierre-Clément Tosi | 6a4808c | 2023-06-29 09:19:38 +0000 | [diff] [blame^] | 34 | configure_heap, |
Alice Wang | b6d2c64 | 2023-06-13 13:07:06 +0000 | [diff] [blame] | 35 | fdt::SwiotlbInfo, |
Alice Wang | 89d2959 | 2023-06-12 09:41:29 +0000 | [diff] [blame] | 36 | layout::{self, crosvm}, |
| 37 | main, |
Pierre-Clément Tosi | c332fae | 2023-06-22 11:37:12 +0000 | [diff] [blame] | 38 | memory::{MemoryTracker, PageTable, MEMORY, PAGE_SIZE, SIZE_64KB}, |
Alice Wang | 4b3cc11 | 2023-06-06 12:22:53 +0000 | [diff] [blame] | 39 | power::reboot, |
| 40 | }; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 41 | |
Alice Wang | b70bdb5 | 2023-06-12 08:17:58 +0000 | [diff] [blame] | 42 | fn new_page_table() -> Result<PageTable> { |
Alice Wang | ee5b180 | 2023-06-07 07:41:54 +0000 | [diff] [blame] | 43 | let mut page_table = PageTable::default(); |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 44 | |
Alice Wang | 89d2959 | 2023-06-12 09:41:29 +0000 | [diff] [blame] | 45 | page_table.map_device(&crosvm::MMIO_RANGE)?; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 46 | page_table.map_data(&layout::scratch_range())?; |
Alice Wang | 4b3cc11 | 2023-06-06 12:22:53 +0000 | [diff] [blame] | 47 | page_table.map_data(&layout::stack_range(40 * PAGE_SIZE))?; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame] | 48 | page_table.map_code(&layout::text_range())?; |
| 49 | page_table.map_rodata(&layout::rodata_range())?; |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 50 | page_table.map_device(&layout::console_uart_range())?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 51 | |
Alice Wang | b70bdb5 | 2023-06-12 08:17:58 +0000 | [diff] [blame] | 52 | Ok(page_table) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 55 | fn try_init_logger() -> Result<bool> { |
| 56 | let mmio_guard_supported = match get_hypervisor().mmio_guard_init() { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 57 | // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging. |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 58 | Ok(()) => { |
| 59 | get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?; |
| 60 | true |
| 61 | } |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 62 | // MMIO guard enroll is not supported in unprotected VM. |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 63 | Err(hyp::Error::MmioGuardNotsupported) => false, |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 64 | Err(e) => return Err(e.into()), |
| 65 | }; |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 66 | vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit)?; |
| 67 | Ok(mmio_guard_supported) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 68 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 69 | |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 70 | /// # Safety |
| 71 | /// |
| 72 | /// Behavior is undefined if any of the following conditions are violated: |
| 73 | /// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`. |
| 74 | unsafe fn try_main(fdt_addr: usize) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 75 | info!("Welcome to Rialto!"); |
Alice Wang | b70bdb5 | 2023-06-12 08:17:58 +0000 | [diff] [blame] | 76 | let page_table = new_page_table()?; |
| 77 | |
| 78 | MEMORY.lock().replace(MemoryTracker::new( |
| 79 | page_table, |
| 80 | crosvm::MEM_START..layout::MAX_VIRT_ADDR, |
| 81 | crosvm::MMIO_RANGE, |
| 82 | None, // Rialto doesn't have any payload for now. |
| 83 | )); |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 84 | |
| 85 | let fdt_range = MEMORY |
| 86 | .lock() |
| 87 | .as_mut() |
| 88 | .unwrap() |
| 89 | .alloc(fdt_addr, NonZeroUsize::new(crosvm::FDT_MAX_SIZE).unwrap())?; |
| 90 | // SAFETY: The tracker validated the range to be in main memory, mapped, and not overlap. |
| 91 | let fdt = unsafe { slice::from_raw_parts(fdt_range.start as *mut u8, fdt_range.len()) }; |
Alice Wang | 674257a | 2023-06-13 09:44:53 +0000 | [diff] [blame] | 92 | // We do not need to validate the DT since it is already validated in pvmfw. |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 93 | let fdt = libfdt::Fdt::from_slice(fdt)?; |
| 94 | let pci_info = PciInfo::from_fdt(fdt)?; |
| 95 | debug!("PCI: {pci_info:#x?}"); |
| 96 | |
Alice Wang | 674257a | 2023-06-13 09:44:53 +0000 | [diff] [blame] | 97 | let memory_range = fdt.first_memory_range()?; |
| 98 | MEMORY.lock().as_mut().unwrap().shrink(&memory_range).map_err(|e| { |
| 99 | error!("Failed to use memory range value from DT: {memory_range:#x?}"); |
| 100 | e |
| 101 | })?; |
Alice Wang | b6d2c64 | 2023-06-13 13:07:06 +0000 | [diff] [blame] | 102 | |
| 103 | if get_hypervisor().has_cap(HypervisorCap::DYNAMIC_MEM_SHARE) { |
| 104 | let granule = memory_protection_granule()?; |
| 105 | MEMORY.lock().as_mut().unwrap().init_dynamic_shared_pool(granule).map_err(|e| { |
| 106 | error!("Failed to initialize dynamically shared pool."); |
| 107 | e |
| 108 | })?; |
| 109 | } else { |
| 110 | let range = SwiotlbInfo::new_from_fdt(fdt)?.fixed_range().ok_or_else(|| { |
| 111 | error!("Pre-shared pool range not specified in swiotlb node"); |
| 112 | Error::from(FdtError::BadValue) |
| 113 | })?; |
| 114 | MEMORY.lock().as_mut().unwrap().init_static_shared_pool(range).map_err(|e| { |
| 115 | error!("Failed to initialize pre-shared pool."); |
| 116 | e |
| 117 | })?; |
| 118 | } |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 119 | Ok(()) |
| 120 | } |
| 121 | |
Alice Wang | b6d2c64 | 2023-06-13 13:07:06 +0000 | [diff] [blame] | 122 | fn memory_protection_granule() -> result::Result<usize, hyp::Error> { |
| 123 | match get_hypervisor().memory_protection_granule() { |
| 124 | Ok(granule) => Ok(granule), |
| 125 | // Take the default page size when KVM call is not supported in non-protected VMs. |
| 126 | Err(hyp::Error::KvmError(KvmError::NotSupported, _)) => Ok(PAGE_SIZE), |
| 127 | Err(e) => Err(e), |
| 128 | } |
| 129 | } |
| 130 | |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 131 | fn try_unshare_all_memory(mmio_guard_supported: bool) -> Result<()> { |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 132 | info!("Starting unsharing memory..."); |
| 133 | |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 134 | // No logging after unmapping UART. |
Alice Wang | b70bdb5 | 2023-06-12 08:17:58 +0000 | [diff] [blame] | 135 | if mmio_guard_supported { |
| 136 | get_hypervisor().mmio_guard_unmap(vmbase::console::BASE_ADDRESS)?; |
| 137 | } |
| 138 | // Unshares all memory and deactivates page table. |
| 139 | drop(MEMORY.lock().take()); |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 140 | Ok(()) |
| 141 | } |
| 142 | |
| 143 | fn unshare_all_memory(mmio_guard_supported: bool) { |
| 144 | if let Err(e) = try_unshare_all_memory(mmio_guard_supported) { |
| 145 | error!("Failed to unshare the memory: {e}"); |
| 146 | } |
| 147 | } |
| 148 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 149 | /// Entry point for Rialto. |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 150 | pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) { |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 151 | let Ok(mmio_guard_supported) = try_init_logger() else { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 152 | // Don't log anything if the logger initialization fails. |
| 153 | reboot(); |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 154 | }; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 155 | // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to |
| 156 | // a valid `Fdt`. |
| 157 | match unsafe { try_main(fdt_addr as usize) } { |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 158 | Ok(()) => unshare_all_memory(mmio_guard_supported), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 159 | Err(e) => { |
| 160 | error!("Rialto failed with {e}"); |
Alice Wang | 77d9dd3 | 2023-06-07 13:41:21 +0000 | [diff] [blame] | 161 | unshare_all_memory(mmio_guard_supported); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 162 | reboot() |
| 163 | } |
| 164 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 165 | } |
| 166 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 167 | main!(main); |
Pierre-Clément Tosi | 6a4808c | 2023-06-29 09:19:38 +0000 | [diff] [blame^] | 168 | configure_heap!(SIZE_64KB); |