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::{ |
Per Larsen | 7ec45d3 | 2024-11-02 00:56:46 +0000 | [diff] [blame] | 18 | bionic, console, heap, |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 19 | layout::{UART_ADDRESSES, UART_PAGE_ADDR}, |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 20 | logger, |
Pierre-Clément Tosi | 1f7c523 | 2024-08-13 19:51:25 +0100 | [diff] [blame] | 21 | memory::{PAGE_SIZE, SIZE_16KB, SIZE_4KB}, |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 22 | power::{reboot, shutdown}, |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 23 | rand, |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 24 | }; |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 25 | use core::mem::size_of; |
Per Larsen | 7ec45d3 | 2024-11-02 00:56:46 +0000 | [diff] [blame] | 26 | use hypervisor_backends::{get_mmio_guard, Error}; |
Pierre-Clément Tosi | c08e63c | 2024-05-14 11:17:47 +0100 | [diff] [blame] | 27 | use static_assertions::const_assert_eq; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 28 | |
Per Larsen | 7ec45d3 | 2024-11-02 00:56:46 +0000 | [diff] [blame] | 29 | fn try_console_init() -> Result<(), Error> { |
| 30 | if let Some(mmio_guard) = get_mmio_guard() { |
Pierre-Clément Tosi | 15ae4a5 | 2023-07-11 13:31:31 +0000 | [diff] [blame] | 31 | mmio_guard.enroll()?; |
Pierre-Clément Tosi | 32d9679 | 2024-04-29 01:19:37 +0100 | [diff] [blame] | 32 | |
| 33 | // TODO(ptosi): Use MmioSharer::share() to properly track this MMIO_GUARD_MAP. |
| 34 | // |
Pierre-Clément Tosi | c08e63c | 2024-05-14 11:17:47 +0100 | [diff] [blame] | 35 | // The following call shares the UART but also anything else present in 0..granule. |
| 36 | // |
| 37 | // For 4KiB, that's only the UARTs. For 16KiB, it also covers the RTC and watchdog but, as |
| 38 | // neither is used by vmbase clients (and as both are outside of the UART page), they |
| 39 | // will never have valid stage-1 mappings to those devices. As a result, this |
| 40 | // MMIO_GUARD_MAP isn't affected by the granule size in any visible way. Larger granule |
| 41 | // sizes will need to be checked separately, if needed. |
| 42 | assert!({ |
| 43 | let granule = mmio_guard.granule()?; |
| 44 | granule == SIZE_4KB || granule == SIZE_16KB |
| 45 | }); |
| 46 | // Validate the assumption above by ensuring that the UART is not moved to another page: |
Pierre-Clément Tosi | 38a3621 | 2024-06-06 11:30:39 +0100 | [diff] [blame] | 47 | const_assert_eq!(UART_PAGE_ADDR, 0); |
| 48 | mmio_guard.map(UART_PAGE_ADDR)?; |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 51 | // SAFETY: UART_PAGE is mapped at stage-1 (see entry.S) and was just MMIO-guarded. |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 52 | unsafe { console::init(&UART_ADDRESSES) }; |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 53 | |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 54 | Ok(()) |
| 55 | } |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 56 | |
| 57 | /// This is the entry point to the Rust code, called from the binary entry point in `entry.S`. |
| 58 | #[no_mangle] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 59 | extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! { |
Andrew Walbran | 2e059cc | 2024-10-15 18:22:05 +0100 | [diff] [blame] | 60 | heap::init(); |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 61 | |
| 62 | if try_console_init().is_err() { |
| 63 | // Don't panic (or log) here to avoid accessing the console. |
| 64 | reboot() |
| 65 | } |
| 66 | |
| 67 | logger::init().expect("Failed to initialize the logger"); |
| 68 | // We initialize the logger to Off (like the log crate) and clients should log::set_max_level. |
| 69 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 70 | const SIZE_OF_STACK_GUARD: usize = size_of::<u64>(); |
| 71 | let mut stack_guard = [0u8; SIZE_OF_STACK_GUARD]; |
| 72 | // We keep a null byte at the top of the stack guard to act as a string terminator. |
| 73 | let random_guard = &mut stack_guard[..(SIZE_OF_STACK_GUARD - 1)]; |
| 74 | |
Pierre-Clément Tosi | 20daaef | 2023-10-26 11:34:55 +0100 | [diff] [blame] | 75 | if let Err(e) = rand::init() { |
| 76 | panic!("Failed to initialize a source of entropy: {e}"); |
| 77 | } |
| 78 | |
| 79 | if let Err(e) = rand::fill_with_entropy(random_guard) { |
| 80 | panic!("Failed to get stack canary entropy: {e}"); |
| 81 | } |
| 82 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 83 | bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard); |
| 84 | |
| 85 | // Note: If rust_entry ever returned (which it shouldn't by being -> !), the compiler-injected |
| 86 | // stack guard comparison would detect a mismatch and call __stack_chk_fail. |
| 87 | |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 88 | // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it |
| 89 | // has the right type. |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 90 | unsafe { |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 91 | main(x0, x1, x2, x3); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 92 | } |
| 93 | shutdown(); |
| 94 | } |
| 95 | |
| 96 | extern "Rust" { |
| 97 | /// Main function provided by the application using the `main!` macro. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 98 | fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// Marks the main function of the binary. |
| 102 | /// |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 103 | /// Once main is entered, it can assume that: |
| 104 | /// - The panic_handler has been configured and panic!() and friends are available; |
| 105 | /// - The global_allocator has been configured and heap memory is available; |
| 106 | /// - The logger has been configured and the log::{info, warn, error, ...} macros are available. |
| 107 | /// |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 108 | /// Example: |
| 109 | /// |
| 110 | /// ```rust |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 111 | /// use vmbase::main; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 112 | /// use log::{info, LevelFilter}; |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 113 | /// |
| 114 | /// main!(my_main); |
| 115 | /// |
| 116 | /// fn my_main() { |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 117 | /// log::set_max_level(LevelFilter::Info); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 118 | /// info!("Hello world"); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 119 | /// } |
| 120 | /// ``` |
| 121 | #[macro_export] |
| 122 | macro_rules! main { |
| 123 | ($name:path) => { |
| 124 | // Export a symbol with a name matching the extern declaration above. |
| 125 | #[export_name = "main"] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 126 | fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 127 | // 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] | 128 | $name(arg0, arg1, arg2, arg3) |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | } |
Pierre-Clément Tosi | 1f7c523 | 2024-08-13 19:51:25 +0100 | [diff] [blame] | 132 | |
| 133 | /// Prepends a Linux kernel header to the generated binary image. |
| 134 | /// |
| 135 | /// See https://docs.kernel.org/arch/arm64/booting.html |
| 136 | /// ``` |
| 137 | #[macro_export] |
| 138 | macro_rules! generate_image_header { |
| 139 | () => { |
| 140 | #[cfg(not(target_endian = "little"))] |
| 141 | compile_error!("Image header uses wrong endianness: bootloaders expect LE!"); |
| 142 | |
| 143 | core::arch::global_asm!( |
| 144 | // This section gets linked at the start of the image. |
| 145 | ".section .init.head, \"ax\"", |
| 146 | // This prevents the macro from being called more than once. |
| 147 | ".global image_header", |
| 148 | "image_header:", |
| 149 | // Linux uses a special NOP to be ELF-compatible; we're not. |
| 150 | "nop", // code0 |
| 151 | "b entry", // code1 |
| 152 | ".quad 0", // text_offset |
| 153 | ".quad bin_end - image_header", // image_size |
| 154 | ".quad (1 << 1)", // flags (PAGE_SIZE=4KiB) |
| 155 | ".quad 0", // res2 |
| 156 | ".quad 0", // res3 |
| 157 | ".quad 0", // res4 |
| 158 | ".ascii \"ARM\x64\"", // magic |
| 159 | ".long 0", // res5 |
| 160 | ); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | // If this fails, the image header flags are out-of-sync with PAGE_SIZE! |
| 165 | static_assertions::const_assert_eq!(PAGE_SIZE, SIZE_4KB); |