From mboxrd@z Thu Jan 1 00:00:00 1970 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.astral-os.org; s=s2026; t=1791298015; bh=CQEuad6rIOJU1Vi19j/dkngjUnpjPTjnUgH+Tlit+40=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id:List-Help: List-Owner:List-Post:List-Subscribe:List-Unsubscribe:From; b=ISgdamAOuRG1melb+tYgjYv9A8zrR47b+en4qaN1dSZ+22S8bpiPzSnGZPdIfHFkc UJwigTvQ+SSTOyMVMxzWGbfbAoKrWHFWh7EqZ6f/935SJZY4RQgnrLfx0VYYcVdygl gybpxwIBZGLTZkkRoAHoVRsYYgt2I5CIXsoQ+OFaqrh4Gs5ZcQn+fAWzvzB11TJxee sPx2MVzDSbJmqvnj7x25FgQqaf7TG6Hv/o96tFC1INurFbcZfllPXZGcy1xt0ciWaa nug5QVbIOfg0mvSXE59oqrErRqhpqtjmJbO32VyDMvQo9ZR2bd3ZBbd4mzGfe33eZY GuIBqHaMqe65A== Received: from vps-ace4d786.vps.ovh.us (localhost [127.0.0.1]) by mail.astral-os.org (Postfix) with ESMTP id 437F33ED43; Tue, 06 Oct 2026 14:46:55 +0000 (UTC) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=lists.astral-os.org; s=arc2026; t=1791298015; b=q1RxORHNKAlG/ip6e2nXdg90BcTkMdwGNIO1KudQG2afTY5HRwiAyc3mfL8m8Oo4fS8z/ RFHClO0ZPLEMqJJyDWoPb3DIF7+z+9vSksH1vgyBZk7kExpU1DdOYInaKujqbM2yGUYJcR6 pjRm20noB5dqqxNyw6FZC1QSoh3ZZd66Yfzh/GFFZUceNY9FtaBv/TQukfVZAVjCm2aVjjA b7Pcav9XDhTNdV+9B6rWXG+BxaYMXy5K1JPfDs6VyHWTCeULvj6uDwmfyneDPM1oQxDfy7R Ix2eMXgnYQPxtOY23ZPlN+7+60cdzduAHgOSU0xIRiBJ+g97TfP2ITZkTBRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.astral-os.org; s=arc2026; t=1791298015; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=WV80UlSEMJYDAYrjCWv21aaeEYelTfoPByrf+h4uSHc=; b=jAK1pJpcgMcfuABLxtmJWLdcPHXGGlybc/Uz3mOhP3/zrNA4KHlgjHsB4E5+YVOcg5TG0 hMQiNFHaihFvxRWK1VTqM2S9bok0n9FxaBeEEndkmG1Hyb136KHt2HH4V1OT4mW72k02xj0 f6PjDqb7lIywQi93B/c+BrIEmuqBd4kMadKWHRtwH6xWXgEHE2Gnidz/r35J3ZkDTCj0vWw 8OHGTX5jgGEb/SkG/NHJZ8HA+Yc8ym8J1577Jvhu1Ob/zJtmn5nwFGq71uG5uye34rKZs5/ wxR+zIBeqe8deo8DXS/pVD60Eu7NWb404RGMK0tfi9ckvkvM+ubIJL6QtDBw== ARC-Authentication-Results: i=1; lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none; dmarc=none Authentication-Results: lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none (Message is not ARC signed); dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=astral-os.org; s=mail2026; t=1791298013; bh=CQEuad6rIOJU1Vi19j/dkngjUnpjPTjnUgH+Tlit+40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Igy2l8HOKfPU4vIjO7thoiLcrS/TTSopx9wgAYsSUlmM0RxBElLOhoCXtDzKxBImd N4blIC2+j/KFA8BnH9mscNcjyepSh9JEjHEE5P33ti/jB8tSoOXIoIwqB9FDGzFaWz +W8lUomyc+9+w/6FVQ0+Dm/mmjtafCUe/joOZ7wfqsuagd2ckZJl+0yvMrBvZFwtub AWPFR+zg+Sff64J3YTpFHU+LQnHnizSh1jyI5Zo63ip8y0bP7r3elmSdHqvr8hv6hN 5WA+vM4vCJuYwUpnIqaQvcYqPTKQ7jSI32OTzRaVcKsglG97GjZihikyLMfQHElAR1 nuiJdr88XuKCw== Received: from LAPTOP-R01LEAQM.localdomain (unknown [189.112.151.75]) by mail.astral-os.org (Postfix) with ESMTPSA id 382033ED41; Tue, 06 Oct 2026 14:46:53 +0000 (UTC) From: Matheus To: astral-devel@lists.astral-os.org Subject: [PATCH 2/4] cpu: add irq depth counting Date: Tue, 6 Oct 2026 11:46:33 -0300 Message-ID: <20261006144635.141071-3-matheus@astral-os.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261006144635.141071-1-matheus@astral-os.org> References: <20261006144635.141071-1-matheus@astral-os.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 6ZJKQ4LFNXBCN76KR255LWFEQZ5T7DT2 X-Message-ID-Hash: 6ZJKQ4LFNXBCN76KR255LWFEQZ5T7DT2 X-MailFrom: matheus@astral-os.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Matheus X-Mailman-Version: 3.3.10 Precedence: list List-Id: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Matheus 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