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}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 26 | use aarch64_paging::{ |
| 27 | idmap::IdMap, |
| 28 | paging::{Attributes, MemoryRegion}, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 29 | }; |
| 30 | use buddy_system_allocator::LockedHeap; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 31 | use core::{ops::Range, slice}; |
| 32 | use fdtpci::PciInfo; |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 33 | use hyp::get_hypervisor; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 34 | use log::{debug, error, info}; |
Pierre-Clément Tosi | 5fa484f | 2023-04-21 15:01:50 +0100 | [diff] [blame] | 35 | use vmbase::{layout, main, power::reboot}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 36 | |
| 37 | const SZ_1K: usize = 1024; |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 38 | const SZ_4K: usize = 4 * SZ_1K; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 39 | const SZ_64K: usize = 64 * SZ_1K; |
| 40 | const SZ_1M: usize = 1024 * SZ_1K; |
| 41 | const SZ_1G: usize = 1024 * SZ_1M; |
| 42 | |
| 43 | // Root level is given by the value of TCR_EL1.TG0 and TCR_EL1.T0SZ, set in |
| 44 | // entry.S. For 4KB granule and 39-bit VA, the root level is 1. |
| 45 | const PT_ROOT_LEVEL: usize = 1; |
| 46 | const PT_ASID: usize = 1; |
| 47 | |
Jakob Vukalovic | 50559df | 2023-04-19 19:24:13 +0100 | [diff] [blame] | 48 | const PROT_DEV: Attributes = |
| 49 | Attributes::DEVICE_NGNRE.union(Attributes::EXECUTE_NEVER).union(Attributes::VALID); |
| 50 | const PROT_RX: Attributes = Attributes::NORMAL |
| 51 | .union(Attributes::NON_GLOBAL) |
| 52 | .union(Attributes::READ_ONLY) |
| 53 | .union(Attributes::VALID); |
| 54 | const PROT_RO: Attributes = Attributes::NORMAL |
| 55 | .union(Attributes::NON_GLOBAL) |
| 56 | .union(Attributes::READ_ONLY) |
| 57 | .union(Attributes::EXECUTE_NEVER) |
| 58 | .union(Attributes::VALID); |
| 59 | const PROT_RW: Attributes = Attributes::NORMAL |
| 60 | .union(Attributes::NON_GLOBAL) |
| 61 | .union(Attributes::EXECUTE_NEVER) |
| 62 | .union(Attributes::VALID); |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 63 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 64 | #[global_allocator] |
| 65 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 66 | |
| 67 | static mut HEAP: [u8; SZ_64K] = [0; SZ_64K]; |
| 68 | |
Pierre-Clément Tosi | 5fa484f | 2023-04-21 15:01:50 +0100 | [diff] [blame] | 69 | fn into_memreg(r: &Range<usize>) -> MemoryRegion { |
| 70 | MemoryRegion::new(r.start, r.end) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | fn init_heap() { |
| 74 | // SAFETY: Allocator set to otherwise unused, static memory. |
| 75 | unsafe { |
| 76 | HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len()); |
| 77 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 80 | fn init_kernel_pgt(pgt: &mut IdMap) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 81 | // The first 1 GiB of address space is used by crosvm for MMIO. |
| 82 | let reg_dev = MemoryRegion::new(0, SZ_1G); |
Pierre-Clément Tosi | 5fa484f | 2023-04-21 15:01:50 +0100 | [diff] [blame] | 83 | let reg_text = into_memreg(&layout::text_range()); |
| 84 | let reg_rodata = into_memreg(&layout::rodata_range()); |
Pierre-Clément Tosi | 8bb3d72 | 2023-04-21 16:10:56 +0100 | [diff] [blame] | 85 | let reg_scratch = into_memreg(&layout::scratch_range()); |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 86 | let reg_stack = into_memreg(&layout::stack_range(40 * SZ_4K)); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 87 | |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 88 | debug!("Preparing kernel page table."); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 89 | debug!(" dev: {}-{}", reg_dev.start(), reg_dev.end()); |
| 90 | debug!(" text: {}-{}", reg_text.start(), reg_text.end()); |
| 91 | debug!(" rodata: {}-{}", reg_rodata.start(), reg_rodata.end()); |
Pierre-Clément Tosi | 8bb3d72 | 2023-04-21 16:10:56 +0100 | [diff] [blame] | 92 | debug!(" scratch:{}-{}", reg_scratch.start(), reg_scratch.end()); |
| 93 | debug!(" stack: {}-{}", reg_stack.start(), reg_stack.end()); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 94 | |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 95 | pgt.map_range(®_dev, PROT_DEV)?; |
| 96 | pgt.map_range(®_text, PROT_RX)?; |
| 97 | pgt.map_range(®_rodata, PROT_RO)?; |
Pierre-Clément Tosi | 8bb3d72 | 2023-04-21 16:10:56 +0100 | [diff] [blame] | 98 | pgt.map_range(®_scratch, PROT_RW)?; |
| 99 | pgt.map_range(®_stack, PROT_RW)?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 100 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 101 | pgt.activate(); |
| 102 | info!("Activated kernel page table."); |
David Brazdil | b6463c9 | 2022-07-11 15:50:43 +0100 | [diff] [blame] | 103 | Ok(()) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 106 | fn try_init_logger() -> Result<()> { |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 107 | match get_hypervisor().mmio_guard_init() { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 108 | // 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] | 109 | Ok(()) => get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?, |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 110 | // MMIO guard enroll is not supported in unprotected VM. |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 111 | Err(hyp::Error::MmioGuardNotsupported) => {} |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 112 | Err(e) => return Err(e.into()), |
| 113 | }; |
| 114 | vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit) |
| 115 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 116 | |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 117 | /// # Safety |
| 118 | /// |
| 119 | /// Behavior is undefined if any of the following conditions are violated: |
| 120 | /// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`. |
| 121 | unsafe fn try_main(fdt_addr: usize) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 122 | info!("Welcome to Rialto!"); |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 123 | // SAFETY: The caller ensures that `fdt_addr` is valid. |
| 124 | let fdt = unsafe { slice::from_raw_parts(fdt_addr as *mut u8, SZ_1M) }; |
| 125 | let fdt = libfdt::Fdt::from_slice(fdt)?; |
| 126 | let pci_info = PciInfo::from_fdt(fdt)?; |
| 127 | debug!("PCI: {:#x?}", pci_info); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 128 | |
| 129 | let mut pgt = IdMap::new(PT_ASID, PT_ROOT_LEVEL); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 130 | init_kernel_pgt(&mut pgt)?; |
| 131 | Ok(()) |
| 132 | } |
| 133 | |
| 134 | /// Entry point for Rialto. |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 135 | 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] | 136 | init_heap(); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 137 | if try_init_logger().is_err() { |
| 138 | // Don't log anything if the logger initialization fails. |
| 139 | reboot(); |
| 140 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 141 | // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to |
| 142 | // a valid `Fdt`. |
| 143 | match unsafe { try_main(fdt_addr as usize) } { |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 144 | Ok(()) => info!("Rialto ends successfully."), |
| 145 | Err(e) => { |
| 146 | error!("Rialto failed with {e}"); |
| 147 | reboot() |
| 148 | } |
| 149 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 150 | } |
| 151 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 152 | main!(main); |