-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathlib.rs
More file actions
201 lines (175 loc) · 6.19 KB
/
lib.rs
File metadata and controls
201 lines (175 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! [ArceOS] hardware abstraction layer, provides unified APIs for
//! platform-specific operations.
//!
//! It does the bootstrapping and initialization process for the specified
//! platform, and provides useful operations on the hardware.
//!
//! Currently supported platforms (specify by cargo features):
//!
//! - `x86-pc`: Standard PC with x86_64 ISA.
//! - `riscv64-qemu-virt`: QEMU virt machine with RISC-V ISA.
//! - `aarch64-qemu-virt`: QEMU virt machine with AArch64 ISA.
//! - `aarch64-raspi`: Raspberry Pi with AArch64 ISA.
//! - `dummy`: If none of the above platform is selected, the dummy platform
//! will be used. In this platform, most of the operations are no-op or
//! `unimplemented!()`. This platform is mainly used for [cargo test].
//!
//! # Cargo Features
//!
//! - `smp`: Enable SMP (symmetric multiprocessing) support.
//! - `fp-simd`: Enable floating-point and SIMD support.
//! - `paging`: Enable page table manipulation.
//! - `irq`: Enable interrupt handling support.
//! - `tls`: Enable kernel space thread-local storage support.
//! - `rtc`: Enable real-time clock support.
//! - `uspace`: Enable user space support.
//!
//! [ArceOS]: https://github.com/arceos-org/arceos
//! [cargo test]: https://doc.rust-lang.org/cargo/guide/tests.html
#![no_std]
#![feature(doc_auto_cfg)]
#[allow(unused_imports)]
#[macro_use]
extern crate log;
#[allow(unused_imports)]
#[macro_use]
extern crate memory_addr;
cfg_if::cfg_if! {
if #[cfg(feature = "myplat")] {
// link the custom platform crate in your application.
} else if #[cfg(target_os = "none")] {
#[cfg(target_arch = "x86_64")]
extern crate axplat_x86_pc;
#[cfg(target_arch = "aarch64")]
extern crate axplat_aarch64_qemu_virt;
#[cfg(target_arch = "riscv64")]
extern crate axplat_riscv64_qemu_virt;
#[cfg(target_arch = "loongarch64")]
extern crate axplat_loongarch64_qemu_virt;
} else {
// Link the dummy platform implementation to pass cargo test.
mod dummy;
}
}
pub mod mem;
pub mod percpu;
pub mod time;
#[cfg(feature = "tls")]
pub mod tls;
#[cfg(feature = "irq")]
pub mod irq;
#[cfg(feature = "paging")]
pub mod paging;
/// Console input and output.
pub mod console {
pub use axplat::console::{read_bytes, write_bytes};
}
/// CPU power management.
pub mod power {
#[cfg(feature = "smp")]
pub use axplat::power::cpu_boot;
pub use axplat::power::system_off;
}
/// Trap handling.
pub mod trap {
#[cfg(feature = "uspace")]
pub use axcpu::trap::SYSCALL;
pub use axcpu::trap::{IRQ, PAGE_FAULT};
pub use axcpu::trap::{PageFaultFlags, register_trap_handler};
}
/// CPU register states for context switching.
///
/// There are three types of context:
///
/// - [`TaskContext`][axcpu::TaskContext]: The context of a task.
/// - [`TrapFrame`][axcpu::TrapFrame]: The context of an interrupt or an exception.
/// - [`UspaceContext`][axcpu::uspace::UspaceContext]: The context for user/kernel mode switching.
pub mod context {
#[cfg(feature = "uspace")]
pub use axcpu::uspace::UspaceContext;
pub use axcpu::{TaskContext, TrapFrame};
}
pub use axcpu::asm;
pub use axplat::init::init_later;
#[cfg(feature = "smp")]
pub use axplat::init::{init_early_secondary, init_later_secondary};
#[cfg(feature = "smp")]
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
/// Initializes CPU-local data structures for the primary core.
///
/// This function should be called as early as possible, as other initializations
/// may acess the CPU-local data.
pub fn init_percpu(cpu_id: usize) {
self::percpu::init_primary(cpu_id);
}
/// Initializes CPU-local data structures for secondary cores.
///
/// This function should be called as early as possible, as other initializations
/// may acess the CPU-local data.
#[cfg(feature = "smp")]
pub fn init_percpu_secondary(cpu_id: usize) {
self::percpu::init_secondary(cpu_id);
}
use lazyinit::LazyInit;
static BOOT_ARG: LazyInit<usize> = LazyInit::new();
/// Initializes the platform and boot argument.
/// This function should be called as early as possible.
pub fn init_early(cpu_id: usize, arg: usize) {
BOOT_ARG.init_once(arg);
axplat::init::init_early(cpu_id, arg);
}
/// Returns the boot argument.
/// This is typically the device tree blob address passed from the bootloader.
pub fn get_bootarg() -> usize {
*BOOT_ARG
}
/// The number of CPUs in the system. Based on the number declared by the
/// platform crate and limited by the configured maximum CPU number.
#[cfg(feature = "smp")]
static CPU_NUM: AtomicUsize = AtomicUsize::new(1);
/// Gets the number of CPUs running in the system.
///
/// When SMP is disabled, this function always returns 1.
///
/// When SMP is enabled, It's the smaller one between the platform-declared CPU
/// number [`axplat::power::cpu_num`] and the configured maximum CPU number
/// `axconfig::plat::MAX_CPU_NUM`.
///
/// This value is determined during the BSP initialization phase.
pub fn cpu_num() -> usize {
#[cfg(feature = "smp")]
{
// Relaxed is used here for best performance, as this value is only set
// once during initialization and never changed afterwards.
//
// The BSP will always see the correct value because `CPU_NUM` is set by
// itself.
//
// All APs will see the correct value because it is written with
// `Ordering::Release` and read with `Ordering::Acquire`, ensuring
// memory visibility.
CPU_NUM.load(Ordering::Acquire)
}
#[cfg(not(feature = "smp"))]
{
1
}
}
/// Initializes the CPU number information.
pub fn init_cpu_num() {
#[cfg(feature = "smp")]
{
let plat_cpu_num = axplat::power::cpu_num();
let max_cpu_num = axconfig::plat::MAX_CPU_NUM;
let cpu_num = plat_cpu_num.min(max_cpu_num);
info!("CPU number: max = {max_cpu_num}, platform = {plat_cpu_num}, use = {cpu_num}",);
if plat_cpu_num > max_cpu_num {
warn!(
"platform declares more CPUs ({plat_cpu_num}) than configured max ({max_cpu_num}), \
only the first {max_cpu_num} CPUs will be used."
);
}
CPU_NUM.store(cpu_num, Ordering::Release);
}
// No-op for non-SMP builds.
}