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}; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 26 | use aarch64_paging::idmap::IdMap; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 27 | use buddy_system_allocator::LockedHeap; |
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 | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 30 | use hyp::get_hypervisor; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 31 | use log::{debug, error, info}; |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 32 | use vmbase::{layout, main, memory::PageTable, power::reboot}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 33 | |
| 34 | const SZ_1K: usize = 1024; |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 35 | const SZ_4K: usize = 4 * SZ_1K; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 36 | const SZ_64K: usize = 64 * SZ_1K; |
| 37 | const SZ_1M: usize = 1024 * SZ_1K; |
| 38 | const SZ_1G: usize = 1024 * SZ_1M; |
| 39 | |
| 40 | // Root level is given by the value of TCR_EL1.TG0 and TCR_EL1.T0SZ, set in |
| 41 | // entry.S. For 4KB granule and 39-bit VA, the root level is 1. |
| 42 | const PT_ROOT_LEVEL: usize = 1; |
| 43 | const PT_ASID: usize = 1; |
| 44 | |
| 45 | #[global_allocator] |
| 46 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 47 | |
| 48 | static mut HEAP: [u8; SZ_64K] = [0; SZ_64K]; |
| 49 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 50 | fn init_heap() { |
| 51 | // SAFETY: Allocator set to otherwise unused, static memory. |
| 52 | unsafe { |
| 53 | HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len()); |
| 54 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 57 | fn init_page_table() -> Result<()> { |
| 58 | let mut page_table: PageTable = IdMap::new(PT_ASID, PT_ROOT_LEVEL).into(); |
| 59 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 60 | // The first 1 GiB of address space is used by crosvm for MMIO. |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 61 | page_table.map_device(&(0..SZ_1G))?; |
| 62 | page_table.map_data(&layout::scratch_range())?; |
| 63 | page_table.map_data(&layout::stack_range(40 * SZ_4K))?; |
| 64 | page_table.map_code(&layout::text_range())?; |
| 65 | page_table.map_rodata(&layout::rodata_range())?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 66 | |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 67 | // SAFETY: It is safe to activate the page table by setting `TTBR0_EL1` to point to |
| 68 | // it as this is the first time we activate the page table. |
| 69 | unsafe { page_table.activate() } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 70 | info!("Activated kernel page table."); |
David Brazdil | b6463c9 | 2022-07-11 15:50:43 +0100 | [diff] [blame] | 71 | Ok(()) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 74 | fn try_init_logger() -> Result<()> { |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 75 | match get_hypervisor().mmio_guard_init() { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 76 | // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging. |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 77 | Ok(()) => get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?, |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 78 | // MMIO guard enroll is not supported in unprotected VM. |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 79 | Err(hyp::Error::MmioGuardNotsupported) => {} |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 80 | Err(e) => return Err(e.into()), |
| 81 | }; |
| 82 | vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit) |
| 83 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 84 | |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 85 | /// # Safety |
| 86 | /// |
| 87 | /// Behavior is undefined if any of the following conditions are violated: |
| 88 | /// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`. |
| 89 | unsafe fn try_main(fdt_addr: usize) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 90 | info!("Welcome to Rialto!"); |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 91 | // SAFETY: The caller ensures that `fdt_addr` is valid. |
| 92 | let fdt = unsafe { slice::from_raw_parts(fdt_addr as *mut u8, SZ_1M) }; |
| 93 | let fdt = libfdt::Fdt::from_slice(fdt)?; |
| 94 | let pci_info = PciInfo::from_fdt(fdt)?; |
| 95 | debug!("PCI: {:#x?}", pci_info); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 96 | |
Pierre-Clément Tosi | 3d4c5c3 | 2023-05-31 16:57:06 +0000 | [diff] [blame^] | 97 | init_page_table()?; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 98 | Ok(()) |
| 99 | } |
| 100 | |
| 101 | /// Entry point for Rialto. |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 102 | pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) { |
Pierre-Clément Tosi | ff27757 | 2023-04-27 10:06:44 +0000 | [diff] [blame] | 103 | init_heap(); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 104 | if try_init_logger().is_err() { |
| 105 | // Don't log anything if the logger initialization fails. |
| 106 | reboot(); |
| 107 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 108 | // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to |
| 109 | // a valid `Fdt`. |
| 110 | match unsafe { try_main(fdt_addr as usize) } { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 111 | Ok(()) => info!("Rialto ends successfully."), |
| 112 | Err(e) => { |
| 113 | error!("Rialto failed with {e}"); |
| 114 | reboot() |
| 115 | } |
| 116 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 117 | } |
| 118 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 119 | main!(main); |