axhal/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#![feature(doc_auto_cfg)]
32
33#[allow(unused_imports)]
34#[macro_use]
35extern crate log;
36
37#[allow(unused_imports)]
38#[macro_use]
39extern crate memory_addr;
40
41cfg_if::cfg_if! {
42 if #[cfg(feature = "myplat")] {
43 // link the custom platform crate in your application.
44 } else if #[cfg(target_os = "none")] {
45 #[cfg(target_arch = "x86_64")]
46 extern crate axplat_x86_pc;
47 #[cfg(target_arch = "aarch64")]
48 extern crate axplat_aarch64_qemu_virt;
49 #[cfg(target_arch = "riscv64")]
50 extern crate axplat_riscv64_qemu_virt;
51 #[cfg(target_arch = "loongarch64")]
52 extern crate axplat_loongarch64_qemu_virt;
53 } else {
54 // Link the dummy platform implementation to pass cargo test.
55 mod dummy;
56 }
57}
58
59pub mod mem;
60pub mod percpu;
61pub mod time;
62
63#[cfg(feature = "tls")]
64pub mod tls;
65
66#[cfg(feature = "irq")]
67pub mod irq;
68
69#[cfg(feature = "paging")]
70pub mod paging;
71
72/// Console input and output.
73pub mod console {
74 pub use axplat::console::{read_bytes, write_bytes};
75}
76
77/// CPU power management.
78pub mod power {
79 #[cfg(feature = "smp")]
80 pub use axplat::power::cpu_boot;
81 pub use axplat::power::system_off;
82}
83
84/// Trap handling.
85pub mod trap {
86 #[cfg(feature = "uspace")]
87 pub use axcpu::trap::SYSCALL;
88 pub use axcpu::trap::{IRQ, PAGE_FAULT};
89 pub use axcpu::trap::{PageFaultFlags, register_trap_handler};
90}
91
92/// CPU register states for context switching.
93///
94/// There are three types of context:
95///
96/// - [`TaskContext`][axcpu::TaskContext]: The context of a task.
97/// - [`TrapFrame`][axcpu::TrapFrame]: The context of an interrupt or an exception.
98/// - [`UspaceContext`][axcpu::uspace::UspaceContext]: The context for user/kernel mode switching.
99pub mod context {
100 #[cfg(feature = "uspace")]
101 pub use axcpu::uspace::UspaceContext;
102 pub use axcpu::{TaskContext, TrapFrame};
103}
104
105pub use axcpu::asm;
106pub use axplat::init::{init_early, init_later};
107
108#[cfg(feature = "smp")]
109pub use axplat::init::{init_early_secondary, init_later_secondary};
110
111/// Initializes CPU-local data structures for the primary core.
112///
113/// This function should be called as early as possible, as other initializations
114/// may acess the CPU-local data.
115pub fn init_percpu(cpu_id: usize) {
116 self::percpu::init_primary(cpu_id);
117}
118
119/// Initializes CPU-local data structures for secondary cores.
120///
121/// This function should be called as early as possible, as other initializations
122/// may acess the CPU-local data.
123#[cfg(feature = "smp")]
124pub fn init_percpu_secondary(cpu_id: usize) {
125 self::percpu::init_secondary(cpu_id);
126}