blob: 8b1d7c690297310d81f51dfb13259fe1b9dce721 [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 Tosia9b345f2024-04-27 01:01:42 +010018 bionic, console, heap, hyp, logger,
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000019 power::{reboot, shutdown},
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000020 rand,
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000021};
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000022use core::mem::size_of;
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000023
24fn try_console_init() -> Result<(), hyp::Error> {
25 console::init();
26
Pierre-Clément Tosia9b345f2024-04-27 01:01:42 +010027 if let Some(mmio_guard) = hyp::get_mmio_guard() {
Pierre-Clément Tosi15ae4a52023-07-11 13:31:31 +000028 mmio_guard.enroll()?;
29 mmio_guard.validate_granule()?;
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000030 mmio_guard.map(console::BASE_ADDRESS)?;
31 }
32
33 Ok(())
34}
Andrew Walbranb996b4a2022-04-22 15:15:41 +000035
36/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
37#[no_mangle]
Andrew Walbrane03395a2022-04-29 15:15:49 +000038extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
Andrew Walbranc06e7342023-07-05 14:00:51 +000039 // SAFETY: Only called once, from here, and inaccessible to client code.
Pierre-Clément Tosi8ca7a442023-06-22 13:47:15 +000040 unsafe { heap::init() };
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000041
42 if try_console_init().is_err() {
43 // Don't panic (or log) here to avoid accessing the console.
44 reboot()
45 }
46
47 logger::init().expect("Failed to initialize the logger");
48 // We initialize the logger to Off (like the log crate) and clients should log::set_max_level.
49
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000050 const SIZE_OF_STACK_GUARD: usize = size_of::<u64>();
51 let mut stack_guard = [0u8; SIZE_OF_STACK_GUARD];
52 // We keep a null byte at the top of the stack guard to act as a string terminator.
53 let random_guard = &mut stack_guard[..(SIZE_OF_STACK_GUARD - 1)];
54
Pierre-Clément Tosi20daaef2023-10-26 11:34:55 +010055 if let Err(e) = rand::init() {
56 panic!("Failed to initialize a source of entropy: {e}");
57 }
58
59 if let Err(e) = rand::fill_with_entropy(random_guard) {
60 panic!("Failed to get stack canary entropy: {e}");
61 }
62
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000063 bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard);
64
65 // Note: If rust_entry ever returned (which it shouldn't by being -> !), the compiler-injected
66 // stack guard comparison would detect a mismatch and call __stack_chk_fail.
67
Andrew Walbranc06e7342023-07-05 14:00:51 +000068 // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it
69 // has the right type.
Andrew Walbranb996b4a2022-04-22 15:15:41 +000070 unsafe {
Andrew Walbrane03395a2022-04-29 15:15:49 +000071 main(x0, x1, x2, x3);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000072 }
73 shutdown();
74}
75
76extern "Rust" {
77 /// Main function provided by the application using the `main!` macro.
Andrew Walbrane03395a2022-04-29 15:15:49 +000078 fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000079}
80
81/// Marks the main function of the binary.
82///
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000083/// Once main is entered, it can assume that:
84/// - The panic_handler has been configured and panic!() and friends are available;
85/// - The global_allocator has been configured and heap memory is available;
86/// - The logger has been configured and the log::{info, warn, error, ...} macros are available.
87///
Andrew Walbranb996b4a2022-04-22 15:15:41 +000088/// Example:
89///
90/// ```rust
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000091/// use vmbase::main;
Jakob Vukalovicef996292023-04-13 14:28:34 +000092/// use log::{info, LevelFilter};
Andrew Walbranb996b4a2022-04-22 15:15:41 +000093///
94/// main!(my_main);
95///
96/// fn my_main() {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000097/// log::set_max_level(LevelFilter::Info);
Jakob Vukalovicef996292023-04-13 14:28:34 +000098/// info!("Hello world");
Andrew Walbranb996b4a2022-04-22 15:15:41 +000099/// }
100/// ```
101#[macro_export]
102macro_rules! main {
103 ($name:path) => {
104 // Export a symbol with a name matching the extern declaration above.
105 #[export_name = "main"]
Andrew Walbrane03395a2022-04-29 15:15:49 +0000106 fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000107 // Ensure that the main function provided by the application has the correct type.
Andrew Walbrane03395a2022-04-29 15:15:49 +0000108 $name(arg0, arg1, arg2, arg3)
Andrew Walbranb996b4a2022-04-22 15:15:41 +0000109 }
110 };
111}