Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 1 | // 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 Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame^] | 17 | use crate::{ |
| 18 | console, heap, logger, |
| 19 | power::{reboot, shutdown}, |
| 20 | }; |
| 21 | use hyp::{self, get_mmio_guard}; |
| 22 | |
| 23 | fn 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 Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 33 | |
| 34 | /// This is the entry point to the Rust code, called from the binary entry point in `entry.S`. |
| 35 | #[no_mangle] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 36 | extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! { |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 37 | // SAFETY: Only called once, from here, and inaccessible to client code. |
Pierre-Clément Tosi | 8ca7a44 | 2023-06-22 13:47:15 +0000 | [diff] [blame] | 38 | unsafe { heap::init() }; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame^] | 39 | |
| 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 Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 48 | // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it |
| 49 | // has the right type. |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 50 | unsafe { |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 51 | main(x0, x1, x2, x3); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 52 | } |
| 53 | shutdown(); |
| 54 | } |
| 55 | |
| 56 | extern "Rust" { |
| 57 | /// Main function provided by the application using the `main!` macro. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 58 | fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// Marks the main function of the binary. |
| 62 | /// |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame^] | 63 | /// 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 Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 68 | /// Example: |
| 69 | /// |
| 70 | /// ```rust |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame^] | 71 | /// use vmbase::main; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 72 | /// use log::{info, LevelFilter}; |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 73 | /// |
| 74 | /// main!(my_main); |
| 75 | /// |
| 76 | /// fn my_main() { |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame^] | 77 | /// log::set_max_level(LevelFilter::Info); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 78 | /// info!("Hello world"); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 79 | /// } |
| 80 | /// ``` |
| 81 | #[macro_export] |
| 82 | macro_rules! main { |
| 83 | ($name:path) => { |
| 84 | // Export a symbol with a name matching the extern declaration above. |
| 85 | #[export_name = "main"] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 86 | fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 87 | // Ensure that the main function provided by the application has the correct type. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 88 | $name(arg0, arg1, arg2, arg3) |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | } |