aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-09-29 12:40:48 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-09-29 12:40:48 +0200
commit569f0e50663095e8ba7363cdab5421d0aeb9e6c3 (patch)
treecf7c5d890db69c1babd17f8d13e5b3e5e62fa7ed /kernel
parente259f732d9bf38e390588f07884c31ece95f5b7b (diff)
downloadkernel-569f0e50663095e8ba7363cdab5421d0aeb9e6c3.tar.xz
kernel-569f0e50663095e8ba7363cdab5421d0aeb9e6c3.zip
kapi/interrupts: add tests
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kapi/interrupts.stress.cpp65
-rw-r--r--kernel/kapi/interrupts.tests.cpp189
2 files changed, 254 insertions, 0 deletions
diff --git a/kernel/kapi/interrupts.stress.cpp b/kernel/kapi/interrupts.stress.cpp
new file mode 100644
index 00000000..d3baf7b8
--- /dev/null
+++ b/kernel/kapi/interrupts.stress.cpp
@@ -0,0 +1,65 @@
+#include <kapi/interrupts.hpp>
+
+#include <kernel/test_support/cpu.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <future>
+#include <mutex>
+
+SCENARIO("irq_lock remote unlock protection", "[kapi][interrupts]")
+{
+ GIVEN("a simple irq_lock")
+ {
+ auto lock = kapi::interrupts::irq_lock{};
+
+ WHEN("it is locked on the main core")
+ {
+ auto guard = std::lock_guard{lock};
+
+ THEN("unlocking it on a different core panics")
+ {
+ auto unlocker = [&] {
+ try
+ {
+ lock.unlock();
+ return false;
+ }
+ catch (kernel::tests::cpu::halt const &)
+ {
+ return true;
+ }
+ };
+
+ auto result = std::async(std::launch::async, unlocker);
+
+ REQUIRE(result.get());
+ }
+ }
+ }
+
+ GIVEN("A heap allocated irq_lock")
+ {
+ // We use plain new-delete here, since the destruction path of std::shared_ptr is fully noexcept.
+ auto lock = new kapi::interrupts::irq_lock<>{};
+
+ THEN("destroying it on a different core panics")
+ {
+ auto destroyer = [&] {
+ try
+ {
+ delete lock;
+ return false;
+ }
+ catch (kernel::tests::cpu::halt const &)
+ {
+ return true;
+ }
+ };
+
+ auto result = std::async(std::launch::async, destroyer);
+
+ REQUIRE(result.get());
+ }
+ }
+} \ No newline at end of file
diff --git a/kernel/kapi/interrupts.tests.cpp b/kernel/kapi/interrupts.tests.cpp
new file mode 100644
index 00000000..e87c6a00
--- /dev/null
+++ b/kernel/kapi/interrupts.tests.cpp
@@ -0,0 +1,189 @@
+#include <kapi/interrupts.hpp>
+
+#include <kapi/tracked_mutex.hpp>
+
+#include <kstd/mutex.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+SCENARIO("irq_lock semantics", "[kapi][interrupts]")
+{
+ GIVEN("a simple irq_lock")
+ {
+ auto lock = kapi::interrupts::irq_lock{};
+
+ AND_GIVEN("IRQs are currently disabled")
+ {
+ kapi::interrupts::enabled(false);
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ WHEN("locking the irq_lock")
+ {
+ lock.lock();
+
+ THEN("IRQs are still disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ AND_WHEN("unlocking the irq_lock")
+ {
+ lock.unlock();
+
+ THEN("IRQs are still disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+ }
+ }
+ }
+
+ THEN("the irq_lock is locked")
+ {
+ REQUIRE_FALSE(lock.try_lock());
+ lock.unlock();
+ }
+ }
+
+ THEN("the irq_lock can be locked")
+ {
+ REQUIRE(lock.try_lock());
+ lock.unlock();
+ }
+ }
+
+ AND_GIVEN("IRQs are currently enabled")
+ {
+ kapi::interrupts::enabled(true);
+ REQUIRE(kapi::interrupts::enabled());
+
+ WHEN("locking the irq_lock")
+ {
+ lock.lock();
+
+ THEN("IRQs are disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ AND_WHEN("unlocking the irq_lock")
+ {
+ lock.unlock();
+
+ THEN("IRQs are enabled again")
+ {
+ REQUIRE(kapi::interrupts::enabled());
+ }
+ }
+ }
+ }
+ }
+ }
+
+ GIVEN("a wrapping irq_lock")
+ {
+ auto mutex = kapi::tracked_mutex{};
+ auto lock = kapi::interrupts::irq_lock{mutex};
+
+ AND_GIVEN("IRQs are currently disabled")
+ {
+ kapi::interrupts::enabled(false);
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ WHEN("locking the irq_lock")
+ {
+ lock.lock();
+
+ THEN("IRQs are still disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ AND_WHEN("unlocking the irq_lock")
+ {
+ lock.unlock();
+
+ THEN("IRQs are still disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+ }
+
+ THEN("the inner mutex is unlocked")
+ {
+ REQUIRE(mutex.try_lock());
+ mutex.unlock();
+ }
+ }
+ }
+
+ THEN("the irq_lock is locked")
+ {
+ REQUIRE_FALSE(lock.try_lock());
+ lock.unlock();
+ }
+
+ THEN("the inner mutex is locked")
+ {
+ REQUIRE_FALSE(mutex.try_lock());
+ lock.unlock();
+ }
+ }
+
+ THEN("the irq_lock can be locked")
+ {
+ REQUIRE(lock.try_lock());
+ lock.unlock();
+ }
+ }
+
+ AND_GIVEN("IRQs are currently enabled")
+ {
+ kapi::interrupts::enabled(true);
+ REQUIRE(kapi::interrupts::enabled());
+
+ WHEN("locking the irq_lock")
+ {
+ lock.lock();
+
+ THEN("IRQs are disabled")
+ {
+ REQUIRE_FALSE(kapi::interrupts::enabled());
+
+ AND_WHEN("unlocking the irq_lock")
+ {
+ lock.unlock();
+
+ THEN("IRQs are enabled again")
+ {
+ REQUIRE(kapi::interrupts::enabled());
+ }
+
+ THEN("the inner mutex is unlocked")
+ {
+ REQUIRE(mutex.try_lock());
+ mutex.unlock();
+ }
+ }
+ }
+
+ THEN("the irq_lock is locked")
+ {
+ REQUIRE_FALSE(lock.try_lock());
+ lock.unlock();
+ }
+
+ THEN("the inner mutex is locked")
+ {
+ REQUIRE_FALSE(mutex.try_lock());
+ lock.unlock();
+ }
+ }
+ }
+
+ AND_GIVEN("the inner mutex is already locked")
+ {
+ auto guard = kstd::lock_guard{mutex};
+
+ THEN("the irq_lock cannot be locked")
+ {
+ REQUIRE_FALSE(lock.try_lock());
+ }
+ }
+ }
+} \ No newline at end of file