blob: dedc6aec30cf592c79813afa594534ed4089767f [file] [log] [blame]
Andrew Walbranb996b4a2022-04-22 15:15:41 +00001// 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//! Rust entry point.
16
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000017use crate::{
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010018 bionic, console, heap, hyp,
19 layout::UART_PAGE_ADDR,
20 logger,
21 memory::{SIZE_16KB, SIZE_4KB},
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000022 power::{reboot, shutdown},
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000023 rand,
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000024};
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000025use core::mem::size_of;
Pierre-Clément Tosic08e63c2024-05-14 11:17:47 +010026use static_assertions::const_assert_eq;
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000027
28fn try_console_init() -> Result<(), hyp::Error> {
29 console::init();
30
Pierre-Clément Tosia9b345f2024-04-27 01:01:42 +010031 if let Some(mmio_guard) = hyp::get_mmio_guard() {
Pierre-Clément Tosi15ae4a52023-07-11 13:31:31 +000032 mmio_guard.enroll()?;
Pierre-Clément Tosi32d96792024-04-29 01:19:37 +010033
34 // TODO(ptosi): Use MmioSharer::share() to properly track this MMIO_GUARD_MAP.
35 //
Pierre-Clément Tosic08e63c2024-05-14 11:17:47 +010036 // The following call shares the UART but also anything else present in 0..granule.
37 //
38 // For 4KiB, that's only the UARTs. For 16KiB, it also covers the RTC and watchdog but, as
39 // neither is used by vmbase clients (and as both are outside of the UART page), they
40 // will never have valid stage-1 mappings to those devices. As a result, this
41 // MMIO_GUARD_MAP isn't affected by the granule size in any visible way. Larger granule
42 // sizes will need to be checked separately, if needed.
43 assert!({
44 let granule = mmio_guard.granule()?;
45 granule == SIZE_4KB || granule == SIZE_16KB
46 });
47 // Validate the assumption above by ensuring that the UART is not moved to another page:
Pierre-Clément Tosi38a36212024-06-06 11:30:39 +010048 const_assert_eq!(UART_PAGE_ADDR, 0);
49 mmio_guard.map(UART_PAGE_ADDR)?;
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000050 }
51
52 Ok(())
53}
Andrew Walbranb996b4a2022-04-22 15:15:41 +000054
55/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
56#[no_mangle]
Andrew Walbrane03395a2022-04-29 15:15:49 +000057extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
Andrew Walbranc06e7342023-07-05 14:00:51 +000058 // SAFETY: Only called once, from here, and inaccessible to client code.
Pierre-Clément Tosi8ca7a442023-06-22 13:47:15 +000059 unsafe { heap::init() };
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000060
61 if try_console_init().is_err() {
62 // Don't panic (or log) here to avoid accessing the console.
63 reboot()
64 }
65
66 logger::init().expect("Failed to initialize the logger");
67 // We initialize the logger to Off (like the log crate) and clients should log::set_max_level.
68
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000069 const SIZE_OF_STACK_GUARD: usize = size_of::<u64>();
70 let mut stack_guard = [0u8; SIZE_OF_STACK_GUARD];
71 // We keep a null byte at the top of the stack guard to act as a string terminator.
72 let random_guard = &mut stack_guard[..(SIZE_OF_STACK_GUARD - 1)];
73
Pierre-Clément Tosi20daaef2023-10-26 11:34:55 +010074 if let Err(e) = rand::init() {
75 panic!("Failed to initialize a source of entropy: {e}");
76 }
77
78 if let Err(e) = rand::fill_with_entropy(random_guard) {
79 panic!("Failed to get stack canary entropy: {e}");
80 }
81
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000082 bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard);
83
84 // Note: If rust_entry ever returned (which it shouldn't by being -> !), the compiler-injected
85 // stack guard comparison would detect a mismatch and call __stack_chk_fail.
86
Andrew Walbranc06e7342023-07-05 14:00:51 +000087 // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it
88 // has the right type.
Andrew Walbranb996b4a2022-04-22 15:15:41 +000089 unsafe {
Andrew Walbrane03395a2022-04-29 15:15:49 +000090 main(x0, x1, x2, x3);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000091 }
92 shutdown();
93}
94
95extern "Rust" {
96 /// Main function provided by the application using the `main!` macro.
Andrew Walbrane03395a2022-04-29 15:15:49 +000097 fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000098}
99
100/// Marks the main function of the binary.
101///
Pierre-Clément Tosid3305482023-06-29 15:03:48 +0000102/// Once main is entered, it can assume that:
103/// - The panic_handler has been configured and panic!() and friends are available;
104/// - The global_allocator has been configured and heap memory is available;
105/// - The logger has been configured and the log::{info, warn, error, ...} macros are available.
106///
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000107/// Example:
108///
109/// ```rust
Pierre-Clément Tosid3305482023-06-29 15:03:48 +0000110/// use vmbase::main;
Jakob Vukalovicef996292023-04-13 14:28:34 +0000111/// use log::{info, LevelFilter};
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000112///
113/// main!(my_main);
114///
115/// fn my_main() {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +0000116/// log::set_max_level(LevelFilter::Info);
Jakob Vukalovicef996292023-04-13 14:28:34 +0000117/// info!("Hello world");
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000118/// }
119/// ```
120#[macro_export]
121macro_rules! main {
122 ($name:path) => {
123 // Export a symbol with a name matching the extern declaration above.
124 #[export_name = "main"]
Andrew Walbrane03395a2022-04-29 15:15:49 +0000125 fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000126 // Ensure that the main function provided by the application has the correct type.
Andrew Walbrane03395a2022-04-29 15:15:49 +0000127 $name(arg0, arg1, arg2, arg3)
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000128 }
129 };
130}