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::{ |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame] | 18 | arch::platform, |
| 19 | bionic, heap, logger, |
| 20 | memory::{switch_to_dynamic_page_tables, PAGE_SIZE, SIZE_4KB}, |
| 21 | power::shutdown, |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 22 | rand, |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 23 | }; |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 24 | use core::mem::size_of; |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 25 | |
| 26 | /// This is the entry point to the Rust code, called from the binary entry point in `entry.S`. |
| 27 | #[no_mangle] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 28 | 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] | 29 | heap::init(); |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame] | 30 | // Initialize platform drivers |
| 31 | platform::init_console(); |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 32 | |
| 33 | logger::init().expect("Failed to initialize the logger"); |
| 34 | // We initialize the logger to Off (like the log crate) and clients should log::set_max_level. |
| 35 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 36 | const SIZE_OF_STACK_GUARD: usize = size_of::<u64>(); |
| 37 | let mut stack_guard = [0u8; SIZE_OF_STACK_GUARD]; |
| 38 | // We keep a null byte at the top of the stack guard to act as a string terminator. |
| 39 | let random_guard = &mut stack_guard[..(SIZE_OF_STACK_GUARD - 1)]; |
| 40 | |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame^] | 41 | if let Err(e) = crate::arch::rand::init() { |
Pierre-Clément Tosi | 20daaef | 2023-10-26 11:34:55 +0100 | [diff] [blame] | 42 | panic!("Failed to initialize a source of entropy: {e}"); |
| 43 | } |
| 44 | |
| 45 | if let Err(e) = rand::fill_with_entropy(random_guard) { |
| 46 | panic!("Failed to get stack canary entropy: {e}"); |
| 47 | } |
| 48 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 49 | bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard); |
| 50 | |
Pierre-Clément Tosi | ae07161 | 2024-11-02 13:13:34 +0000 | [diff] [blame] | 51 | switch_to_dynamic_page_tables(); |
| 52 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 53 | // Note: If rust_entry ever returned (which it shouldn't by being -> !), the compiler-injected |
| 54 | // stack guard comparison would detect a mismatch and call __stack_chk_fail. |
| 55 | |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 56 | // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it |
| 57 | // has the right type. |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 58 | unsafe { |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 59 | main(x0, x1, x2, x3); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 60 | } |
| 61 | shutdown(); |
| 62 | } |
| 63 | |
| 64 | extern "Rust" { |
| 65 | /// Main function provided by the application using the `main!` macro. |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 66 | fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /// Marks the main function of the binary. |
| 70 | /// |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 71 | /// Once main is entered, it can assume that: |
| 72 | /// - The panic_handler has been configured and panic!() and friends are available; |
| 73 | /// - The global_allocator has been configured and heap memory is available; |
| 74 | /// - The logger has been configured and the log::{info, warn, error, ...} macros are available. |
| 75 | /// |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 76 | /// Example: |
| 77 | /// |
| 78 | /// ```rust |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 79 | /// use vmbase::main; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 80 | /// use log::{info, LevelFilter}; |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 81 | /// |
| 82 | /// main!(my_main); |
| 83 | /// |
| 84 | /// fn my_main() { |
Pierre-Clément Tosi | d330548 | 2023-06-29 15:03:48 +0000 | [diff] [blame] | 85 | /// log::set_max_level(LevelFilter::Info); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 86 | /// info!("Hello world"); |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 87 | /// } |
| 88 | /// ``` |
| 89 | #[macro_export] |
| 90 | macro_rules! main { |
| 91 | ($name:path) => { |
| 92 | // Export a symbol with a name matching the extern declaration above. |
| 93 | #[export_name = "main"] |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 94 | fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) { |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 95 | // 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] | 96 | $name(arg0, arg1, arg2, arg3) |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 97 | } |
| 98 | }; |
| 99 | } |
Pierre-Clément Tosi | 1f7c523 | 2024-08-13 19:51:25 +0100 | [diff] [blame] | 100 | |
| 101 | /// Prepends a Linux kernel header to the generated binary image. |
| 102 | /// |
| 103 | /// See https://docs.kernel.org/arch/arm64/booting.html |
| 104 | /// ``` |
| 105 | #[macro_export] |
| 106 | macro_rules! generate_image_header { |
| 107 | () => { |
| 108 | #[cfg(not(target_endian = "little"))] |
| 109 | compile_error!("Image header uses wrong endianness: bootloaders expect LE!"); |
| 110 | |
| 111 | core::arch::global_asm!( |
| 112 | // This section gets linked at the start of the image. |
| 113 | ".section .init.head, \"ax\"", |
| 114 | // This prevents the macro from being called more than once. |
| 115 | ".global image_header", |
| 116 | "image_header:", |
| 117 | // Linux uses a special NOP to be ELF-compatible; we're not. |
| 118 | "nop", // code0 |
| 119 | "b entry", // code1 |
| 120 | ".quad 0", // text_offset |
| 121 | ".quad bin_end - image_header", // image_size |
| 122 | ".quad (1 << 1)", // flags (PAGE_SIZE=4KiB) |
| 123 | ".quad 0", // res2 |
| 124 | ".quad 0", // res3 |
| 125 | ".quad 0", // res4 |
| 126 | ".ascii \"ARM\x64\"", // magic |
| 127 | ".long 0", // res5 |
| 128 | ); |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | // If this fails, the image header flags are out-of-sync with PAGE_SIZE! |
| 133 | static_assertions::const_assert_eq!(PAGE_SIZE, SIZE_4KB); |