• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 use crate::{console, power::shutdown};
18 
19 /// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
20 #[no_mangle]
rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> !21 extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
22     console::init();
23     unsafe {
24         main(x0, x1, x2, x3);
25     }
26     shutdown();
27 }
28 
29 extern "Rust" {
30     /// Main function provided by the application using the `main!` macro.
main(arg0: u64, arg1: u64, arg2: u64, arg3: u64)31     fn main(arg0: u64, arg1: u64, arg2: u64, arg3: u64);
32 }
33 
34 /// Marks the main function of the binary.
35 ///
36 /// Example:
37 ///
38 /// ```rust
39 /// use vmbase::{logger, main};
40 /// use log::{info, LevelFilter};
41 ///
42 /// main!(my_main);
43 ///
44 /// fn my_main() {
45 ///     logger::init(LevelFilter::Info).unwrap();
46 ///     info!("Hello world");
47 /// }
48 /// ```
49 #[macro_export]
50 macro_rules! main {
51     ($name:path) => {
52         // Export a symbol with a name matching the extern declaration above.
53         #[export_name = "main"]
54         fn __main(arg0: u64, arg1: u64, arg2: u64, arg3: u64) {
55             // Ensure that the main function provided by the application has the correct type.
56             $name(arg0, arg1, arg2, arg3)
57         }
58     };
59 }
60