From 18bdc6fced0f802416e42b0a04591a42acb3d7f6 Mon Sep 17 00:00:00 2001 From: Morten Brenna Date: Wed, 18 Nov 2026 15:19:27 +0100 Subject: [PATCH 1/2] m68k kernel: mask interrupts while core_Switch() requeues ThisTask core_Switch() Remove()s/Enqueue()s the outgoing task on TaskReady or TaskWait. Only Wait() reaches it with a Disable() held. Every Schedule() path (Permit(), Enable(), Reschedule() via KrnSchedule/Supervisor(), and ExitIntr preemption) arrives with INTEN set, at IPL 0 or at the level of the exiting interrupt. An interrupt that calls Signal() in the middle of the Enqueue() edits the same list and one task drops off both lists: it never runs again, and anything that walks the lists can crash later. Raise the IPL to 7 before core_Switch(), as cpu_Dispatch() already does before core_Dispatch(); the RTE to the next task restores its SR. Reproduced on a V4 with a stress test (CIA-B timer Signal()s six pri-0 waiters at 5 kHz while a pri-2 echo task is pinged with Signal() and Forbid()/Signal()/Permit(), and the exec lists are checked under Disable()): R9.6-RC06 hangs the board within seconds; with this change it passes 3 runs (150 s, ~1.6M task switches, ~730k interrupt-level Signal()s) and the board stays healthy. The code is unchanged since early AROS m68k, so R9.5 is affected too. AROS has the same fix for its newer Switch() in d0b5fa16ab. Co-Authored-By: Claude Opus 5.5 --- arch/m68k-all/kernel/kernel_cpu.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/m68k-all/kernel/kernel_cpu.c b/arch/m68k-all/kernel/kernel_cpu.c index cb045746ff2..8a6b9c97895 100644 --- a/arch/m68k-all/kernel/kernel_cpu.c +++ b/arch/m68k-all/kernel/kernel_cpu.c @@ -62,6 +62,19 @@ void cpu_Switch(regs_t *regs) /* Update tc_SPReg */ task->tc_SPReg = (APTR)regs->a[7]; + /* + * core_Switch() Remove()s/Enqueue()s ThisTask on TaskReady/TaskWait. + * Only Wait() gets here with a Disable() held. The Schedule() path + * (Permit(), Enable(), Reschedule(), ExitIntr preemption) arrives + * with INTEN set and the CPU at IPL 0 (Supervisor() does not raise + * it) or at the level of the interrupt that is exiting, so a higher + * level interrupt calling Signal() could edit the same list half-way + * through the Enqueue() and lose a task from both lists. Mask + * interrupts from here on; cpu_Dispatch() does the same before + * core_Dispatch() and the RTE to the new task restores its SR. + */ + asm volatile ("ori.w #0x0700,%%sr\n" : : : "memory"); + core_Switch(); } From 6d51ee88bc834a27fabb8631c0b70d7ae5319025 Mon Sep 17 00:00:00 2001 From: Morten Brenna Date: Wed, 18 Nov 2026 15:19:27 +0100 Subject: [PATCH 2/2] amiga kernel: only restore INTEN on Paula IRQ exit when it was lost ec124385f2 (#158) writes INTENA = SETCLR|INTEN on every Paula interrupt exit, so that code which runs Disable() and then turns on the hardware itself (issue #742 demos) gets interrupts back. Done unconditionally, it also fires when the interrupt lands just as the interrupted code enters Disable() (INTENA written, IDNestCnt not yet raised), which turns interrupts back on inside a critical section. On RC06 this showed up as ApolloRadio losing networking after hours; a revert ROM ran overnight without the problem. Restore the bit only when the interrupted context had a Disable() outstanding (IDNestCnt >= 0 at entry) and INTEN is actually clear at exit. That still covers the #742 case (checked on a V4 with a test that does Forbid()+Disable(), sets INTENA, adds a VBL server and waits for VBLs) and no longer touches INTENA in any other case. Co-Authored-By: Claude Opus 5.5 --- arch/m68k-amiga/kernel/amiga_irq.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/m68k-amiga/kernel/amiga_irq.c b/arch/m68k-amiga/kernel/amiga_irq.c index 801111acb93..3257c97475b 100644 --- a/arch/m68k-amiga/kernel/amiga_irq.c +++ b/arch/m68k-amiga/kernel/amiga_irq.c @@ -128,6 +128,8 @@ static inline UWORD custom_r(ULONG reg) UWORD intenar = custom_r(INTENAR); \ if (!(intenar & INTF_INTEN)) \ return TRUE; \ + /* Interrupted context had a Disable() outstanding (see EXIT) */ \ + const BOOL in_disable = (SysBase->IDNestCnt >= 0); \ UWORD mask = intenar & custom_r(INTREQR) & (irq_mask); \ do { @@ -148,8 +150,13 @@ static inline UWORD custom_r(ULONG reg) * re-enabled the hardware itself (demos do: Disable(), set INTENA, \ * AddIntServer(), spin on a VBL counter), and its Enable() then leaves \ * INTEN clear for good. Interrupt handling must be transparent to the \ - * interrupted code, so put the bit back */ \ - custom_w(INTENA, INTF_SETCLR | INTF_INTEN); \ + * interrupted code, so put the bit back - but only in that case. \ + * Doing it unconditionally also fires when the interrupt lands just \ + * as the interrupted code enters Disable() (INTENA written, IDNestCnt \ + * not yet raised), turning interrupts back on inside its critical \ + * section. */ \ + if (in_disable && !(custom_r(INTENAR) & INTF_INTEN)) \ + custom_w(INTENA, INTF_SETCLR | INTF_INTEN); \ /* Call Exec/ExitIntr */ \ return TRUE;