blob: 77999fbd5a567662b62d9968a19fc844b89efedc [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};
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000026use aarch64_paging::idmap::IdMap;
David Brazdil66fc1202022-07-04 21:48:45 +010027use buddy_system_allocator::LockedHeap;
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};
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000032use vmbase::{layout, main, memory::PageTable, power::reboot};
David Brazdil66fc1202022-07-04 21:48:45 +010033
34const SZ_1K: usize = 1024;
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010035const SZ_4K: usize = 4 * SZ_1K;
David Brazdil66fc1202022-07-04 21:48:45 +010036const SZ_64K: usize = 64 * SZ_1K;
37const SZ_1M: usize = 1024 * SZ_1K;
38const 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.
42const PT_ROOT_LEVEL: usize = 1;
43const PT_ASID: usize = 1;
44
45#[global_allocator]
46static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
47
48static mut HEAP: [u8; SZ_64K] = [0; SZ_64K];
49
David Brazdil66fc1202022-07-04 21:48:45 +010050fn 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 Brazdil66fc1202022-07-04 21:48:45 +010055}
56
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000057fn init_page_table() -> Result<()> {
58 let mut page_table: PageTable = IdMap::new(PT_ASID, PT_ROOT_LEVEL).into();
59
David Brazdil66fc1202022-07-04 21:48:45 +010060 // The first 1 GiB of address space is used by crosvm for MMIO.
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000061 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 Brazdil66fc1202022-07-04 21:48:45 +010066
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000067 // 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 Brazdil66fc1202022-07-04 21:48:45 +010070 info!("Activated kernel page table.");
David Brazdilb6463c92022-07-11 15:50:43 +010071 Ok(())
David Brazdil66fc1202022-07-04 21:48:45 +010072}
73
Alice Wang9a8b39f2023-04-12 15:31:48 +000074fn try_init_logger() -> Result<()> {
Alice Wang90e6f162023-04-17 13:49:45 +000075 match get_hypervisor().mmio_guard_init() {
Alice Wang9a8b39f2023-04-12 15:31:48 +000076 // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging.
Alice Wang90e6f162023-04-17 13:49:45 +000077 Ok(()) => get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?,
Alice Wang9a8b39f2023-04-12 15:31:48 +000078 // MMIO guard enroll is not supported in unprotected VM.
Alice Wang90e6f162023-04-17 13:49:45 +000079 Err(hyp::Error::MmioGuardNotsupported) => {}
Alice Wang9a8b39f2023-04-12 15:31:48 +000080 Err(e) => return Err(e.into()),
81 };
82 vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit)
83}
David Brazdil66fc1202022-07-04 21:48:45 +010084
Alice Wangdda3ba92023-05-25 15:15:30 +000085/// # 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`.
89unsafe fn try_main(fdt_addr: usize) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010090 info!("Welcome to Rialto!");
Alice Wangdda3ba92023-05-25 15:15:30 +000091 // 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 Brazdil66fc1202022-07-04 21:48:45 +010096
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000097 init_page_table()?;
Alice Wang9a8b39f2023-04-12 15:31:48 +000098 Ok(())
99}
100
101/// Entry point for Rialto.
Alice Wangdda3ba92023-05-25 15:15:30 +0000102pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) {
Pierre-Clément Tosiff277572023-04-27 10:06:44 +0000103 init_heap();
Alice Wang9a8b39f2023-04-12 15:31:48 +0000104 if try_init_logger().is_err() {
105 // Don't log anything if the logger initialization fails.
106 reboot();
107 }
Alice Wangdda3ba92023-05-25 15:15:30 +0000108 // 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 Wang9a8b39f2023-04-12 15:31:48 +0000111 Ok(()) => info!("Rialto ends successfully."),
112 Err(e) => {
113 error!("Rialto failed with {e}");
114 reboot()
115 }
116 }
David Brazdil66fc1202022-07-04 21:48:45 +0100117}
118
David Brazdil66fc1202022-07-04 21:48:45 +0100119main!(main);