From fe7251c4139b5b505505f12850ab2ab72dff0031 Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Thu, 20 Jun 2024 06:16:04 +0000 Subject: [PATCH] Rename aster_main as ostd::main --- Cargo.lock | 20 +++++++++---------- Cargo.toml | 2 +- Makefile | 2 +- docs/src/osdk/guide/create-project.md | 2 +- docs/src/osdk/guide/run-project.md | 2 +- docs/src/osdk/reference/manifest.md | 2 +- docs/src/ostd/a-100-line-kernel.md | 4 ++-- kernel/src/lib.rs | 2 +- osdk/src/commands/new/kernel.template | 2 +- osdk/src/util.rs | 8 ++++---- .../myos/src/lib.rs | 2 +- ostd/Cargo.toml | 2 +- .../{aster-main => ostd-macros}/Cargo.toml | 2 +- .../{aster-main => ostd-macros}/src/lib.rs | 17 +++++++++++++--- ostd/src/arch/x86/boot/linux_boot/mod.rs | 2 +- ostd/src/arch/x86/boot/multiboot/mod.rs | 2 +- ostd/src/arch/x86/boot/multiboot2/mod.rs | 2 +- ostd/src/boot/mod.rs | 12 +++++------ ostd/src/lib.rs | 3 ++- ostd/src/prelude.rs | 2 -- 20 files changed, 51 insertions(+), 41 deletions(-) rename ostd/libs/{aster-main => ostd-macros}/Cargo.toml (92%) rename ostd/libs/{aster-main => ostd-macros}/src/lib.rs (57%) diff --git a/Cargo.lock b/Cargo.lock index 29001efd4..a7a791f06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,15 +128,6 @@ dependencies = [ "spin 0.9.8", ] -[[package]] -name = "aster-main" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.49", -] - [[package]] name = "aster-network" version = "0.1.0" @@ -1101,7 +1092,6 @@ dependencies = [ "align_ext", "aml", "array-init", - "aster-main", "bit_field", "bitflags 1.3.2", "bitvec", @@ -1121,6 +1111,7 @@ dependencies = [ "num", "num-derive", "num-traits", + "ostd-macros", "pod", "rsdp", "spin 0.9.8", @@ -1134,6 +1125,15 @@ dependencies = [ "xarray", ] +[[package]] +name = "ostd-macros" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.49", +] + [[package]] name = "owo-colors" version = "3.5.0" diff --git a/Cargo.toml b/Cargo.toml index df1efd782..48e6dc111 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = [ "ostd", "ostd/libs/align_ext", - "ostd/libs/aster-main", + "ostd/libs/ostd-macros", "ostd/libs/id-alloc", "ostd/libs/linux-bzimage/builder", "ostd/libs/linux-bzimage/boot-params", diff --git a/Makefile b/Makefile index 2c1ee6e89..492cc0299 100644 --- a/Makefile +++ b/Makefile @@ -86,7 +86,7 @@ export # or tested without OSDK. NON_OSDK_CRATES := \ ostd/libs/align_ext \ - ostd/libs/aster-main \ + ostd/libs/ostd-macros \ ostd/libs/id-alloc \ ostd/libs/linux-bzimage/builder \ ostd/libs/linux-bzimage/boot-params \ diff --git a/docs/src/osdk/guide/create-project.md b/docs/src/osdk/guide/create-project.md index e4cff2b31..028bf5a59 100644 --- a/docs/src/osdk/guide/create-project.md +++ b/docs/src/osdk/guide/create-project.md @@ -51,7 +51,7 @@ myos/ #### Kernel project The `src/lib.rs` file contains the code for a simple kernel. -The function marked with the `#[aster_main]` macro +The function marked with the `#[ostd::main]` macro is considered the kernel entry point by OSDK. The kernel will print `Hello world from the guest kernel!`to the console diff --git a/docs/src/osdk/guide/run-project.md b/docs/src/osdk/guide/run-project.md index f49114642..7c44bc0bd 100644 --- a/docs/src/osdk/guide/run-project.md +++ b/docs/src/osdk/guide/run-project.md @@ -40,7 +40,7 @@ and then hand over control to the kernel entry point to execute the kernel code. **Note**: Only kernel projects (the projects -that defines the function marked with `#[aster_main]`) +that defines the function marked with `#[ostd::main]`) can be run; library projects cannot. diff --git a/docs/src/osdk/reference/manifest.md b/docs/src/osdk/reference/manifest.md index 918a04a4f..f56403216 100644 --- a/docs/src/osdk/reference/manifest.md +++ b/docs/src/osdk/reference/manifest.md @@ -76,7 +76,7 @@ Here are some additional notes for the fields: 1. The type of current crate. Optional. If not specified, - the default value is inferred from the usage of the macro `#[aster_main]`. + the default value is inferred from the usage of the macro `#[ostd::main]`. if the macro is used, the default value is `kernel`. Otherwise, the default value is `library`. diff --git a/docs/src/ostd/a-100-line-kernel.md b/docs/src/ostd/a-100-line-kernel.md index a10373ec1..3962907a0 100644 --- a/docs/src/ostd/a-100-line-kernel.md +++ b/docs/src/ostd/a-100-line-kernel.md @@ -61,8 +61,8 @@ use ostd::mm::{PageFlags, PAGE_SIZE, Vaddr, FrameAllocOptions, VmIo, VmMapOption /// The kernel's boot and initialization process is managed by Asterinas OSTD. /// After the process is done, the kernel's execution environment /// (e.g., stack, heap, tasks) will be ready for use and the entry function -/// labeled as `#[aster_main]` will be called. -#[aster_main] +/// labeled as `#[ostd::main]` will be called. +#[ostd::main] pub fn main() { let program_binary = include_bytes!("../hello_world"); let user_space = create_user_space(program_binary); diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index c60d51f09..dd59f76b0 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -7,7 +7,7 @@ extern crate ostd; use ostd::prelude::*; -#[aster_main] +#[ostd::main] pub fn main() { println!("[kernel] finish init ostd"); component::init_all(component::parse_metadata!()).unwrap(); diff --git a/osdk/src/commands/new/kernel.template b/osdk/src/commands/new/kernel.template index a6a09c9e7..bb5486add 100644 --- a/osdk/src/commands/new/kernel.template +++ b/osdk/src/commands/new/kernel.template @@ -3,7 +3,7 @@ use ostd::prelude::*; -#[aster_main] +#[ostd::main] fn kernel_main() { println!("Hello world from guest kernel!"); } diff --git a/osdk/src/util.rs b/osdk/src/util.rs index c82491152..1f1e4b855 100644 --- a/osdk/src/util.rs +++ b/osdk/src/util.rs @@ -91,7 +91,7 @@ pub struct CrateInfo { /// If there are multiple kernel crates or no kernel crates in the workspace, /// this function will exit with an error. /// -/// A crate is considered a kernel crate if it utilizes the `aster_main` macro. +/// A crate is considered a kernel crate if it utilizes the `ostd::main` macro. fn get_default_member(metadata: &serde_json::Value) -> &str { let default_members = metadata .get("workspace_default_members") @@ -127,7 +127,7 @@ fn get_default_member(metadata: &serde_json::Value) -> &str { syn::parse_file(&content).unwrap() }; - contains_aster_main_macro(&file) + contains_ostd_main_macro(&file) }) .collect() }; @@ -145,7 +145,7 @@ fn get_default_member(metadata: &serde_json::Value) -> &str { packages[0].get("id").unwrap().as_str().unwrap() } -fn contains_aster_main_macro(file: &syn::File) -> bool { +fn contains_ostd_main_macro(file: &syn::File) -> bool { for item in &file.items { let syn::Item::Fn(item_fn) = item else { continue; @@ -153,7 +153,7 @@ fn contains_aster_main_macro(file: &syn::File) -> bool { for attr in &item_fn.attrs { let attr = format!("{}", attr.to_token_stream()); - if attr.as_str() == "# [aster_main]" { + if attr.as_str() == "# [ostd :: main]" || attr.as_str() == "#[main]" { return true; } } diff --git a/osdk/tests/examples_in_book/work_in_workspace_templates/myos/src/lib.rs b/osdk/tests/examples_in_book/work_in_workspace_templates/myos/src/lib.rs index 6214b0bb3..a8fc07f1d 100644 --- a/osdk/tests/examples_in_book/work_in_workspace_templates/myos/src/lib.rs +++ b/osdk/tests/examples_in_book/work_in_workspace_templates/myos/src/lib.rs @@ -5,7 +5,7 @@ use ostd::prelude::*; -#[aster_main] +#[ostd::main] fn kernel_main() { let avail_mem_as_mb = mylib::available_memory() / 1_000_000; println!("The available memory is {} MB", avail_mem_as_mb); diff --git a/ostd/Cargo.toml b/ostd/Cargo.toml index d03097b44..6ff4bdca1 100644 --- a/ostd/Cargo.toml +++ b/ostd/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] align_ext = { path = "libs/align_ext" } -aster-main = { path = "libs/aster-main" } +ostd-macros = { path = "libs/ostd-macros" } bit_field = "0.10.1" bitflags = "1.3" bitvec = { version = "1.0", default-features = false, features = ["alloc"] } diff --git a/ostd/libs/aster-main/Cargo.toml b/ostd/libs/ostd-macros/Cargo.toml similarity index 92% rename from ostd/libs/aster-main/Cargo.toml rename to ostd/libs/ostd-macros/Cargo.toml index 482ea6015..81172d7ab 100644 --- a/ostd/libs/aster-main/Cargo.toml +++ b/ostd/libs/ostd-macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "aster-main" +name = "ostd-macros" version = "0.1.0" edition = "2021" diff --git a/ostd/libs/aster-main/src/lib.rs b/ostd/libs/ostd-macros/src/lib.rs similarity index 57% rename from ostd/libs/aster-main/src/lib.rs rename to ostd/libs/ostd-macros/src/lib.rs index a0bef7e7d..09f95b6c6 100644 --- a/ostd/libs/aster-main/src/lib.rs +++ b/ostd/libs/ostd-macros/src/lib.rs @@ -4,15 +4,26 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, ItemFn}; -/// The kernel entry point. +/// This macro is used to mark the kernel entry point. +/// +/// # Example +/// +/// ```norun +/// use ostd::prelude::*; +/// +/// #[ostd::main] +/// pub fn main() { +/// println!("hello world"); +/// } +/// ``` #[proc_macro_attribute] -pub fn aster_main(_attr: TokenStream, item: TokenStream) -> TokenStream { +pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream { let main_fn = parse_macro_input!(item as ItemFn); let main_fn_name = &main_fn.sig.ident; quote!( #[no_mangle] - pub fn __aster_main() -> ! { + pub fn __ostd_main() -> ! { ostd::init(); #main_fn_name(); ostd::prelude::abort(); diff --git a/ostd/src/arch/x86/boot/linux_boot/mod.rs b/ostd/src/arch/x86/boot/linux_boot/mod.rs index 16c354e69..c1f05fd10 100644 --- a/ostd/src/arch/x86/boot/linux_boot/mod.rs +++ b/ostd/src/arch/x86/boot/linux_boot/mod.rs @@ -160,5 +160,5 @@ unsafe extern "sysv64" fn __linux_boot(params_ptr: *const BootParams) -> ! { init_framebuffer_info, init_memory_regions, ); - crate::boot::call_aster_main(); + crate::boot::call_ostd_main(); } diff --git a/ostd/src/arch/x86/boot/multiboot/mod.rs b/ostd/src/arch/x86/boot/multiboot/mod.rs index 460c396eb..49daa42a4 100644 --- a/ostd/src/arch/x86/boot/multiboot/mod.rs +++ b/ostd/src/arch/x86/boot/multiboot/mod.rs @@ -404,5 +404,5 @@ unsafe extern "sysv64" fn __multiboot_entry(boot_magic: u32, boot_params: u64) - init_framebuffer_info, init_memory_regions, ); - crate::boot::call_aster_main(); + crate::boot::call_ostd_main(); } diff --git a/ostd/src/arch/x86/boot/multiboot2/mod.rs b/ostd/src/arch/x86/boot/multiboot2/mod.rs index cae146188..47cc3d374 100644 --- a/ostd/src/arch/x86/boot/multiboot2/mod.rs +++ b/ostd/src/arch/x86/boot/multiboot2/mod.rs @@ -167,5 +167,5 @@ unsafe extern "sysv64" fn __multiboot2_entry(boot_magic: u32, boot_params: u64) init_framebuffer_info, init_memory_regions, ); - crate::boot::call_aster_main(); + crate::boot::call_ostd_main(); } diff --git a/ostd/src/boot/mod.rs b/ostd/src/boot/mod.rs index 1bfe8710a..d2aea974f 100644 --- a/ostd/src/boot/mod.rs +++ b/ostd/src/boot/mod.rs @@ -67,7 +67,7 @@ macro_rules! define_global_static_boot_arguments { /// /// For the introduction of a new boot protocol, the entry point could be a novel /// one. The entry point function should register all the boot initialization - /// methods before `aster_main` is called. A boot initialization method takes a + /// methods before `ostd::main` is called. A boot initialization method takes a /// reference of the global static boot information variable and initialize it, /// so that the boot information it represents could be accessed in the kernel /// anywhere. @@ -110,17 +110,17 @@ pub fn init() { /// Calls the OSTD-user defined entrypoint of the actual kernel. /// -/// Any kernel that uses the `ostd` crate should define a function named -/// `aster_main` as the entrypoint. -pub fn call_aster_main() -> ! { +/// Any kernel that uses the `ostd` crate should define a function marked with +/// `ostd::main` as the entrypoint. +pub fn call_ostd_main() -> ! { #[cfg(not(ktest))] unsafe { // The entry point of kernel code, which should be defined by the package that // uses OSTD. extern "Rust" { - fn __aster_main() -> !; + fn __ostd_main() -> !; } - __aster_main(); + __ostd_main(); } #[cfg(ktest)] unsafe { diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index 92622c63e..542fbf97b 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -44,12 +44,13 @@ pub mod task; pub mod trap; pub mod user; +pub use ostd_macros::main; #[cfg(feature = "intel_tdx")] use tdx_guest::init_tdx; pub use self::{cpu::CpuLocal, error::Error, prelude::Result}; -/// Initialize OSTD. +/// Initializes OSTD. /// /// This function represents the first phase booting up the system. It makes /// all functionalities of OSTD available after the call. diff --git a/ostd/src/prelude.rs b/ostd/src/prelude.rs index 8cc88440b..a2eb9ad80 100644 --- a/ostd/src/prelude.rs +++ b/ostd/src/prelude.rs @@ -10,8 +10,6 @@ pub type Result = core::result::Result; pub(crate) use alloc::{boxed::Box, sync::Arc, vec::Vec}; pub(crate) use core::any::Any; -pub use aster_main::aster_main; - pub use crate::{ early_print as print, early_println as println, mm::{Paddr, Vaddr},