Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit bee485a

Browse files
authored
Merge pull request #11 from os-checker/fix/all-features_riscv32imafc-unknown-none-elf
fix: missing constants, AtomicU64 and nightly build for --all-features on riscv32 target
2 parents 43d6d1b + 609a297 commit bee485a

File tree

4 files changed

+105
-71
lines changed

4 files changed

+105
-71
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Build & Check CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
ci:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
rust-toolchain: [nightly, nightly-2024-12-04]
12+
targets: [x86_64-unknown-linux-gnu, x86_64-unknown-none, riscv64gc-unknown-none-elf, riscv32imafc-unknown-none-elf, aarch64-unknown-none-softfloat]
13+
features: ["", "--all-features"]
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: dtolnay/rust-toolchain@nightly
17+
with:
18+
toolchain: ${{ matrix.rust-toolchain }}
19+
components: rust-src, clippy, rustfmt
20+
targets: ${{ matrix.targets }}
21+
- name: Check rust version
22+
run: rustc --version --verbose
23+
- name: Check code format
24+
run: cargo fmt --all -- --check
25+
- name: Clippy
26+
run: cargo clippy --target ${{ matrix.targets }} ${{ matrix.features }}
27+
- name: Build
28+
run: cargo build --target ${{ matrix.targets }} ${{ matrix.features }}
29+
- name: Unit test
30+
if: ${{ matrix.targets == 'x86_64-unknown-linux-gnu' }}
31+
run: cargo test --target ${{ matrix.targets }} ${{ matrix.features }} -- --nocapture

src/arch/riscv.rs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,40 @@ impl TaskContext {
6161
}
6262

6363
#[cfg(target_arch = "riscv32")]
64-
core::arch::global_asm!(
65-
r"
64+
macro_rules! STR_LDR {
65+
() => {
66+
r"
6667
.ifndef XLENB
6768
.equ XLENB, 4
6869
69-
.macro LDR rd, rs, off
70-
lw \rd, \off*XLENB(\rs)
71-
.endm
7270
.macro STR rs2, rs1, off
7371
sw \rs2, \off*XLENB(\rs1)
7472
.endm
73+
.macro LDR rd, rs, off
74+
lw \rd, \off*XLENB(\rs)
75+
.endm
7576
7677
.endif"
77-
);
78+
};
79+
}
7880

7981
#[cfg(target_arch = "riscv64")]
80-
core::arch::global_asm!(
81-
r"
82+
macro_rules! STR_LDR {
83+
() => {
84+
r"
8285
.ifndef XLENB
8386
.equ XLENB, 8
8487
85-
.macro LDR rd, rs, off
86-
ld \rd, \off*XLENB(\rs)
87-
.endm
8888
.macro STR rs2, rs1, off
8989
sd \rs2, \off*XLENB(\rs1)
9090
.endm
91+
.macro LDR rd, rs, off
92+
ld \rd, \off*XLENB(\rs)
93+
.endm
9194
92-
.endif",
93-
);
95+
.endif"
96+
};
97+
}
9498

9599
#[naked]
96100
/// Switches the context from the current task to the next task.
@@ -101,40 +105,41 @@ core::arch::global_asm!(
101105
pub unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: &TaskContext) {
102106
unsafe {
103107
naked_asm!(
108+
STR_LDR!(),
104109
"
105-
// save old context (callee-saved registers)
106-
STR ra, a0, 0
107-
STR sp, a0, 1
108-
STR s0, a0, 2
109-
STR s1, a0, 3
110-
STR s2, a0, 4
111-
STR s3, a0, 5
112-
STR s4, a0, 6
113-
STR s5, a0, 7
114-
STR s6, a0, 8
115-
STR s7, a0, 9
116-
STR s8, a0, 10
117-
STR s9, a0, 11
118-
STR s10, a0, 12
119-
STR s11, a0, 13
120-
121-
// restore new context
122-
LDR s11, a1, 13
123-
LDR s10, a1, 12
124-
LDR s9, a1, 11
125-
LDR s8, a1, 10
126-
LDR s7, a1, 9
127-
LDR s6, a1, 8
128-
LDR s5, a1, 7
129-
LDR s4, a1, 6
130-
LDR s3, a1, 5
131-
LDR s2, a1, 4
132-
LDR s1, a1, 3
133-
LDR s0, a1, 2
134-
LDR sp, a1, 1
135-
LDR ra, a1, 0
136-
137-
ret",
110+
// save old context (callee-saved registers)
111+
STR ra, a0, 0
112+
STR sp, a0, 1
113+
STR s0, a0, 2
114+
STR s1, a0, 3
115+
STR s2, a0, 4
116+
STR s3, a0, 5
117+
STR s4, a0, 6
118+
STR s5, a0, 7
119+
STR s6, a0, 8
120+
STR s7, a0, 9
121+
STR s8, a0, 10
122+
STR s9, a0, 11
123+
STR s10, a0, 12
124+
STR s11, a0, 13
125+
126+
// restore new context
127+
LDR s11, a1, 13
128+
LDR s10, a1, 12
129+
LDR s9, a1, 11
130+
LDR s8, a1, 10
131+
LDR s7, a1, 9
132+
LDR s6, a1, 8
133+
LDR s5, a1, 7
134+
LDR s4, a1, 6
135+
LDR s3, a1, 5
136+
LDR s2, a1, 4
137+
LDR s1, a1, 3
138+
LDR s0, a1, 2
139+
LDR sp, a1, 1
140+
LDR ra, a1, 0
141+
142+
ret",
138143
)
139144
}
140145
}

src/task.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
#[cfg(feature = "tls")]
22
use crate::tls::TlsArea;
33

4-
use crate::{TaskStack, TimeStat, arch::TaskContext};
54
extern crate alloc;
5+
use crate::{TaskStack, TimeStat, arch::TaskContext};
66
use alloc::{boxed::Box, string::String};
7-
8-
#[allow(unused_imports)]
97
use core::{
108
cell::UnsafeCell,
119
fmt,
12-
sync::atomic::{AtomicBool, AtomicI32, AtomicU8, AtomicU64, AtomicUsize, Ordering},
10+
sync::atomic::{AtomicBool, AtomicI32, AtomicUsize, Ordering},
1311
};
1412
use memory_addr::{VirtAddr, align_up_4k};
1513

1614
/// A unique identifier for a thread.
1715
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
18-
pub struct TaskId(u64);
16+
pub struct TaskId(usize);
1917
pub const MAX_RT_PRIO: usize = 99;
2018

