blob: 3e826a2b7b6ab0ef1775beec613a1e873ae37116 [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::{
18 console, heap, logger,
19 power::{reboot, shutdown},
20};
21use hyp::{self, get_mmio_guard};
22
23fn try_console_init() -> Result<(), hyp::Error> {
24 console::init();
25
26 if let Some(mmio_guard) = get_mmio_guard() {
27 mmio_guard.init()?;
28 mmio_guard.map(console::BASE_ADDRESS)?;
29 }
30
31 Ok(())
32}
Andrew Walbranb996b4a2022-04-22 15:15:41 +000033
34/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
35#[no_mangle]
Andrew Walbrane03395a2022-04-29 15:15:49 +000036extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
Andrew Walbranc06e7342023-07-05 14:00:51 +000037 // SAFETY: Only called once, from here, and inaccessible to client code.
Pierre-Clément Tosi8ca7a442023-06-22 13:47:15 +000038 unsafe { heap::init() };
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000039
40 if try_console_init().is_err() {
41 // Don't panic (or log) here to avoid accessing the console.
42 reboot()
43 }
44
45 logger::init().expect("Failed to initialize the logger");
46 // We initialize the logger to Off (like the log crate) and clients should log::set_max_level.
47
Andrew Walbranc06e7342023-07-05 14:00:51 +000048 // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it
49 // has the right type.
Andrew Walbranb996b4a2022-04-22 15:15:41 +000050 unsafe {
Andrew Walbrane03395a2022-04-29 15:15:49 +000051 main(x0, x1, x2, x3);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000052 }
53 shutdown();
54}
55
56extern "Rust" {
57 /// Main function provided by the application using the `main!` macro.
Andrew Walbrane03395a2022-04-29 15:15:49 +000058 fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000059}
60
61/// Marks the main function of the binary.
62///
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000063/// Once main is entered, it can assume that:
64/// - The panic_handler has been configured and panic!() and friends are available;
65/// - The global_allocator has been configured and heap memory is available;
66/// - The logger has been configured and the log::{info, warn, error, ...} macros are available.
67///
Andrew Walbranb996b4a2022-04-22 15:15:41 +000068/// Example:
69///
70/// ```rust
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000071/// use vmbase::main;
Jakob Vukalovicef996292023-04-13 14:28:34 +000072/// use log::{info, LevelFilter};
Andrew Walbranb996b4a2022-04-22 15:15:41 +000073///
74/// main!(my_main);
75///
76/// fn my_main() {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000077/// log::set_max_level(LevelFilter::Info);
Jakob Vukalovicef996292023-04-13 14:28:34 +000078/// info!("Hello world");
Andrew Walbranb996b4a2022-04-22 15:15:41 +000079/// }
80/// ```
81#[macro_export]
82macro_rules! main {
83 ($name:path) => {
84 // Export a symbol with a name matching the extern declaration above.
85 #[export_name = "main"]
Andrew Walbrane03395a2022-04-29 15:15:49 +000086 fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbranb996b4a2022-04-22 15:15:41 +000087 // Ensure that the main function provided by the application has the correct type.
Andrew Walbrane03395a2022-04-29 15:15:49 +000088 $name(arg0, arg1, arg2, arg3)
Andrew Walbranb996b4a2022-04-22 15:15:41 +000089 }
90 };
91}