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 | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 31 | use hyp::mmio_guard; |
| 32 | use log::{debug, error, info}; |
| 33 | use vmbase::{main, power::reboot}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 34 | |
| 35 | const SZ_1K: usize = 1024; |
| 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 | |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 45 | const PROT_DEV: Attributes = Attributes::from_bits_truncate( |
| 46 | Attributes::DEVICE_NGNRE.bits() | Attributes::EXECUTE_NEVER.bits(), |
| 47 | ); |
| 48 | const PROT_RX: Attributes = Attributes::from_bits_truncate( |
| 49 | Attributes::NORMAL.bits() | Attributes::NON_GLOBAL.bits() | Attributes::READ_ONLY.bits(), |
| 50 | ); |
| 51 | const PROT_RO: Attributes = Attributes::from_bits_truncate( |
| 52 | Attributes::NORMAL.bits() |
| 53 | | Attributes::NON_GLOBAL.bits() |
| 54 | | Attributes::READ_ONLY.bits() |
| 55 | | Attributes::EXECUTE_NEVER.bits(), |
| 56 | ); |
| 57 | const PROT_RW: Attributes = Attributes::from_bits_truncate( |
| 58 | Attributes::NORMAL.bits() | Attributes::NON_GLOBAL.bits() | Attributes::EXECUTE_NEVER.bits(), |
| 59 | ); |
| 60 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 61 | #[global_allocator] |
| 62 | static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new(); |
| 63 | |
| 64 | static mut HEAP: [u8; SZ_64K] = [0; SZ_64K]; |
| 65 | |
| 66 | unsafe fn kimg_ptr(sym: &u8) -> *const u8 { |
| 67 | sym as *const u8 |
| 68 | } |
| 69 | |
| 70 | unsafe fn kimg_addr(sym: &u8) -> usize { |
| 71 | kimg_ptr(sym) as usize |
| 72 | } |
| 73 | |
| 74 | unsafe fn kimg_region(begin: &u8, end: &u8) -> MemoryRegion { |
| 75 | MemoryRegion::new(kimg_addr(begin), kimg_addr(end)) |
| 76 | } |
| 77 | |
| 78 | fn init_heap() { |
| 79 | // SAFETY: Allocator set to otherwise unused, static memory. |
| 80 | unsafe { |
| 81 | HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len()); |
| 82 | } |
| 83 | info!("Initialized heap."); |
| 84 | } |
| 85 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 86 | fn init_kernel_pgt(pgt: &mut IdMap) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 87 | // The first 1 GiB of address space is used by crosvm for MMIO. |
| 88 | let reg_dev = MemoryRegion::new(0, SZ_1G); |
| 89 | // SAFETY: Taking addresses of kernel image sections to set up page table |
| 90 | // mappings. Not taking ownerhip of the memory. |
| 91 | let reg_text = unsafe { kimg_region(&text_begin, &text_end) }; |
| 92 | let reg_rodata = unsafe { kimg_region(&rodata_begin, &rodata_end) }; |
| 93 | let reg_data = unsafe { kimg_region(&data_begin, &boot_stack_end) }; |
| 94 | |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 95 | debug!("Preparing kernel page table."); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 96 | debug!(" dev: {}-{}", reg_dev.start(), reg_dev.end()); |
| 97 | debug!(" text: {}-{}", reg_text.start(), reg_text.end()); |
| 98 | debug!(" rodata: {}-{}", reg_rodata.start(), reg_rodata.end()); |
| 99 | debug!(" data: {}-{}", reg_data.start(), reg_data.end()); |
| 100 | |
David Brazdil | 6629b6e | 2022-07-09 22:48:49 +0100 | [diff] [blame] | 101 | pgt.map_range(®_dev, PROT_DEV)?; |
| 102 | pgt.map_range(®_text, PROT_RX)?; |
| 103 | pgt.map_range(®_rodata, PROT_RO)?; |
| 104 | pgt.map_range(®_data, PROT_RW)?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 105 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 106 | pgt.activate(); |
| 107 | info!("Activated kernel page table."); |
David Brazdil | b6463c9 | 2022-07-11 15:50:43 +0100 | [diff] [blame] | 108 | Ok(()) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 111 | fn try_init_logger() -> Result<()> { |
| 112 | match mmio_guard::init() { |
| 113 | // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging. |
| 114 | Ok(()) => mmio_guard::map(vmbase::console::BASE_ADDRESS)?, |
| 115 | // MMIO guard enroll is not supported in unprotected VM. |
| 116 | Err(mmio_guard::Error::EnrollFailed(smccc::Error::NotSupported)) => {} |
| 117 | Err(e) => return Err(e.into()), |
| 118 | }; |
| 119 | vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit) |
| 120 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 121 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 122 | fn try_main() -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 123 | info!("Welcome to Rialto!"); |
| 124 | init_heap(); |
| 125 | |
| 126 | let mut pgt = IdMap::new(PT_ASID, PT_ROOT_LEVEL); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 127 | init_kernel_pgt(&mut pgt)?; |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
| 131 | /// Entry point for Rialto. |
| 132 | pub fn main(_a0: u64, _a1: u64, _a2: u64, _a3: u64) { |
| 133 | if try_init_logger().is_err() { |
| 134 | // Don't log anything if the logger initialization fails. |
| 135 | reboot(); |
| 136 | } |
| 137 | match try_main() { |
| 138 | Ok(()) => info!("Rialto ends successfully."), |
| 139 | Err(e) => { |
| 140 | error!("Rialto failed with {e}"); |
| 141 | reboot() |
| 142 | } |
| 143 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | extern "C" { |
| 147 | static text_begin: u8; |
| 148 | static text_end: u8; |
| 149 | static rodata_begin: u8; |
| 150 | static rodata_end: u8; |
| 151 | static data_begin: u8; |
| 152 | static boot_stack_end: u8; |
| 153 | } |
| 154 | |
| 155 | main!(main); |