blob: bc5ab2cfba3ea91793b1cb79c560c6c8df27fb06 [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;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000027use core::slice;
Alice Wangdda3ba92023-05-25 15:15:30 +000028use fdtpci::PciInfo;
Alice Wang90e6f162023-04-17 13:49:45 +000029use hyp::get_hypervisor;
Alice Wang9a8b39f2023-04-12 15:31:48 +000030use log::{debug, error, info};
Alice Wang4b3cc112023-06-06 12:22:53 +000031use vmbase::{
32 layout, main,
33 memory::{PageTable, PAGE_SIZE},
34 power::reboot,
35};
David Brazdil66fc1202022-07-04 21:48:45 +010036
37const SZ_1K: usize = 1024;
38const SZ_64K: usize = 64 * SZ_1K;
39const SZ_1M: usize = 1024 * SZ_1K;
40const SZ_1G: usize = 1024 * SZ_1M;
41
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
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000054fn init_page_table() -> Result<()> {
Alice Wangee5b1802023-06-07 07:41:54 +000055 let mut page_table = PageTable::default();
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000056
David Brazdil66fc1202022-07-04 21:48:45 +010057 // The first 1 GiB of address space is used by crosvm for MMIO.
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000058 page_table.map_device(&(0..SZ_1G))?;
59 page_table.map_data(&layout::scratch_range())?;
Alice Wang4b3cc112023-06-06 12:22:53 +000060 page_table.map_data(&layout::stack_range(40 * PAGE_SIZE))?;
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000061 page_table.map_code(&layout::text_range())?;
62 page_table.map_rodata(&layout::rodata_range())?;
Alice Wang807fa592023-06-02 09:54:43 +000063 page_table.map_device(&layout::console_uart_range())?;
David Brazdil66fc1202022-07-04 21:48:45 +010064
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000065 // SAFETY: It is safe to activate the page table by setting `TTBR0_EL1` to point to
66 // it as this is the first time we activate the page table.
67 unsafe { page_table.activate() }
David Brazdil66fc1202022-07-04 21:48:45 +010068 info!("Activated kernel page table.");
David Brazdilb6463c92022-07-11 15:50:43 +010069 Ok(())
David Brazdil66fc1202022-07-04 21:48:45 +010070}
71
Alice Wang9a8b39f2023-04-12 15:31:48 +000072fn try_init_logger() -> Result<()> {
Alice Wang90e6f162023-04-17 13:49:45 +000073 match get_hypervisor().mmio_guard_init() {
Alice Wang9a8b39f2023-04-12 15:31:48 +000074 // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging.
Alice Wang90e6f162023-04-17 13:49:45 +000075 Ok(()) => get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?,
Alice Wang9a8b39f2023-04-12 15:31:48 +000076 // MMIO guard enroll is not supported in unprotected VM.
Alice Wang90e6f162023-04-17 13:49:45 +000077 Err(hyp::Error::MmioGuardNotsupported) => {}
Alice Wang9a8b39f2023-04-12 15:31:48 +000078 Err(e) => return Err(e.into()),
79 };
80 vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit)
81}
David Brazdil66fc1202022-07-04 21:48:45 +010082
Alice Wangdda3ba92023-05-25 15:15:30 +000083/// # Safety
84///
85/// Behavior is undefined if any of the following conditions are violated:
86/// * The `fdt_addr` must be a valid pointer and points to a valid `Fdt`.
87unsafe fn try_main(fdt_addr: usize) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010088 info!("Welcome to Rialto!");
Alice Wangdda3ba92023-05-25 15:15:30 +000089 // SAFETY: The caller ensures that `fdt_addr` is valid.
90 let fdt = unsafe { slice::from_raw_parts(fdt_addr as *mut u8, SZ_1M) };
91 let fdt = libfdt::Fdt::from_slice(fdt)?;
92 let pci_info = PciInfo::from_fdt(fdt)?;
93 debug!("PCI: {:#x?}", pci_info);
David Brazdil66fc1202022-07-04 21:48:45 +010094
Pierre-Clément Tosi3d4c5c32023-05-31 16:57:06 +000095 init_page_table()?;
Alice Wang9a8b39f2023-04-12 15:31:48 +000096 Ok(())
97}
98
99/// Entry point for Rialto.
Alice Wangdda3ba92023-05-25 15:15:30 +0000100pub fn main(fdt_addr: u64, _a1: u64, _a2: u64, _a3: u64) {
Pierre-Clément Tosiff277572023-04-27 10:06:44 +0000101 init_heap();
Alice Wang9a8b39f2023-04-12 15:31:48 +0000102 if try_init_logger().is_err() {
103 // Don't log anything if the logger initialization fails.
104 reboot();
105 }
Alice Wangdda3ba92023-05-25 15:15:30 +0000106 // SAFETY: `fdt_addr` is supposed to be a valid pointer and points to
107 // a valid `Fdt`.
108 match unsafe { try_main(fdt_addr as usize) } {
Alice Wang9a8b39f2023-04-12 15:31:48 +0000109 Ok(()) => info!("Rialto ends successfully."),
110 Err(e) => {
111 error!("Rialto failed with {e}");
112 reboot()
113 }
114 }
David Brazdil66fc1202022-07-04 21:48:45 +0100115}
116
David Brazdil66fc1202022-07-04 21:48:45 +0100117main!(main);