blob: 0a96d8620bcb2ca50d8a56b70a344691ea233875 [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 Tosi8ca7a442023-06-22 13:47:15 +000017use crate::{console, heap, power::shutdown};
Andrew Walbranb996b4a2022-04-22 15:15:41 +000018
19/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
20#[no_mangle]
Andrew Walbrane03395a2022-04-29 15:15:49 +000021extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
Andrew Walbranc06e7342023-07-05 14:00:51 +000022 // SAFETY: Only called once, from here, and inaccessible to client code.
Pierre-Clément Tosi8ca7a442023-06-22 13:47:15 +000023 unsafe { heap::init() };
Andrew Walbranb996b4a2022-04-22 15:15:41 +000024 console::init();
Andrew Walbranc06e7342023-07-05 14:00:51 +000025 // SAFETY: `main` is provided by the application using the `main!` macro, and we make sure it
26 // has the right type.
Andrew Walbranb996b4a2022-04-22 15:15:41 +000027 unsafe {
Andrew Walbrane03395a2022-04-29 15:15:49 +000028 main(x0, x1, x2, x3);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000029 }
30 shutdown();
31}
32
33extern "Rust" {
34 /// Main function provided by the application using the `main!` macro.
Andrew Walbrane03395a2022-04-29 15:15:49 +000035 fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
Andrew Walbranb996b4a2022-04-22 15:15:41 +000036}
37
38/// Marks the main function of the binary.
39///
40/// Example:
41///
42/// ```rust
Jakob Vukalovicef996292023-04-13 14:28:34 +000043/// use vmbase::{logger, main};
44/// use log::{info, LevelFilter};
Andrew Walbranb996b4a2022-04-22 15:15:41 +000045///
46/// main!(my_main);
47///
48/// fn my_main() {
Jakob Vukalovicef996292023-04-13 14:28:34 +000049/// logger::init(LevelFilter::Info).unwrap();
50/// info!("Hello world");
Andrew Walbranb996b4a2022-04-22 15:15:41 +000051/// }
52/// ```
53#[macro_export]
54macro_rules! main {
55 ($name:path) => {
56 // Export a symbol with a name matching the extern declaration above.
57 #[export_name = "main"]
Andrew Walbrane03395a2022-04-29 15:15:49 +000058 fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
Andrew Walbranb996b4a2022-04-22 15:15:41 +000059 // Ensure that the main function provided by the application has the correct type.
Andrew Walbrane03395a2022-04-29 15:15:49 +000060 $name(arg0, arg1, arg2, arg3)
Andrew Walbranb996b4a2022-04-22 15:15:41 +000061 }
62 };
63}