From: Matheus <matheus@astral-os.org>
To: astral-devel@lists.astral-os.org
Cc: Matheus <matheusuky@gmail.com>
Subject: [PATCH 2/4] cpu: add irq depth counting
Date: Tue, 6 Oct 2026 11:46:33 -0300 [thread overview]
Message-ID: <20261006144635.141071-3-matheus@astral-os.org> (raw)
In-Reply-To: <20261006144635.141071-1-matheus@astral-os.org>
From: Matheus <matheusuky@gmail.com>
Add a way of checking if a cpu is in the middle of handling an irq and
how deep it is into it. This will allow for future code to conditionally
run only in/outside interrupts.
---
kernel-src/arch/x86-64/cpu.c | 4 ++++
kernel-src/include/x86-64/arch/cpu.h | 2 ++
kernel-src/sys/interrupt.c | 13 ++++++++++++-
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/kernel-src/arch/x86-64/cpu.c b/kernel-src/arch/x86-64/cpu.c
index d8f84f07..33d8e05c 100644
--- a/kernel-src/arch/x86-64/cpu.c
+++ b/kernel-src/arch/x86-64/cpu.c
@@ -147,6 +147,8 @@ void arch_nmi_isr(context_t *ctx) {
uint64_t old_gsbase = rdmsr(MSR_GSBASE);
wrmsr(MSR_GSBASE, *(uint64_t *)(ctx + 1));
+ __atomic_fetch_add(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
+
bool handled = false;
#ifdef ENABLE_PROFILING
@@ -157,6 +159,8 @@ void arch_nmi_isr(context_t *ctx) {
if (!handled)
_panic("Unhandled NMI", ctx);
+ __atomic_fetch_sub(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
+
// restore the old gsbase
wrmsr(MSR_GSBASE, old_gsbase);
}
diff --git a/kernel-src/include/x86-64/arch/cpu.h b/kernel-src/include/x86-64/arch/cpu.h
index 2de21908..3dd5dda4 100644
--- a/kernel-src/include/x86-64/arch/cpu.h
+++ b/kernel-src/include/x86-64/arch/cpu.h
@@ -30,6 +30,8 @@ typedef struct cpu_t {
uint64_t scratch; // expected to be here by other code
+ unsigned long irq_depth; // expected to be here by other code
+
timekeeper_source_t *timekeeper_source;
timekeeper_source_info_t *timekeeper_source_info;
time_t timekeeper_source_base_ticks;
diff --git a/kernel-src/sys/interrupt.c b/kernel-src/sys/interrupt.c
index 9367d367..d64887c9 100644
--- a/kernel-src/sys/interrupt.c
+++ b/kernel-src/sys/interrupt.c
@@ -45,6 +45,7 @@ static void dopending(context_t *ctx, void *) {
arch_interrupt_disable();
bool entrystatus = current_cpu()->intstatus;
current_cpu()->intstatus = false;
+ __atomic_fetch_add(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
isr_t *list = NULL;
isr_t *iterator = current_cpu()->isrqueue;
@@ -87,18 +88,25 @@ static void dopending(context_t *ctx, void *) {
}
cleanup:
+ __atomic_fetch_sub(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
if (entrystatus) {
current_cpu()->intstatus = true;
arch_interrupt_enable();
}
}
-__attribute__((no_caller_saved_registers)) void sched_userspacecheck(context_t *context, bool syscall, uint64_t syscallret, uint64_t syscallerrno);
+__attribute__((no_caller_saved_registers)) void sched_userspacecheck(context_t *context,
+ bool syscall,
+ uint64_t syscallret,
+ uint64_t syscallerrno);
void interrupt_isr(int vec, context_t *ctx) {
isr_t *isr = ¤t_cpu()->isr[vec];
current_cpu()->intstatus = false;
+ if (isr->priority != IPL_IGNORE)
+ __atomic_fetch_add(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
+
if (isr->func == NULL) {
char oops[64];
snprintf(oops, 64, "Unregistered interrupt %d\n", vec);
@@ -116,6 +124,9 @@ void interrupt_isr(int vec, context_t *ctx) {
if (isr->eoi)
isr->eoi(isr);
+ if (isr->priority != IPL_IGNORE)
+ __atomic_fetch_sub(¤t_cpu()->irq_depth, 1, __ATOMIC_RELAXED);
+
if (ARCH_CONTEXT_ISUSER(ctx))
sched_userspacecheck(ctx, false, 0, 0);
--
2.43.0
next prev parent reply other threads:[~2026-10-06 14:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-06 14:46 [PATCH 0/4] kcov: initial implementation Matheus
2026-10-06 14:46 ` [PATCH 1/4] mm: properly fork shared file mappings Matheus
2026-10-06 14:46 ` Matheus [this message]
2026-10-06 14:46 ` [PATCH 3/4] mmu: move tlb shootdown to IPL_MAX Matheus
2026-10-06 14:46 ` [PATCH 4/4] kcov: initial implementation Matheus
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261006144635.141071-3-matheus@astral-os.org \
--to=matheus@astral-os.org \
--cc=astral-devel@lists.astral-os.org \
--cc=matheusuky@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox