Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .agents/skills/rust-resource-guard-must-use/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ The following types in `kernel/` already have type-level `#[must_use]` attribute

| Types | Source | Guard behavior |
| --- | --- | --- |
| `DisableInterruptGuard`, `PlainDisableInterruptGuard` | [support.rs](../../../kernel/kernel/src/support.rs) | The former restores the saved local interrupt state in `Drop`; the latter re-enables local interrupts in `Drop`. |
| `DisableInterruptGuard` | [support.rs](../../../kernel/kernel/src/support.rs) | Drops one counted IRQ guard and restores the saved mask on the last release. |
| `PreemptGuard` | [thread/mod.rs](../../../kernel/kernel/src/thread/mod.rs) | Calls `enable_preempt()` on its thread in `Drop`. |
| `SpinLockGuard`, `SpinLockReadGuard` | [sync/spinlock.rs](../../../kernel/kernel/src/sync/spinlock.rs) | Hold the underlying lock guard and an optional interrupt guard. Cleanup occurs through their fields when dropped. |
| `SpinLockGuard`, `OwnedIrqSpinLockGuard`, `SpinLockReadGuard` | [sync/spinlock.rs](../../../kernel/kernel/src/sync/spinlock.rs) | Write guards release the payload before their preemption or IRQ protection. Read guards carry the protection selected at acquisition. |
| `RwLockReadGuard`, `RwLockWriteGuard`, `RwLockUpgradableGuard` | [tinyrwlock.rs](../../../kernel/infra/src/tinyrwlock.rs) | Update the reader count or clear lock state in `Drop`. |
| `__BlueOSTestDoneGuard` | [test_harness/src/lib.rs](../../../kernel/test_harness/src/lib.rs) | Generated inside a `quote!` block with `#[must_use]`; increments the completion counter and wakes waiters in `Drop`. |

Expand All @@ -34,9 +34,7 @@ Actual type definition from `kernel/kernel/src/support.rs`:
```rust
#[derive(Debug)]
#[must_use]
pub struct DisableInterruptGuard {
old: usize,
}
pub struct DisableInterruptGuard(NotThreadSafe);
```

Before delivery, check every resource guard generated or modified in the task. Confirm that the attribute is on the type definition, including types generated by macros.
7 changes: 3 additions & 4 deletions external/CMSIS-RTOS2_Validation/Source/Config/RV2_Config.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern int stdout_putchar (int ch);

