blob: 61e9948e6c50b36ec1a298e86360132890cec826 [file] [log] [blame]
David Brazdil66fc1202022-07-04 21:48:45 +01001// 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 Brazdil66fc1202022-07-04 21:48:45 +010019
Alice Wang4e082c32023-07-11 07:41:50 +000020mod communication;
Alice Wang9a8b39f2023-04-12 15:31:48 +000021mod error;
David Brazdil66fc1202022-07-04 21:48:45 +010022mod exceptions;
Alice Wang474c0ee2023-09-14 12:52:33 +000023mod fdt;
David Brazdil66fc1202022-07-04 21:48:45 +010024
25extern crate alloc;
David Brazdil66fc1202022-07-04 21:48:45 +010026
Alice Wang748b0322023-07-24 12:51:18 +000027use crate::communication::VsockStream;
Alice Wang9a8b39f2023-04-12 15:31:48 +000028use crate::error::{Error, Result};
Alice Wanga2228b92024-07-26 08:38:47 +000029use crate::fdt::{read_dice_range_from, read_is_strict_boot, read_vendor_hashtree_root_digest};
Alice Wang77639bf2023-09-21 06:57:12 +000030use alloc::boxed::Box;
Alice Wang953a6572023-08-24 13:40:10 +000031use ciborium_io::Write;
Alice Wang74f7f4b2023-06-13 08:24:50 +000032use core::num::NonZeroUsize;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000033use core::slice;
Alice Wang77639bf2023-09-21 06:57:12 +000034use diced_open_dice::{bcc_handover_parse, DiceArtifacts};
Per Larsen7ec45d32024-11-02 00:56:46 +000035use hypervisor_backends::get_mem_sharer;
Alice Wangb6d2c642023-06-13 13:07:06 +000036use libfdt::FdtError;
Alice Wang9a8b39f2023-04-12 15:31:48 +000037use log::{debug, error, info};
Alice Wangb5d9a462024-02-09 10:10:47 +000038use service_vm_comm::{ServiceVmRequest, VmType};
Alice Wang4ac9c8b2023-12-05 16:23:14 +000039use service_vm_fake_chain::service_vm;
Alice Wang9eebbab2024-04-10 14:57:27 +000040use service_vm_requests::{process_request, RequestContext};
Alice Wang62183352023-07-04 08:33:27 +000041use virtio_drivers::{
Alice Wangd158e392023-08-30 12:51:12 +000042 device::socket::{VsockAddr, VMADDR_CID_HOST},
Alice Wang62183352023-07-04 08:33:27 +000043 transport::{pci::bus::PciRoot, DeviceType, Transport},
44 Hal,
45};
Alice Wang4b3cc112023-06-06 12:22:53 +000046use vmbase::{
Pierre-Clément Tosi6a4808c2023-06-29 09:19:38 +000047 configure_heap,
Pierre-Clément Tosif2c19d42024-10-01 17:42:04 +010048 fdt::pci::PciInfo,
Alice Wangb6d2c642023-06-13 13:07:06 +000049 fdt::SwiotlbInfo,
Frederick Mayle75842402024-08-05 19:32:08 -070050 generate_image_header,
Pierre-Clément Tosi38b6c782024-11-02 13:47:42 +000051 layout::{self, crosvm},
Alice Wang89d29592023-06-12 09:41:29 +000052 main,
Alice Wang62183352023-07-04 08:33:27 +000053 memory::{MemoryTracker, PageTable, MEMORY, PAGE_SIZE, SIZE_128KB},
Alice Wang4b3cc112023-06-06 12:22:53 +000054 power::reboot,
Alice Wang62183352023-07-04 08:33:27 +000055 virtio::{
56 pci::{self, PciTransportIterator, VirtIOSocket},
57 HalImpl,
58 },
Alice Wang4b3cc112023-06-06 12:22:53 +000059};
David Brazdil66fc1202022-07-04 21:48:45 +010060
Alice Wanga2228b92024-07-26 08:38:47 +000061fn host_addr(fdt: &libfdt::Fdt) -> Result<VsockAddr> {
62 Ok(VsockAddr { cid: VMADDR_CID_HOST, port: vm_type(fdt)?.port() })
Alice Wang4e082c32023-07-11 07:41:50 +000063}
64
Alice Wanga2228b92024-07-26 08:38:47 +000065fn vm_type(fdt: &libfdt::Fdt) -> Result<VmType> {
66 if read_is_strict_boot(fdt)? {
67 Ok(VmType::ProtectedVm)
Alice Wang1d9a5872023-09-06 14:32:36 +000068 } else {
Alice Wanga2228b92024-07-26 08:38:47 +000069 Ok(VmType::NonProtectedVm)
Alice Wang1d9a5872023-09-06 14:32:36 +000070 }
Alice Wang4e082c32023-07-11 07:41:50 +000071}
72
Alice Wangb70bdb52023-06-12 08:17:58 +000073fn new_page_table() -> Result<PageTable> {
Alice Wangee5b1802023-06-07 07:41:54 +000074 let mut page_table = PageTable::default();
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000075
Pierre-Clément Tosi0b02a2b2024-11-28 22:48:27 +000076 page_table.map_data(&layout::data_bss_range().into())?;
77 page_table.map_data(&layout::eh_stack_range().into())?;
Alice Wanga3931aa2023-07-05 12:52:09 +000078 page_table.map_data(&layout::stack_range(40 * PAGE_SIZE).into())?;
79 page_table.map_code(&layout::text_range().into())?;
80 page_table.map_rodata(&layout::rodata_range().into())?;
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010081 page_table.map_device(&layout::console_uart_page().into())?;
David Brazdil66fc1202022-07-04 21:48:45 +010082
Alice Wangb70bdb52023-06-12 08:17:58 +000083 Ok(page_table)
David Brazdil66fc1202022-07-04 21:48:45 +010084}
85
Alice Wangdda3ba92023-05-25 15:15:30 +000086/// # Safety
87///
88/// Behavior is undefined if any of the following conditions are violated:
89/// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`.
90unsafe fn try_main(fdt_addr: usize) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010091 info!("Welcome to Rialto!");
Alice Wangb70bdb52023-06-12 08:17:58 +000092 let page_table = new_page_table()?;
93
94 MEMORY.lock().replace(MemoryTracker::new(
95 page_table,
96 crosvm::MEM_START..layout::MAX_VIRT_ADDR,
97 crosvm::MMIO_RANGE,
98 None, // Rialto doesn't have any payload for now.
99 ));
Alice Wang74f7f4b2023-06-13 08:24:50 +0000100
101 let fdt_range = MEMORY
102 .lock()
103 .as_mut()
104 .unwrap()
105 .alloc(fdt_addr, NonZeroUsize::new(crosvm::FDT_MAX_SIZE).unwrap())?;
106 // SAFETY: The tracker validated the range to be in main memory, mapped, and not overlap.
107 let fdt = unsafe { slice::from_raw_parts(fdt_range.start as *mut u8, fdt_range.len()) };
Alice Wang674257a2023-06-13 09:44:53 +0000108 // We do not need to validate the DT since it is already validated in pvmfw.
Alice Wang74f7f4b2023-06-13 08:24:50 +0000109 let fdt = libfdt::Fdt::from_slice(fdt)?;
Alice Wang74f7f4b2023-06-13 08:24:50 +0000110
Alice Wang674257a2023-06-13 09:44:53 +0000111 let memory_range = fdt.first_memory_range()?;
Chris Wailes0f121752024-09-06 10:45:06 -0700112 MEMORY.lock().as_mut().unwrap().shrink(&memory_range).inspect_err(|_| {
Alice Wang674257a2023-06-13 09:44:53 +0000113 error!("Failed to use memory range value from DT: {memory_range:#x?}");
Alice Wang674257a2023-06-13 09:44:53 +0000114 })?;
Alice Wangb6d2c642023-06-13 13:07:06 +0000115
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000116 if let Some(mem_sharer) = get_mem_sharer() {
117 let granule = mem_sharer.granule()?;
Chris Wailes0f121752024-09-06 10:45:06 -0700118 MEMORY.lock().as_mut().unwrap().init_dynamic_shared_pool(granule).inspect_err(|_| {
Alice Wangb6d2c642023-06-13 13:07:06 +0000119 error!("Failed to initialize dynamically shared pool.");
Alice Wangb6d2c642023-06-13 13:07:06 +0000120 })?;
Pierre-Clément Tosi3c5e7a72024-11-27 20:12:37 +0000121 } else if let Ok(Some(swiotlb_info)) = SwiotlbInfo::new_from_fdt(fdt) {
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +0000122 let range = swiotlb_info.fixed_range().ok_or_else(|| {
Alice Wangb6d2c642023-06-13 13:07:06 +0000123 error!("Pre-shared pool range not specified in swiotlb node");
124 Error::from(FdtError::BadValue)
125 })?;
Chris Wailes0f121752024-09-06 10:45:06 -0700126 MEMORY.lock().as_mut().unwrap().init_static_shared_pool(range).inspect_err(|_| {
Alice Wangb6d2c642023-06-13 13:07:06 +0000127 error!("Failed to initialize pre-shared pool.");
Alice Wangb6d2c642023-06-13 13:07:06 +0000128 })?;
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +0000129 } else {
130 info!("No MEM_SHARE capability detected or swiotlb found: allocating buffers from heap.");
Chris Wailes0f121752024-09-06 10:45:06 -0700131 MEMORY.lock().as_mut().unwrap().init_heap_shared_pool().inspect_err(|_| {
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +0000132 error!("Failed to initialize heap-based pseudo-shared pool.");
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +0000133 })?;
Alice Wangb6d2c642023-06-13 13:07:06 +0000134 }
Alice Wang7b2ab942023-09-12 13:04:42 +0000135
Alice Wanga2228b92024-07-26 08:38:47 +0000136 let bcc_handover: Box<dyn DiceArtifacts> = match vm_type(fdt)? {
Alice Wang474c0ee2023-09-14 12:52:33 +0000137 VmType::ProtectedVm => {
138 let dice_range = read_dice_range_from(fdt)?;
139 info!("DICE range: {dice_range:#x?}");
Alice Wang9f3ca832023-09-20 09:33:14 +0000140 // SAFETY: This region was written by pvmfw in its writable_data region. The region
141 // has no overlap with the main memory region and is safe to be mapped as read-only
142 // data.
143 let res = unsafe {
144 MEMORY.lock().as_mut().unwrap().alloc_range_outside_main_memory(&dice_range)
145 };
Chris Wailes0f121752024-09-06 10:45:06 -0700146 res.inspect_err(|_| {
Alice Wang9f3ca832023-09-20 09:33:14 +0000147 error!("Failed to use DICE range from DT: {dice_range:#x?}");
Alice Wang9f3ca832023-09-20 09:33:14 +0000148 })?;
149 let dice_start = dice_range.start as *const u8;
150 // SAFETY: There's no memory overlap and the region is mapped as read-only data.
151 let bcc_handover = unsafe { slice::from_raw_parts(dice_start, dice_range.len()) };
Alice Wang77639bf2023-09-21 06:57:12 +0000152 Box::new(bcc_handover_parse(bcc_handover)?)
Alice Wang474c0ee2023-09-14 12:52:33 +0000153 }
Alice Wang77639bf2023-09-21 06:57:12 +0000154 // Currently, a sample DICE data is used for non-protected VMs, as these VMs only run
155 // in tests at the moment.
Alice Wang4ac9c8b2023-12-05 16:23:14 +0000156 VmType::NonProtectedVm => Box::new(service_vm::fake_service_vm_dice_artifacts()?),
Alice Wang474c0ee2023-09-14 12:52:33 +0000157 };
Alice Wangd36c7112023-07-04 09:50:45 +0000158
159 let pci_info = PciInfo::from_fdt(fdt)?;
160 debug!("PCI: {pci_info:#x?}");
Alice Wang62183352023-07-04 08:33:27 +0000161 let mut pci_root = pci::initialize(pci_info, MEMORY.lock().as_mut().unwrap())
Alice Wangd36c7112023-07-04 09:50:45 +0000162 .map_err(Error::PciInitializationFailed)?;
163 debug!("PCI root: {pci_root:#x?}");
Alice Wang62183352023-07-04 08:33:27 +0000164 let socket_device = find_socket_device::<HalImpl>(&mut pci_root)?;
165 debug!("Found socket device: guest cid = {:?}", socket_device.guest_cid());
Alice Wang9eebbab2024-04-10 14:57:27 +0000166 let vendor_hashtree_root_digest = read_vendor_hashtree_root_digest(fdt)?;
167 let request_context =
168 RequestContext { dice_artifacts: bcc_handover.as_ref(), vendor_hashtree_root_digest };
Alice Wang4e082c32023-07-11 07:41:50 +0000169
Alice Wanga2228b92024-07-26 08:38:47 +0000170 let mut vsock_stream = VsockStream::new(socket_device, host_addr(fdt)?)?;
Alice Wangfbdc85b2023-09-07 12:56:46 +0000171 while let ServiceVmRequest::Process(req) = vsock_stream.read_request()? {
Alice Wang2e6cdc12024-02-19 11:36:36 +0000172 info!("Received request: {}", req.name());
Alice Wang9eebbab2024-04-10 14:57:27 +0000173 let response = process_request(req, &request_context);
Alice Wang2e6cdc12024-02-19 11:36:36 +0000174 info!("Sending response: {}", response.name());
Alice Wangfbdc85b2023-09-07 12:56:46 +0000175 vsock_stream.write_response(&response)?;
176 vsock_stream.flush()?;
177 }
Alice Wang748b0322023-07-24 12:51:18 +0000178 vsock_stream.shutdown()?;
Alice Wang4e082c32023-07-11 07:41:50 +0000179
Alice Wang9a8b39f2023-04-12 15:31:48 +0000180 Ok(())
181}
182
Alice Wang62183352023-07-04 08:33:27 +0000183fn find_socket_device<T: Hal>(pci_root: &mut PciRoot) -> Result<VirtIOSocket<T>> {
184 PciTransportIterator::<T>::new(pci_root)
185 .find(|t| DeviceType::Socket == t.device_type())
186 .map(VirtIOSocket::<T>::new)
187 .transpose()
188 .map_err(Error::VirtIOSocketCreationFailed)?
189 .ok_or(Error::MissingVirtIOSocketDevice)
190}
191
Alice Wang9a8b39f2023-04-12 15:31:48 +0000192/// Entry point for Rialto.
Alice Wangdda3ba92023-05-25 15:15:30 +0000193pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +0000194 log::set_max_level(log::LevelFilter::Debug);
Alice Wangdda3ba92023-05-25 15:15:30 +0000195 // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to
196 // a valid `Fdt`.
Pierre-Clément Tosi38b6c782024-11-02 13:47:42 +0000197 if let Err(e) = unsafe { try_main(fdt_addr as usize) } {
198 error!("Rialto failed with {e}");
199 reboot()
Alice Wang9a8b39f2023-04-12 15:31:48 +0000200 }
David Brazdil66fc1202022-07-04 21:48:45 +0100201}
202
Frederick Mayle75842402024-08-05 19:32:08 -0700203generate_image_header!();
David Brazdil66fc1202022-07-04 21:48:45 +0100204main!(main);
Alice Wang65ea4cb2024-04-30 10:07:51 +0000205configure_heap!(SIZE_128KB * 2);