Skip to content

Commit

Permalink
[feat] Use ctor to init resources manually before entering main func
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Nov 20, 2024
1 parent ea19a87 commit 5ccc2bf
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 16 deletions.
25 changes: 22 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static_assertions = "1.1.0"
spin = { version = "0.9" }
lazy_static = { version = "1.5", features = ["spin_no_std"] }
crate_interface = "0.1"
linkme = "0.3"
ctor_bare = "0.1"

[build-dependencies]
bindgen ={ version = "0.69" }
2 changes: 1 addition & 1 deletion api/arceos_posix_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub mod pipe;
#[cfg(feature = "multitask")]
pub mod pthread;

#[ctor_bare::register_ctor]
#[cfg(feature = "fd")]
#[linkme::distributed_slice(axns::INIT_RESOURCE)]
fn init_stdio() {
use crate::imp::fd_ops::FD_TABLE;
use alloc::sync::Arc;
Expand Down
9 changes: 9 additions & 0 deletions modules/axhal/linker.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ SECTIONS
_erodata = .;
}

.init_array : ALIGN(4K) {
_sinit_array = .;
__init_array_start = .;
*(.init_array .init_array.*)
__init_array_end = .;
. = ALIGN(4K);
_einit_array = .;
}

.data : ALIGN(4K) {
_sdata = .;
*(.data.boot_page_table)
Expand Down
8 changes: 8 additions & 0 deletions modules/axhal/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ fn kernel_image_regions() -> impl Iterator<Item = MemRegion> {
flags: MemRegionFlags::RESERVED | MemRegionFlags::READ,
name: ".rodata",
},
MemRegion {
paddr: virt_to_phys((_sinit_array as usize).into()),
size: _einit_array as usize - _sinit_array as usize,
flags: MemRegionFlags::RESERVED | MemRegionFlags::READ,
name: ".init_array",
},
MemRegion {
paddr: virt_to_phys((_sdata as usize).into()),
size: _edata as usize - _sdata as usize,
Expand Down Expand Up @@ -153,6 +159,8 @@ extern "C" {
fn _etext();
fn _srodata();
fn _erodata();
fn _sinit_array();
fn _einit_array();
fn _sdata();
fn _edata();
fn _sbss();
Expand Down
3 changes: 3 additions & 0 deletions modules/axmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub fn new_kernel_aspace() -> AxResult<AddrSpace> {
axconfig::KERNEL_ASPACE_SIZE,
)?;
for r in axhal::mem::memory_regions() {
if r.size == 0 {
continue;
}
aspace.map_linear(phys_to_virt(r.paddr), r.paddr, r.size, r.flags.into())?;
}
Ok(aspace)
Expand Down
2 changes: 0 additions & 2 deletions modules/axns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ default = []
thread-local = []

[dependencies]
log = "0.4.21"
lazyinit = "0.2"
crate_interface = "0.1"
linkme = "0.3"

[dev-dependencies]
axns = { workspace = true, features = ["thread-local"] }
5 changes: 0 additions & 5 deletions modules/axns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern crate alloc;

use alloc::sync::Arc;
use core::{alloc::Layout, fmt, ops::Deref};
use linkme::distributed_slice;

use lazyinit::LazyInit;

Expand All @@ -32,10 +31,6 @@ extern "C" {
fn __stop_axns_resource();
}

/// A distributed slice that contains all method to initialize user-defined resources.
#[distributed_slice]
pub static INIT_RESOURCE: [fn()];

/// A namespace that contains all user-defined resources.
///
/// There are two types of namespaces:
Expand Down
1 change: 1 addition & 0 deletions modules/axruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ axns = { workspace = true, optional = true }
crate_interface = "0.1"
percpu = { version = "0.1", optional = true }
kernel_guard = { version = "0.1", optional = true }
ctor_bare = "0.1"

chrono = { version = "0.4.38", default-features = false }
5 changes: 1 addition & 4 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
info!("Primary CPU {} init OK.", cpu_id);
INITED_CPUS.fetch_add(1, Ordering::Relaxed);

#[cfg(feature = "alloc")]
for init in axns::INIT_RESOURCE.iter() {
init();
}
ctor_bare::call_ctors();

while !is_init_ok() {
core::hint::spin_loop();
Expand Down

0 comments on commit 5ccc2bf

Please sign in to comment.