typedef unsigned short IRQn_Type;
/* forward to rust */
extern void NVIC_SetPriority(IRQn_Type IRQn, char priority);
extern void configure_validation_irqs(IRQn_Type primary, IRQn_Type secondary);
extern void NVIC_EnableIRQ(IRQn_Type IRQn);
extern void NVIC_DisableIRQ(IRQn_Type IRQn);
extern uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn);
Expand Down Expand Up @@ -65,9 +65,8 @@ void TST_IRQ_HANDLER_B (void) {
void TS_Init (void) {

#if defined(__CORTEX_M)
/* Set primary and secondary interrupt priority */
NVIC_SetPriority ((IRQn_Type)TST_IRQ_NUM_A, 5U);
NVIC_SetPriority ((IRQn_Type)TST_IRQ_NUM_B, 4U);
/* Install the validation handlers and set their kernel-callable priorities. */
configure_validation_irqs (TST_IRQ_NUM_A, TST_IRQ_NUM_B);

/* Enable interrupts */
NVIC_EnableIRQ((IRQn_Type)TST_IRQ_NUM_A);
Expand Down
4 changes: 2 additions & 2 deletions kernel/adapter/cmsis/src/os2/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use blueos::{
sync::{
atomic_wait, atomic_wake,
event_flags::{EventFlags, EventFlagsMode},
ConstBarrier, SpinLockGuard,
ConstBarrier, OwnedIrqSpinLockGuard,
},
thread::{self, Entry, GlobalQueueVisitor, Thread},
time,
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Os2Thread {
to self.inner() {
pub fn priority(&self) -> ThreadPriority;
pub fn state(&self) -> Uint;
pub fn lock(&self) -> SpinLockGuard<'_, Thread>;
pub fn lock(&self) -> OwnedIrqSpinLockGuard<'_, Thread>;
pub fn stack_size(&self) -> usize;
pub fn stack_base(&self) -> usize;
pub fn saved_stack_usage(&self) -> usize;
Expand Down
3 changes: 2 additions & 1 deletion kernel/adapter/cmsis/src/validation.checker
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TOTAL-TIMEOUT: 25
// ASSERT-SUCC: cmsis_rv2 unittest finished
// ASSERT-FAIL: Oops:
// ASSERT-FAIL: Oops:
// ASSERT-FAIL: Test Result: FAILED
19 changes: 14 additions & 5 deletions kernel/adapter/cmsis/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,21 @@ mod tests {
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn NVIC_SetPriority(irq: u16, priority: u8) {
pub unsafe extern "C" fn configure_validation_irqs(primary: u16, secondary: u16) {
use arch::irq::{disable_irq, register_raw_isr, set_irq_priority, IrqNumber, Priority};
let primary = IrqNumber::new(primary);
let secondary = IrqNumber::new(secondary);
disable_irq(primary);
disable_irq(secondary);
// SAFETY: TS_Init installs the validation vectors before enabling either IRQ.
unsafe {
cortex_m::Peripherals::steal()
.NVIC
.set_priority(IrqNumber::new(irq), priority)
};
register_raw_isr(primary, uart::uart1rx_handler);
register_raw_isr(secondary, uart::uart1tx_handler);
}
// Both ISRs call RTOS APIs. Keep the secondary IRQ above the primary
// while allowing the kernel's IRQ mask to exclude either one.
set_irq_priority(primary, Priority::Low as u8);
set_irq_priority(secondary, Priority::Normal as u8);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn NVIC_EnableIRQ(irq: u16) {
Expand Down
40 changes: 40 additions & 0 deletions kernel/adapter/thread_metric/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,43 @@ group("run_tests") {
":run_tm_synchronization_processing_test",
]
}

# Real-kernel lock contract checks, separate from measured benchmarks.
foreach(probe,
[
"local_lock_contract_probe",
"local_lock_reject_yield_probe",
"local_lock_high_irq_probe",
]) {
build_rust(probe) {
crate_type = "bin"
sources = [ "src/local_lock_probe.rs" ]
configs += [ "//build/boards/${board}:kernel_config" ]
deps = board_deps + [
"//kernel/kernel:blueos",
"//kernel/rsrt",
"//libs/libc",
"//libs/librs",
]
linker_script = default_linker_script
if (probe == "local_lock_high_irq_probe") {
rustflags = [ "--cfg=probe_high_irq" ]
}
if (probe == "local_lock_reject_yield_probe") {
rustflags = [ "--cfg=probe_yield_in_lock" ]
}
}
}

build_rust("contended_semaphore_probe") {
crate_type = "bin"
sources = [ "src/contended_semaphore_probe.rs" ]
configs += [ "//build/boards/${board}:kernel_config" ]
deps = board_deps + [
"//kernel/kernel:blueos",
"//kernel/rsrt",
"//libs/libc",
"//libs/librs",
]
linker_script = default_linker_script
}
63 changes: 63 additions & 0 deletions kernel/adapter/thread_metric/src/contended_semaphore_probe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2026 vivo Mobile Communication Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_std]
#![no_main]

extern crate librs;
extern crate rsrt;

use blueos::{scheduler::InsertToEnd, sync::Semaphore, thread, time::Tick};
use core::sync::atomic::{AtomicBool, Ordering};

static PING: Semaphore = Semaphore::new();
static PONG: Semaphore = Semaphore::new();
static STOP: AtomicBool = AtomicBool::new(false);

#[unsafe(no_mangle)]
pub extern "C" fn main() -> i32 {
librs::stdio::init();
librs::pthread::register_my_posix_tcb();

assert!(PING.init(1));
assert!(PONG.init(0));

thread::spawn(|| loop {
assert!(PONG.acquire_notimeout::<InsertToEnd>());
if STOP.load(Ordering::Relaxed) {
break;
}
PING.release();
})
.unwrap();

for _ in 0..6 {
let end = Tick::after(Tick::from_millis(2000));
let mut count = 0u32;
while Tick::now().0 < end.0 {
assert!(PING.acquire_notimeout::<InsertToEnd>());
PONG.release();
count += 1;
}
unsafe {
libc::printf(c"Time Period Total: %u\n".as_ptr(), count);
}
}
STOP.store(true, Ordering::Relaxed);
PONG.release();

loop {
core::hint::spin_loop();
}
}
Loading
Loading