blob: 45bda1b3a3a386569782e2a829a44244f261fa41 [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};
David Brazdil66fc1202022-07-04 21:48:45 +010026use buddy_system_allocator::LockedHeap;
Alice Wang74f7f4b2023-06-13 08:24:50 +000027use core::num::NonZeroUsize;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000028use core::slice;
Alice Wangdda3ba92023-05-25 15:15:30 +000029use fdtpci::PciInfo;
Alice Wang90e6f162023-04-17 13:49:45 +000030use hyp::get_hypervisor;
Alice Wang9a8b39f2023-04-12 15:31:48 +000031use log::{debug, error, info};
Alice Wang4b3cc112023-06-06 12:22:53 +000032use vmbase::{
Alice Wang89d29592023-06-12 09:41:29 +000033 layout::{self, crosvm},
34 main,
Alice Wangb70bdb52023-06-12 08:17:58 +000035 memory::{MemoryTracker, PageTable, MEMORY, PAGE_SIZE},
Alice Wang4b3cc112023-06-06 12:22:53 +000036 power::reboot,
37};
David Brazdil66fc1202022-07-04 21:48:45 +010038
39const SZ_1K: usize = 1024;
40const SZ_64K: usize = 64 * SZ_1K;
David Brazdil66fc1202022-07-04 21:48:45 +010041
David Brazdil66fc1202022-07-04 21:48:45 +010042#[global_allocator]
43static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
44
45static mut HEAP: [u8; SZ_64K] = [0; SZ_64K];
46
David Brazdil66fc1202022-07-04 21:48:45 +010047fn init_heap() {
48 // SAFETY: Allocator set to otherwise unused, static memory.
49 unsafe {
50 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
51 }
David Brazdil66fc1202022-07-04 21:48:45 +010052}
53
Alice Wangb70bdb52023-06-12 08:17:58 +000054fn new_page_table() -> Result<PageTable> {
Alice Wangee5b1802023-06-07 07:41:54 +000055 let mut page_table = PageTable::default();
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000056
Alice Wang89d29592023-06-12 09:41:29 +000057 page_table.map_device(&crosvm::MMIO_RANGE)?;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000058 page_table.map_data(&layout::scratch_range())?;
Alice Wang4b3cc112023-06-06 12:22:53 +000059 page_table.map_data(&layout::stack_range(40 * PAGE_SIZE))?;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000060 page_table.map_code(&layout::text_range())?;
61 page_table.map_rodata(&layout::rodata_range())?;
Alice Wang807fa592023-06-02 09:54:43 +000062 page_table.map_device(&layout::console_uart_range())?;
David Brazdil66fc1202022-07-04 21:48:45 +010063
Alice Wangb70bdb52023-06-12 08:17:58 +000064 Ok(page_table)
David Brazdil66fc1202022-07-04 21:48:45 +010065}
66
Alice Wang77d9dd32023-06-07 13:41:21 +000067fn try_init_logger() -> Result<bool> {
68 let mmio_guard_supported = match get_hypervisor().mmio_guard_init() {
Alice Wang9a8b39f2023-04-12 15:31:48 +000069 // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging.
Alice Wang77d9dd32023-06-07 13:41:21 +000070 Ok(()) => {
71 get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?;
72 true
73 }
Alice Wang9a8b39f2023-04-12 15:31:48 +000074 // MMIO guard enroll is not supported in unprotected VM.
Alice Wang77d9dd32023-06-07 13:41:21 +000075 Err(hyp::Error::MmioGuardNotsupported) => false,
Alice Wang9a8b39f2023-04-12 15:31:48 +000076 Err(e) => return Err(e.into()),
77 };
Alice Wang77d9dd32023-06-07 13:41:21 +000078 vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit)?;
79 Ok(mmio_guard_supported)
Alice Wang9a8b39f2023-04-12 15:31:48 +000080}
David Brazdil66fc1202022-07-04 21:48:45 +010081
Alice Wangdda3ba92023-05-25 15:15:30 +000082/// # Safety
83///
84/// Behavior is undefined if any of the following conditions are violated:
85/// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`.
86unsafe fn try_main(fdt_addr: usize) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010087 info!("Welcome to Rialto!");
Alice Wangb70bdb52023-06-12 08:17:58 +000088 let page_table = new_page_table()?;
89
90 MEMORY.lock().replace(MemoryTracker::new(
91 page_table,
92 crosvm::MEM_START..layout::MAX_VIRT_ADDR,
93 crosvm::MMIO_RANGE,
94 None, // Rialto doesn't have any payload for now.
95 ));
Alice Wang74f7f4b2023-06-13 08:24:50 +000096
97 let fdt_range = MEMORY
98 .lock()
99 .as_mut()
100 .unwrap()
101 .alloc(fdt_addr, NonZeroUsize::new(crosvm::FDT_MAX_SIZE).unwrap())?;
102 // SAFETY: The tracker validated the range to be in main memory, mapped, and not overlap.
103 let fdt = unsafe { slice::from_raw_parts(fdt_range.start as *mut u8, fdt_range.len()) };
104 let fdt = libfdt::Fdt::from_slice(fdt)?;
105 let pci_info = PciInfo::from_fdt(fdt)?;
106 debug!("PCI: {pci_info:#x?}");
107
Alice Wang9a8b39f2023-04-12 15:31:48 +0000108 Ok(())
109}
110
Alice Wang77d9dd32023-06-07 13:41:21 +0000111fn try_unshare_all_memory(mmio_guard_supported: bool) -> Result<()> {
Alice Wang77d9dd32023-06-07 13:41:21 +0000112 info!("Starting unsharing memory...");
113
Alice Wang77d9dd32023-06-07 13:41:21 +0000114 // No logging after unmapping UART.
Alice Wangb70bdb52023-06-12 08:17:58 +0000115 if mmio_guard_supported {
116 get_hypervisor().mmio_guard_unmap(vmbase::console::BASE_ADDRESS)?;
117 }
118 // Unshares all memory and deactivates page table.
119 drop(MEMORY.lock().take());
Alice Wang77d9dd32023-06-07 13:41:21 +0000120 Ok(())
121}
122
123fn unshare_all_memory(mmio_guard_supported: bool) {
124 if let Err(e) = try_unshare_all_memory(mmio_guard_supported) {
125 error!("Failed to unshare the memory: {e}");
126 }
127}
128
Alice Wang9a8b39f2023-04-12 15:31:48 +0000129/// Entry point for Rialto.
Alice Wangdda3ba92023-05-25 15:15:30 +0000130pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) {
Pierre-Clément Tosiff277572023-04-27 10:06:44 +0000131 init_heap();
Alice Wang77d9dd32023-06-07 13:41:21 +0000132 let Ok(mmio_guard_supported) = try_init_logger() else {
Alice Wang9a8b39f2023-04-12 15:31:48 +0000133 // Don't log anything if the logger initialization fails.
134 reboot();
Alice Wang77d9dd32023-06-07 13:41:21 +0000135 };
Alice Wangdda3ba92023-05-25 15:15:30 +0000136 // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to
137 // a valid `Fdt`.
138 match unsafe { try_main(fdt_addr as usize) } {
Alice Wang77d9dd32023-06-07 13:41:21 +0000139 Ok(()) => unshare_all_memory(mmio_guard_supported),
Alice Wang9a8b39f2023-04-12 15:31:48 +0000140 Err(e) => {
141 error!("Rialto failed with {e}");
Alice Wang77d9dd32023-06-07 13:41:21 +0000142 unshare_all_memory(mmio_guard_supported);
Alice Wang9a8b39f2023-04-12 15:31:48 +0000143 reboot()
144 }
145 }
David Brazdil66fc1202022-07-04 21:48:45 +0100146}
147
David Brazdil66fc1202022-07-04 21:48:45 +0100148main!(main);