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::{ |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 18 | bionic, console, heap, logger, |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 19 | power::{reboot, shutdown}, |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 20 | rand, |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 21 | }; |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 22 | use core::mem::size_of; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 23 | use hyp::{self, get_mmio_guard}; |
| 24 | |
| 25 | fn try_console_init() -> Result<(), hyp::Error> { |
| 26 | console::init(); |
| 27 | |
| 28 | if let Some(mmio_guard) = get_mmio_guard() { |
Pierre-Clément Tosi | 15ae4a5 | 2023-07-11 13:31:31 +0000 | [diff] [blame^] | 29 | mmio_guard.enroll()?; |
| 30 | mmio_guard.validate_granule()?; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 31 | mmio_guard.map(console::BASE_ADDRESS)?; |
| 32 | } |
| 33 | |
| 34 | Ok(()) |
| 35 | } |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 36 | |
| 37 | /// This is the entry point to the Rust code, called from the binary entry point in `entry.S`. |
| 38 | #[no_mangle] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 39 | 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] | 40 | // 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] | 41 | unsafe { heap::init() }; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 42 | |
| 43 | if try_console_init().is_err() { |
| 44 | // Don't panic (or log) here to avoid accessing the console. |
| 45 | reboot() |
| 46 | } |
| 47 | |
| 48 | logger::init().expect("Failed to initialize the logger"); |
| 49 | // We initialize the logger to Off (like the log crate) and clients should log::set_max_level. |
| 50 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 51 | const SIZE_OF_STACK_GUARD: usize = size_of::<u64>(); |
| 52 | let mut stack_guard = [0u8; SIZE_OF_STACK_GUARD]; |
| 53 | // We keep a null byte at the top of the stack guard to act as a string terminator. |
| 54 | let random_guard = &mut stack_guard[..(SIZE_OF_STACK_GUARD - 1)]; |
| 55 | |
| 56 | rand::init().expect("Failed to initialize a source of entropy"); |
| 57 | rand::fill_with_entropy(random_guard).expect("Failed to get stack canary entropy"); |
| 58 | bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard); |
| 59 | |
| 60 | // Note: If rust_entry ever returned (which it shouldn't by being -> !), the compiler-injected |
| 61 | // stack guard comparison would detect a mismatch and call __stack_chk_fail. |
| 62 | |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 63 | // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it |
| 64 | // has the right type. |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 65 | unsafe { |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 66 | main(x0, x1, x2, x3); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 67 | } |
| 68 | shutdown(); |
| 69 | } |
| 70 | |
| 71 | extern "Rust" { |
| 72 | /// Main function provided by the application using the `main!` macro. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 73 | fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// Marks the main function of the binary. |
| 77 | /// |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 78 | /// Once main is entered, it can assume that: |
| 79 | /// - The panic_handler has been configured and panic!() and friends are available; |
| 80 | /// - The global_allocator has been configured and heap memory is available; |
| 81 | /// - The logger has been configured and the log::{info, warn, error, ...} macros are available. |
| 82 | /// |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 83 | /// Example: |
| 84 | /// |
| 85 | /// ```rust |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 86 | /// use vmbase::main; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 87 | /// use log::{info, LevelFilter}; |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 88 | /// |
| 89 | /// main!(my_main); |
| 90 | /// |
| 91 | /// fn my_main() { |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 92 | /// log::set_max_level(LevelFilter::Info); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 93 | /// info!("Hello world"); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 94 | /// } |
| 95 | /// ``` |
| 96 | #[macro_export] |
| 97 | macro_rules! main { |
| 98 | ($name:path) => { |
| 99 | // Export a symbol with a name matching the extern declaration above. |
| 100 | #[export_name = "main"] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 101 | fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 102 | // 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] | 103 | $name(arg0, arg1, arg2, arg3) |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | } |