blob: 3e0485d7a39d0947f6f49da444fc62fbb7ca1b1a [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 Wang9a8b39f2023-04-12 15:31:48 +000020mod error;
David Brazdil66fc1202022-07-04 21:48:45 +010021mod exceptions;
22
23extern crate alloc;
David Brazdil66fc1202022-07-04 21:48:45 +010024
Alice Wang9a8b39f2023-04-12 15:31:48 +000025use crate::error::{Error, Result};
Alice Wang74f7f4b2023-06-13 08:24:50 +000026use core::num::NonZeroUsize;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000027use core::slice;
Alice Wangdda3ba92023-05-25 15:15:30 +000028use fdtpci::PciInfo;
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +000029use hyp::{get_mem_sharer, get_mmio_guard};
Alice Wangb6d2c642023-06-13 13:07:06 +000030use libfdt::FdtError;
Alice Wang9a8b39f2023-04-12 15:31:48 +000031use log::{debug, error, info};
Alice Wang4b3cc112023-06-06 12:22:53 +000032use vmbase::{
Pierre-Clément Tosi6a4808c2023-06-29 09:19:38 +000033 configure_heap,
Alice Wangb6d2c642023-06-13 13:07:06 +000034 fdt::SwiotlbInfo,
Alice Wang89d29592023-06-12 09:41:29 +000035 layout::{self, crosvm},
36 main,
Pierre-Clément Tosic332fae2023-06-22 11:37:12 +000037 memory::{MemoryTracker, PageTable, MEMORY, PAGE_SIZE, SIZE_64KB},
Alice Wang4b3cc112023-06-06 12:22:53 +000038 power::reboot,
Alice Wangd36c7112023-07-04 09:50:45 +000039 virtio::pci,
Alice Wang4b3cc112023-06-06 12:22:53 +000040};
David Brazdil66fc1202022-07-04 21:48:45 +010041
Alice Wangb70bdb52023-06-12 08:17:58 +000042fn new_page_table() -> Result<PageTable> {
Alice Wangee5b1802023-06-07 07:41:54 +000043 let mut page_table = PageTable::default();
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000044
Alice Wanga3931aa2023-07-05 12:52:09 +000045 page_table.map_data(&layout::scratch_range().into())?;
46 page_table.map_data(&layout::stack_range(40 * PAGE_SIZE).into())?;
47 page_table.map_code(&layout::text_range().into())?;
48 page_table.map_rodata(&layout::rodata_range().into())?;
49 page_table.map_device(&layout::console_uart_range().into())?;
David Brazdil66fc1202022-07-04 21:48:45 +010050
Alice Wangb70bdb52023-06-12 08:17:58 +000051 Ok(page_table)
David Brazdil66fc1202022-07-04 21:48:45 +010052}
53
Alice Wangdda3ba92023-05-25 15:15:30 +000054/// # Safety
55///
56/// Behavior is undefined if any of the following conditions are violated:
57/// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`.
58unsafe fn try_main(fdt_addr: usize) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010059 info!("Welcome to Rialto!");
Alice Wangb70bdb52023-06-12 08:17:58 +000060 let page_table = new_page_table()?;
61
62 MEMORY.lock().replace(MemoryTracker::new(
63 page_table,
64 crosvm::MEM_START..layout::MAX_VIRT_ADDR,
65 crosvm::MMIO_RANGE,
66 None, // Rialto doesn't have any payload for now.
67 ));
Alice Wang74f7f4b2023-06-13 08:24:50 +000068
69 let fdt_range = MEMORY
70 .lock()
71 .as_mut()
72 .unwrap()
73 .alloc(fdt_addr, NonZeroUsize::new(crosvm::FDT_MAX_SIZE).unwrap())?;
74 // SAFETY: The tracker validated the range to be in main memory, mapped, and not overlap.
75 let fdt = unsafe { slice::from_raw_parts(fdt_range.start as *mut u8, fdt_range.len()) };
Alice Wang674257a2023-06-13 09:44:53 +000076 // We do not need to validate the DT since it is already validated in pvmfw.
Alice Wang74f7f4b2023-06-13 08:24:50 +000077 let fdt = libfdt::Fdt::from_slice(fdt)?;
Alice Wang74f7f4b2023-06-13 08:24:50 +000078
Alice Wang674257a2023-06-13 09:44:53 +000079 let memory_range = fdt.first_memory_range()?;
80 MEMORY.lock().as_mut().unwrap().shrink(&memory_range).map_err(|e| {
81 error!("Failed to use memory range value from DT: {memory_range:#x?}");
82 e
83 })?;
Alice Wangb6d2c642023-06-13 13:07:06 +000084
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +000085 if let Some(mem_sharer) = get_mem_sharer() {
86 let granule = mem_sharer.granule()?;
Alice Wangb6d2c642023-06-13 13:07:06 +000087 MEMORY.lock().as_mut().unwrap().init_dynamic_shared_pool(granule).map_err(|e| {
88 error!("Failed to initialize dynamically shared pool.");
89 e
90 })?;
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +000091 } else if let Ok(swiotlb_info) = SwiotlbInfo::new_from_fdt(fdt) {
92 let range = swiotlb_info.fixed_range().ok_or_else(|| {
Alice Wangb6d2c642023-06-13 13:07:06 +000093 error!("Pre-shared pool range not specified in swiotlb node");
94 Error::from(FdtError::BadValue)
95 })?;
96 MEMORY.lock().as_mut().unwrap().init_static_shared_pool(range).map_err(|e| {
97 error!("Failed to initialize pre-shared pool.");
98 e
99 })?;
Pierre-Clément Tosi8937cb82023-07-06 15:07:38 +0000100 } else {
101 info!("No MEM_SHARE capability detected or swiotlb found: allocating buffers from heap.");
102 MEMORY.lock().as_mut().unwrap().init_heap_shared_pool().map_err(|e| {
103 error!("Failed to initialize heap-based pseudo-shared pool.");
104 e
105 })?;
Alice Wangb6d2c642023-06-13 13:07:06 +0000106 }
Alice Wangd36c7112023-07-04 09:50:45 +0000107
108 let pci_info = PciInfo::from_fdt(fdt)?;
109 debug!("PCI: {pci_info:#x?}");
Alice Wangeff58392023-07-04 13:32:09 +0000110 let pci_root = pci::initialize(pci_info, MEMORY.lock().as_mut().unwrap())
Alice Wangd36c7112023-07-04 09:50:45 +0000111 .map_err(Error::PciInitializationFailed)?;
112 debug!("PCI root: {pci_root:#x?}");
Alice Wang9a8b39f2023-04-12 15:31:48 +0000113 Ok(())
114}
115
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000116fn try_unshare_all_memory() -> Result<()> {
Alice Wang77d9dd32023-06-07 13:41:21 +0000117 info!("Starting unsharing memory...");
118
Alice Wang77d9dd32023-06-07 13:41:21 +0000119 // No logging after unmapping UART.
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000120 if let Some(mmio_guard) = get_mmio_guard() {
121 mmio_guard.unmap(vmbase::console::BASE_ADDRESS)?;
Alice Wangb70bdb52023-06-12 08:17:58 +0000122 }
123 // Unshares all memory and deactivates page table.
124 drop(MEMORY.lock().take());
Alice Wang77d9dd32023-06-07 13:41:21 +0000125 Ok(())
126}
127
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000128fn unshare_all_memory() {
129 if let Err(e) = try_unshare_all_memory() {
Alice Wang77d9dd32023-06-07 13:41:21 +0000130 error!("Failed to unshare the memory: {e}");
131 }
132}
133
Alice Wang9a8b39f2023-04-12 15:31:48 +0000134/// Entry point for Rialto.
Alice Wangdda3ba92023-05-25 15:15:30 +0000135pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +0000136 log::set_max_level(log::LevelFilter::Debug);
Alice Wangdda3ba92023-05-25 15:15:30 +0000137 // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to
138 // a valid `Fdt`.
139 match unsafe { try_main(fdt_addr as usize) } {
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000140 Ok(()) => unshare_all_memory(),
Alice Wang9a8b39f2023-04-12 15:31:48 +0000141 Err(e) => {
142 error!("Rialto failed with {e}");
Pierre-Clément Tosi910a72d2023-06-29 14:29:29 +0000143 unshare_all_memory();
Alice Wang9a8b39f2023-04-12 15:31:48 +0000144 reboot()
145 }
146 }
David Brazdil66fc1202022-07-04 21:48:45 +0100147}
148
David Brazdil66fc1202022-07-04 21:48:45 +0100149main!(main);
Pierre-Clément Tosi6a4808c2023-06-29 09:19:38 +0000150configure_heap!(SIZE_64KB);