Skip to main content

ax_hal/
lib.rs

1//! [ArceOS] hardware abstraction layer, provides unified APIs for
2//! platform-specific operations.
3//!
4//! It does the bootstrapping and initialization process for the specified
5//! platform, and provides useful operations on the hardware.
6//!
7//! Currently supported platforms (specify by cargo features):
8//!
9//! - `x86-pc`: Standard PC with x86_64 ISA.
10//! - `riscv64-qemu-virt`: QEMU virt machine with RISC-V ISA.
11//! - `aarch64-qemu-virt`: QEMU virt machine with AArch64 ISA.
12//! - `aarch64-raspi`: Raspberry Pi with AArch64 ISA.
13//! - `dummy`: If none of the above platform is selected, the dummy platform
14//!   will be used. In this platform, most of the operations are no-op or
15//!   `unimplemented!()`. This platform is mainly used for [cargo test].
16//!
17//! # Cargo Features
18//!
19//! - `smp`: Enable SMP (symmetric multiprocessing) support.
20//! - `fp-simd`: Enable floating-point and SIMD support.
21//! - `paging`: Enable page table manipulation.
22//! - `irq`: Enable interrupt handling support.
23//! - `tls`: Enable kernel space thread-local storage support.
24//! - `rtc`: Enable real-time clock support.
25//! - `uspace`: Enable user space support.
26//!
27//! [ArceOS]: https://github.com/arceos-org/arceos
28//! [cargo test]: https://doc.rust-lang.org/cargo/guide/tests.html
29
30#![no_std]
31
32#[allow(unused_imports)]
33#[macro_use]
34extern crate log;
35
36#[allow(unused_imports)]
37#[macro_use]
38extern crate ax_memory_addr;
39
40cfg_if::cfg_if! {
41    if #[cfg(feature = "myplat")] {
42        // link the custom platform crate in your application.
43    }
44    else if #[cfg(plat_dyn)] {
45        extern crate axplat_dyn;
46    }
47    else if #[cfg(all(target_os = "none", feature = "defplat"))] {
48        #[cfg(target_arch = "x86_64")]
49        extern crate ax_plat_x86_pc;
50        #[cfg(target_arch = "aarch64")]
51        extern crate ax_plat_aarch64_qemu_virt;
52        #[cfg(target_arch = "riscv64")]
53        extern crate ax_plat_riscv64_qemu_virt;
54        #[cfg(target_arch = "loongarch64")]
55        extern crate ax_plat_loongarch64_qemu_virt;
56    } else {
57        // Link the dummy platform implementation to pass cargo test.
58        mod dummy;
59    }
60}
61
62pub mod dtb;
63pub mod mem;
64pub mod percpu;
65pub mod time;
66
67#[cfg(feature = "tls")]
68pub mod tls;
69
70#[cfg(feature = "irq")]
71pub mod irq;
72
73#[cfg(feature = "paging")]
74pub mod paging;
75
76/// Console input and output.
77pub mod console {
78    #[cfg(feature = "irq")]
79    pub use ax_plat::console::irq_num;
80    pub use ax_plat::console::{read_bytes, write_bytes};
81}
82
83/// CPU power management.
84pub mod power {
85    #[cfg(feature = "smp")]
86    pub use ax_plat::power::cpu_boot;
87    pub use ax_plat::power::system_off;
88}
89
90/// Trap handling.
91pub mod trap {
92    pub use ax_cpu::trap::{PageFaultFlags, irq_handler, page_fault_handler};
93}
94
95/// CPU register states for context switching.
96///
97/// There are two types of context:
98///
99/// - [`TaskContext`][ax_cpu::TaskContext]: The context of a task.
100/// - [`TrapFrame`][ax_cpu::TrapFrame]: The context of an interrupt or an exception.
101pub mod context {
102    pub use ax_cpu::{TaskContext, TrapFrame};
103}
104
105pub use ax_cpu::asm;
106#[cfg(feature = "uspace")]
107pub use ax_cpu::uspace;
108pub use ax_plat::init::init_later;
109#[cfg(feature = "smp")]
110pub use ax_plat::init::{init_early_secondary, init_later_secondary};
111
112/// Initializes the platform and boot argument.
113/// This function should be called as early as possible.
114pub fn init_early(cpu_id: usize, arg: usize) {
115    dtb::init(arg);
116    ax_plat::init::init_early(cpu_id, arg);
117}
118
119/// Gets the number of CPUs running in the system.
120///
121/// When SMP is disabled, this function always returns 1.
122///
123/// When SMP is enabled, it's the smaller one between the platform-declared CPU
124/// number [`ax_plat::power::cpu_num`] and the configured maximum CPU number
125/// `ax_config::plat::MAX_CPU_NUM`.
126///
127/// This value is determined during the BSP initialization phase.
128pub fn cpu_num() -> usize {
129    #[cfg(feature = "smp")]
130    {
131        use spin::Lazy;
132
133        /// The number of CPUs in the system. Based on the number declared by the
134        /// platform crate and limited by the configured maximum CPU number.
135        static CPU_NUM: Lazy<usize> = Lazy::new(|| {
136            let max_cpu_num = ax_config::plat::MAX_CPU_NUM;
137            let plat_cpu_num = ax_plat::power::cpu_num();
138            let cpu_num = plat_cpu_num.min(max_cpu_num);
139
140            info!("CPU number: max = {max_cpu_num}, platform = {plat_cpu_num}, use = {cpu_num}");
141
142            if plat_cpu_num > max_cpu_num {
143                warn!(
144                    "platform declares more CPUs ({plat_cpu_num}) than configured max \
145                     ({max_cpu_num}), only the first {max_cpu_num} CPUs will be used."
146                );
147            }
148
149            cpu_num
150        });
151
152        *CPU_NUM
153    }
154    #[cfg(not(feature = "smp"))]
155    {
156        1
157    }
158}
159
160#[allow(unused_macros)]
161macro_rules! addr_of_sym {
162    ($e:ident) => {
163        $e as *const () as usize
164    };
165}
166pub(crate) use addr_of_sym;