blob: 3398d500a6b9746f5b080eb4b4007b8f9e14774a [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 aarch64_paging::{
27 idmap::IdMap,
28 paging::{Attributes, MemoryRegion},
David Brazdil66fc1202022-07-04 21:48:45 +010029};
30use buddy_system_allocator::LockedHeap;
Pierre-Clément Tosi5fa484f2023-04-21 15:01:50 +010031use core::ops::Range;
Alice Wang90e6f162023-04-17 13:49:45 +000032use hyp::get_hypervisor;
Alice Wang9a8b39f2023-04-12 15:31:48 +000033use log::{debug, error, info};
Pierre-Clément Tosi5fa484f2023-04-21 15:01:50 +010034use vmbase::{layout, main, power::reboot};
David Brazdil66fc1202022-07-04 21:48:45 +010035
36const SZ_1K: usize = 1024;
37const SZ_64K: usize = 64 * SZ_1K;
38const SZ_1M: usize = 1024 * SZ_1K;
39const SZ_1G: usize = 1024 * SZ_1M;
40
41// Root level is given by the value of TCR_EL1.TG0 and TCR_EL1.T0SZ, set in
42// entry.S. For 4KB granule and 39-bit VA, the root level is 1.
43const PT_ROOT_LEVEL: usize = 1;
44const PT_ASID: usize = 1;
45
David Brazdil6629b6e2022-07-09 22:48:49 +010046const PROT_DEV: Attributes = Attributes::from_bits_truncate(
47 Attributes::DEVICE_NGNRE.bits() | Attributes::EXECUTE_NEVER.bits(),
48);
49const PROT_RX: Attributes = Attributes::from_bits_truncate(
50 Attributes::NORMAL.bits() | Attributes::NON_GLOBAL.bits() | Attributes::READ_ONLY.bits(),
51);
52const PROT_RO: Attributes = Attributes::from_bits_truncate(
53 Attributes::NORMAL.bits()
54 | Attributes::NON_GLOBAL.bits()
55 | Attributes::READ_ONLY.bits()
56 | Attributes::EXECUTE_NEVER.bits(),
57);
58const PROT_RW: Attributes = Attributes::from_bits_truncate(
59 Attributes::NORMAL.bits() | Attributes::NON_GLOBAL.bits() | Attributes::EXECUTE_NEVER.bits(),
60);
61
David Brazdil66fc1202022-07-04 21:48:45 +010062#[global_allocator]
63static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
64
65static mut HEAP: [u8; SZ_64K] = [0; SZ_64K];
66
Pierre-Clément Tosi5fa484f2023-04-21 15:01:50 +010067fn into_memreg(r: &Range<usize>) -> MemoryRegion {
68 MemoryRegion::new(r.start, r.end)
David Brazdil66fc1202022-07-04 21:48:45 +010069}
70
71fn init_heap() {
72 // SAFETY: Allocator set to otherwise unused, static memory.
73 unsafe {
74 HEAP_ALLOCATOR.lock().init(&mut HEAP as *mut u8 as usize, HEAP.len());
75 }
76 info!("Initialized heap.");
77}
78
Alice Wang9a8b39f2023-04-12 15:31:48 +000079fn init_kernel_pgt(pgt: &mut IdMap) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010080 // The first 1 GiB of address space is used by crosvm for MMIO.
81 let reg_dev = MemoryRegion::new(0, SZ_1G);
Pierre-Clément Tosi5fa484f2023-04-21 15:01:50 +010082 let reg_text = into_memreg(&layout::text_range());
83 let reg_rodata = into_memreg(&layout::rodata_range());
84 let reg_data = into_memreg(&layout::writable_region());
David Brazdil66fc1202022-07-04 21:48:45 +010085
David Brazdil6629b6e2022-07-09 22:48:49 +010086 debug!("Preparing kernel page table.");
David Brazdil66fc1202022-07-04 21:48:45 +010087 debug!(" dev: {}-{}", reg_dev.start(), reg_dev.end());
88 debug!(" text: {}-{}", reg_text.start(), reg_text.end());
89 debug!(" rodata: {}-{}", reg_rodata.start(), reg_rodata.end());
90 debug!(" data: {}-{}", reg_data.start(), reg_data.end());
91
David Brazdil6629b6e2022-07-09 22:48:49 +010092 pgt.map_range(&reg_dev, PROT_DEV)?;
93 pgt.map_range(&reg_text, PROT_RX)?;
94 pgt.map_range(&reg_rodata, PROT_RO)?;
95 pgt.map_range(&reg_data, PROT_RW)?;
David Brazdil66fc1202022-07-04 21:48:45 +010096
David Brazdil66fc1202022-07-04 21:48:45 +010097 pgt.activate();
98 info!("Activated kernel page table.");
David Brazdilb6463c92022-07-11 15:50:43 +010099 Ok(())
David Brazdil66fc1202022-07-04 21:48:45 +0100100}
101
Alice Wang9a8b39f2023-04-12 15:31:48 +0000102fn try_init_logger() -> Result<()> {
Alice Wang90e6f162023-04-17 13:49:45 +0000103 match get_hypervisor().mmio_guard_init() {
Alice Wang9a8b39f2023-04-12 15:31:48 +0000104 // pKVM blocks MMIO by default, we need to enable MMIO guard to support logging.
Alice Wang90e6f162023-04-17 13:49:45 +0000105 Ok(()) => get_hypervisor().mmio_guard_map(vmbase::console::BASE_ADDRESS)?,
Alice Wang9a8b39f2023-04-12 15:31:48 +0000106 // MMIO guard enroll is not supported in unprotected VM.
Alice Wang90e6f162023-04-17 13:49:45 +0000107 Err(hyp::Error::MmioGuardNotsupported) => {}
Alice Wang9a8b39f2023-04-12 15:31:48 +0000108 Err(e) => return Err(e.into()),
109 };
110 vmbase::logger::init(log::LevelFilter::Debug).map_err(|_| Error::LoggerInit)
111}
David Brazdil66fc1202022-07-04 21:48:45 +0100112
Alice Wang9a8b39f2023-04-12 15:31:48 +0000113fn try_main() -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +0100114 info!("Welcome to Rialto!");
115 init_heap();
116
117 let mut pgt = IdMap::new(PT_ASID, PT_ROOT_LEVEL);
Alice Wang9a8b39f2023-04-12 15:31:48 +0000118 init_kernel_pgt(&mut pgt)?;
119 Ok(())
120}
121
122/// Entry point for Rialto.
123pub fn main(_a0: u64, _a1: u64, _a2: u64, _a3: u64) {
124 if try_init_logger().is_err() {
125 // Don't log anything if the logger initialization fails.
126 reboot();
127 }
128 match try_main() {
129 Ok(()) => info!("Rialto ends successfully."),
130 Err(e) => {
131 error!("Rialto failed with {e}");
132 reboot()
133 }
134 }
David Brazdil66fc1202022-07-04 21:48:45 +0100135}
136
David Brazdil66fc1202022-07-04 21:48:45 +0100137main!(main);