Skip to content

m68k kernel: fix task-list race in Switch() and narrow the Paula INTENA restore - #164

Open
RMortenB wants to merge 2 commits into
ApolloTeam-dev:masterfrom
RMortenB:fix-switch-ipl-and-intena-restore
Open

RMortenB wants to merge 2 commits into
ApolloTeam-dev:masterfrom
RMortenB:fix-switch-ipl-and-intena-restore

Conversation

@RMortenB

@RMortenB RMortenB commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This PR has two independent fixes, one per commit.

1. Tasks lost from the exec lists (scheduler race)

core_Switch() Remove()s/Enqueue()s ThisTask on TaskReady/TaskWait. Only Wait() gets there with a Disable() held. All Schedule() paths (Permit(), Enable(), Reschedule() via KrnSchedule → Supervisor(), and ExitIntr preemption) run it with INTEN set, at IPL 0 or at the level of the exiting interrupt. If an interrupt calls Signal() during the Enqueue(), it edits the same list, and a task drops off both lists. That task never runs again, and anything that walks the lists can crash later. This fits the reports of a game port losing a task and of ApolloOS dying on its own after days.

Fix: ori.w #0x0700,%sr in cpu_Switch() before core_Switch(), the same thing cpu_Dispatch() already does before core_Dispatch(). The RTE into the next task restores its SR.

The code is unchanged since early AROS m68k, so R9.5 is affected too. AROS has the equivalent fix for its rewritten Switch() in d0b5fa16ab.

2. The Paula INTENA restore from #158 fires too often

ec12438 (#158) writes INTENA = SETCLR|INTEN on every Paula IRQ exit. When an interrupt lands just as the interrupted code enters Disable() (INTENA written, IDNestCnt not yet incremented), that write turns interrupts back on inside a critical section. On RC06 this showed up as ApolloRadio losing networking after hours; a ROM with #158 reverted ran overnight fine.

This change restores the bit only if the interrupted context had a Disable() outstanding (IDNestCnt >= 0 at entry) and INTEN is actually clear at exit. The #742 demo case still works.

Testing (Vampire V4)

  • Stress test: a CIA-B timer interrupt Signal()s six pri-0 waiters at 5 kHz, while a pri-2 echo task is pinged alternately with Signal() and Forbid()/Signal()/Permit(), and the task lists are checked under Disable().
    • RC06, with or without fix 2: the board hangs within seconds.
    • With both fixes: 3 runs (150 s, ~1.6M switches, ~730k IRQ-level Signal()s) pass, and the board stays healthy.
  • A #742-style test (Forbid()+Disable(), set INTENA by hand, AddIntServer(VERTB), wait for VBLs) passes.
  • Soaked as the everyday ROM on a V4.

Note for #161 / #162

These PRs port the newer AROS scheduler, so commit 1 will conflict with them. With them the mask has to go in front of m68k_SwitchTail() (as AROS d0b5fa16ab does). Their new Switch.S drops to IPL 0 before requeueing, which widens the window. Commit 2 merges cleanly with #161.

🤖 Generated with Claude Code

RMortenB and others added 2 commits November 18, 2026 15:19
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 <noreply@anthropic.com>
ec12438 (ApolloTeam-dev#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 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Maintainer validation is needed for m68k scheduler atomicity and Paula interrupt restoration during nested IRQs and Disable() transitions, which the reported V4 tests cannot establish across supported hardware.

No open issues 🎉

What changed in this PR

Addresses two interrupt-related reliability issues in the m68k kernel: task-list corruption during scheduling and unintended interrupt re-enabling when leaving a Paula interrupt handler.

Changes:

  • Masks CPU interrupts before core_Switch() modifies scheduler task lists.
  • Restores Paula’s master interrupt bit only when the interrupted context had an outstanding Disable() and the bit is clear at exit.
File Description
arch/​m68k-amiga/​kernel/​amiga_irq.c Records the entry disable state and narrows the INTENA restore condition.
arch/​m68k-all/​kernel/​kernel_cpu.c Raises the CPU interrupt mask before scheduler list updates.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants