Skip to content

Make reset reliable (IRQ-safe handoff, robust IOP reboot, SDK-owned stdio) - #70

Merged
fjtrujy merged 10 commits into
ps2dev:masterfrom
kaisa847:fix/reset-robust
Nov 24, 2026
Merged

fjtrujy merged 10 commits into
ps2dev:masterfrom
kaisa847:fix/reset-robust

Conversation

@kaisa847

@kaisa847 kaisa847 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

reset could leave ps2link unreachable after running an EE program, particularly when an SDL2 program used audio, pad input and gsKit. A boot without IPCONFIG.DAT could also crash on the first printf() after ps2link restarted itself, because newlib-nano's stdio state survived in .data while its FILE locks in .bss were cleared.

This is the single consolidated reset-reliability PR. The additional fixes were integrated here from the former alternative #71; there is no need to merge #71 separately. The original eight commits by @kaisa847 are preserved, followed by the consolidation commit.

Changes

  • Raise the EE command-thread priority and, on RESET, demote non-base EE threads so even high-priority audio/rendering loops cannot starve the command thread.
  • Identify pre-application EE threads by entry, stack and GP in addition to thread ID, reducing the risk of preserving a reused ID.
  • Terminate user-created EE threads and quiesce GIF/VIF1 DMA before restarting.
  • Correct the IOP-boot wait, check SifIopReset() submission, bound retries and BOOTEND waits, and continue into the ps2link startup recovery path if the IOP did not reboot successfully.
  • Forward reset to the EE before tearing down IOP services; keep host: and tty mounted until the reboot itself instead of deleting them on a failed submission.
  • Move both network and physical-button reset requests into a normal high-priority IOP worker. The hardware interrupt callback only wakes that worker; it never performs blocking DMA retries or sleeps.
  • Isolate reset and IOP-exception SIF DMA staging buffers and account for the extra command word in the payload length; never sleep/retry from exception context.
  • Avoid hardcoding newlib-nano's private struct _reent layout in ps2link. The stdio reset is now owned by merged ps2sdk #969, whose __locks_init() runs on ps2link's startup after ExecPS2().

Validation

Original implementation, tested by @kaisa847 on PSX DESR-5100 hardware:

Build Workload Observation
Baseline master ATA/printf test, image test, ps2gl 3D demo Reset worked
Baseline master SDL2 demo with audio/pad/gsKit Reset hung; power cycle needed
Baseline master First boot without IPCONFIG.DAT printf() crashed after self-restart
Original PR commits 1–7 SDL2 demo Reset still hung
Original PR commits 1–8 SDL2 demo twice; ATA/printf; image and 3D demos; boot without IPCONFIG.DAT All recovered in about 4 seconds

Consolidated code, verified so far:

Still required before marking ready: repeat runtime tests for the new consolidated branch on real hardware. In particular, use an updated/rebuilt ps2sdk libcglue.a that contains ps2sdk #969 (merge cf9557d or newer). The installed SDK used by the earlier local tests predates this fix, so those builds alone do not prove the newlib-nano restart.

Real-hardware regression cases: no-IPCONFIG.DAT cold boot and subsequent printf(), at least ten sequential application reset/relaunch cycles with an SDL2 audio workload, the physical reset button in both configurations, and congested/failed SIF DMA submission. Confirm that ps2client can reconnect to ps2link and execute another ELF after each recovery, not merely that ICMP ping succeeds.

Related

kaisa847 and others added 8 commits November 13, 2026 15:52
The command thread ran at priority 20. The EE kernel schedules strictly by
priority, so a thread of the loaded program that never blocks and runs at a
numerically lower priority starves it, and reset (and every other command) is
never executed. The thread sleeps unless a command arrives, so a high priority
costs the program nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SifIopSync() returns 1 once the IOP has finished booting (BOOTEND).
pkoReset() looped while it returned 1, so it fell through at once and called
SifInitRpc() on an IOP that was still rebooting. Use the same loop as
restartIOP() in ps2link.c: while (!SifIopSync());

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the program's main thread (userThreadID) was terminated. Other threads it
created (e.g. an SDL audio thread, pad or network threads) kept running while
the IOP was reset underneath them, for example blocked in or issuing RPCs.

Remember which threads exist right before the first program is started
(ps2link's own and any kernel/patch threads) and terminate and delete every
other thread in pkoReset(). The list lives in .bss, so it is rebuilt after the
ExecPS2() restart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A program may leave a GIF (channel 2) or VIF1 (channel 1) transfer running or
PATH3 masked. Stop both channels (with CPND held while STR is cleared), then
reset VIF1 and GIF before the IOP reset and the restart. Only kuseg registers
(0x1000xxxx) are touched, so this also works when the EE runs in user mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pkoReset() restarts ps2link with ExecPS2(&__start, ...). crt0 clears .bss but
.data is kept. newlib marks stdio as initialized in _impure_data (.data,
__cleanup), while the FILE structs and their locks (__sf) live in .bss. After
the restart __sinit() therefore skips the re-initialization and the first
printf() takes a NULL lock: TLB load exception, BadVAddr 8, EPC in
__retarget_lock_acquire_recursive, called from the command thread's first
dbgprintf().

It shows up on every start when IPCONFIG.DAT is missing (the initial boot
prints "Error reading ipconfig.dat" and then restarts), and after a reset
once ps2link has printed anything.

libcglue clears the mark in __locks_init(), but through the full struct
_reent of the installed headers. ps2link links newlib-nano (NEWLIB_NANO = 1),
which is built with _REENT_SMALL, where __cleanup is at offset 32 instead of
52, so that reset hits another field. Clear the mark through the _REENT_SMALL
layout before ExecPS2().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On reset, the IOP command thread first unmounted host: and deleted the tty
driver, and only then sent PKO_RPC_RESET to the EE. With a program that keeps
talking to the IOP (an SDL2 program printing once a second while streaming
audio), the reset never ran: host: was gone (TCP 18193 refused), the program
froze and only ICMP still answered.

Send the command to the EE first, then remove host: and tty as before.
Also import DelayThread, which the next commit uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceSifSetDma() returns 0 when the SIF DMA queue is full, e.g. while a program
streams audio or pad data. pkoSendSifCmd() then dropped the command; for a
reset that leaves ps2link dead. Retry for up to about one second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ps2sdk's InitThread() moves a program's main thread to priority 1 and SDL2
runs its audio thread one above that, at 0. When such a thread keeps the CPU
busy, the command thread (priority 1) is woken by the SIF0 interrupt but never
runs, so pkoReset() is never called.

When the interrupt handler sees PKO_RPC_RESET, demote all threads of the
program to the lowest priority and raise the command thread to 0 before
waking it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Consolidate the additional reset reliability fixes from ps2dev#71 into the original contributor PR. Remove the duplicate newlib-nano private struct, defer physical-button reset to an IOP worker, guard SIF DMA submission failures and the EE IOP reboot wait, and harden thread identity handling.
@fjtrujy fjtrujy changed the title Make reset reliable (thread priorities, IOP sync, stdio after restart, SIF DMA retry) Make reset reliable (IRQ-safe handoff, robust IOP reboot, SDK-owned stdio) Nov 24, 2026
@fjtrujy
fjtrujy marked this pull request as draft November 24, 2026 19:56
@fjtrujy
fjtrujy marked this pull request as ready for review November 24, 2026 20:07
@fjtrujy
fjtrujy merged commit b815f41 into ps2dev:master Nov 24, 2026
6 checks passed
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