21-
static ID_COUNTER: AtomicU64 = AtomicU64::new(1);
19+
static ID_COUNTER: AtomicUsize = AtomicUsize::new(1);
2220
impl TaskId {
2321
/// Create a new task ID.
2422
pub fn new() -> Self {
@@ -27,7 +25,7 @@ impl TaskId {
2725

2826
/// Convert the task ID to a `u64`.
2927
pub const fn as_u64(&self) -> u64 {
30-
self.0
28+
self.0 as u64
3129
}
3230

3331
#[cfg(feature = "monolithic")]
@@ -152,7 +150,7 @@ pub struct TaskInner {
152150
ctx: UnsafeCell<TaskContext>,
153151

154152
#[cfg(feature = "monolithic")]
155-
process_id: AtomicU64,
153+
process_id: AtomicUsize,
156154

157155
#[cfg(feature = "monolithic")]
158156
/// 是否是所属进程下的主线程
@@ -166,20 +164,20 @@ pub struct TaskInner {
166164
pub page_table_token: UnsafeCell<usize>,
167165

168166
#[cfg(feature = "monolithic")]
169-
set_child_tid: AtomicU64,
167+
set_child_tid: AtomicUsize,
170168

171169
#[cfg(feature = "monolithic")]
172-
clear_child_tid: AtomicU64,
170+
clear_child_tid: AtomicUsize,
173171

174172
/// 时间统计, 无论是否为宏内核架构都可能被使用到
175173
#[allow(unused)]
176174
time: UnsafeCell<TimeStat>,
177175

178-
#[cfg(feature = "monolithic")]
179176
/// TODO: to support the sched_setaffinity
180177
///
181178
/// TODO: move to the upper layer
182-
pub cpu_set: AtomicU64,
179+
#[cfg(feature = "monolithic")]
180+
pub cpu_set: AtomicUsize,
183181

184182
#[cfg(feature = "monolithic")]
185183
/// The scheduler status of the task, which defines the scheduling policy and priority
@@ -258,7 +256,7 @@ impl TaskInner {
258256

259257
t.entry = Some(Box::into_raw(Box::new(entry)));
260258

261-
t.process_id.store(process_id, Ordering::Release);
259+
t.process_id.store(process_id as _, Ordering::Release);
262260

263261
t.page_table_token = UnsafeCell::new(page_table_token);
264262

@@ -398,17 +396,17 @@ impl TaskInner {
398396
impl TaskInner {
399397
/// store the child thread ID at the location pointed to by child_tid in clone args
400398
pub fn set_child_tid(&self, tid: usize) {
401-
self.set_child_tid.store(tid as u64, Ordering::Release)
399+
self.set_child_tid.store(tid, Ordering::Release)
402400
}
403401

404402
/// clear (zero) the child thread ID at the location pointed to by child_tid in clone args
405403
pub fn set_clear_child_tid(&self, tid: usize) {
406-
self.clear_child_tid.store(tid as u64, Ordering::Release)
404+
self.clear_child_tid.store(tid, Ordering::Release)
407405
}
408406

409407
/// get the pointer to the child thread ID
410408
pub fn get_clear_child_tid(&self) -> usize {
411-
self.clear_child_tid.load(Ordering::Acquire) as usize
409+
self.clear_child_tid.load(Ordering::Acquire)
412410
}
413411

414412
#[inline]
@@ -428,13 +426,13 @@ impl TaskInner {
428426
#[inline]
429427
/// get the process ID of the task
430428
pub fn get_process_id(&self) -> u64 {
431-
self.process_id.load(Ordering::Acquire)
429+
self.process_id.load(Ordering::Acquire) as _
432430
}
433431

434432
#[inline]
435433
/// set the process ID of the task
436434
pub fn set_process_id(&self, process_id: u64) {
437-
self.process_id.store(process_id, Ordering::Release);
435+
self.process_id.store(process_id as _, Ordering::Release);
438436
}
439437

440438
// /// 获取内核栈的第一个trap上下文
@@ -464,12 +462,12 @@ impl TaskInner {
464462
set_size * 4
465463
};
466464
let now_mask = mask & 1 << ((len) - 1);
467-
self.cpu_set.store(now_mask as u64, Ordering::Release)
465+
self.cpu_set.store(now_mask as _, Ordering::Release)
468466
}
469467

470468
/// to get the CPU set
471469
pub fn get_cpu_set(&self) -> usize {
472-
self.cpu_set.load(Ordering::Acquire) as usize
470+
self.cpu_set.load(Ordering::Acquire)
473471
}
474472

475473
/// set the scheduling policy and priority
@@ -533,7 +531,7 @@ impl TaskInner {
533531
time: UnsafeCell::new(TimeStat::new()),
534532

535533
#[cfg(feature = "monolithic")]
536-
process_id: AtomicU64::new(0),
534+
process_id: AtomicUsize::new(0),
537535

538536
#[cfg(feature = "monolithic")]
539537
is_leader: AtomicBool::new(false),
@@ -542,14 +540,14 @@ impl TaskInner {
542540
page_table_token: UnsafeCell::new(0),
543541

544542
#[cfg(feature = "monolithic")]
545-
set_child_tid: AtomicU64::new(0),
543+
set_child_tid: AtomicUsize::new(0),
546544

547545
#[cfg(feature = "monolithic")]
548-
clear_child_tid: AtomicU64::new(0),
546+
clear_child_tid: AtomicUsize::new(0),
549547

550548
#[cfg(feature = "monolithic")]
551549
// 一开始默认都可以运行在每个CPU上
552-
cpu_set: AtomicU64::new(0),
550+
cpu_set: AtomicUsize::new(0),
553551

554552
#[cfg(feature = "monolithic")]
555553
sched_status: UnsafeCell::new(SchedStatus {

src/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cfg_if::cfg_if! {
6565
} else if #[cfg(target_arch = "aarch64")] {
6666
const TCB_SIZE: usize = 0;
6767
const GAP_ABOVE_TP: usize = 16;
68-
} else if #[cfg(target_arch = "riscv64")] {
68+
} else if #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))] {
6969
const TCB_SIZE: usize = 0;
7070
const GAP_ABOVE_TP: usize = 0;
7171
}

0 commit comments

Comments
 (0)