blob: 5db0f2520f4fd22e63624f66766b1e995aa287d3 [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::{
Michał Mazurekb366b732024-11-29 15:12:11 +010018 arch::platform,
19 bionic, heap, logger,
20 memory::{switch_to_dynamic_page_tables, PAGE_SIZE, SIZE_4KB},
21 power::shutdown,
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000022 rand,
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000023};
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000024use core::mem::size_of;
Andrew Walbranb996b4a2022-04-22 15:15:41 +000025
26/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
27#[no_mangle]
Andrew Walbrane03395a2022-04-29 15:15:49 +000028extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
Andrew Walbran2e059cc2024-10-15 18:22:05 +010029 heap::init();
Michał Mazurekb366b732024-11-29 15:12:11 +010030 // Initialize platform drivers
31 platform::init_console();
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000032
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 Tosi62ffc0d2023-06-30 09:31:56 +000036 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
Pierre-Clément Tosi20daaef2023-10-26 11:34:55 +010041 if let Err(e) = rand::init() {
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 Tosi62ffc0d2023-06-30 09:31:56 +000049 bionic::__get_tls().stack_guard = u64::from_ne_bytes(stack_guard);
50
Pierre-Clément Tosiae071612024-11-02 13:13:34 +000051 switch_to_dynamic_page_tables();
52
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000053 // 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 Walbranc06e7342023-07-05 14:00:51 +000056 // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it
57 // has the right type.
Andrew Walbranb996b4a2022-04-22 15:15:41 +000058 unsafe {
Andrew Walbrane03395a2022-04-29 15:15:49 +000059 main(x0, x1, x2, x3);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000060 }
61 shutdown();
62}
63
64extern "Rust" {
65 /// Main function provided by the application using the `main!` macro.
Andrew Walbrane03395a2022-04-29 15:15:49 +000066 fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000067}
68
69/// Marks the main function of the binary.
70///
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000071/// 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 Walbranb996b4a2022-04-22 15:15:41 +000076/// Example:
77///
78/// ```rust
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000079/// use vmbase::main;
Jakob Vukalovicef996292023-04-13 14:28:34 +000080/// use log::{info, LevelFilter};
Andrew Walbranb996b4a2022-04-22 15:15:41 +000081///
82/// main!(my_main);
83///
84/// fn my_main() {
Pierre-Clément Tosid3305482023-06-29 15:03:48 +000085/// log::set_max_level(LevelFilter::Info);
Jakob Vukalovicef996292023-04-13 14:28:34 +000086/// info!("Hello world");
Andrew Walbranb996b4a2022-04-22 15:15:41 +000087/// }
88/// ```
89#[macro_export]
90macro_rules! main {
91 ($name:path) => {
92 // Export a symbol with a name matching the extern declaration above.
93 #[export_name = "main"]
Andrew Walbrane03395a2022-04-29 15:15:49 +000094 fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbranb996b4a2022-04-22 15:15:41 +000095 // Ensure that the main function provided by the application has the correct type.
Andrew Walbrane03395a2022-04-29 15:15:49 +000096 $name(arg0, arg1, arg2, arg3)
Andrew Walbranb996b4a2022-04-22 15:15:41 +000097 }
98 };
99}
Pierre-Clément Tosi1f7c5232024-08-13 19:51:25 +0100100
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]
106macro_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!
133static_assertions::const_assert_eq!(PAGE_SIZE, SIZE_4KB);