From 67cbeb0fa792b9d2956d81d4ee3529603227aa00 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:00:00 +0100 Subject: [PATCH 1/8] ee: run the command thread at priority 1 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 --- ee/cmdHandler.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index 08f9b19..bfecfac 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -780,7 +780,14 @@ int initCmdRpc(void) th_attr.stack = cmdThreadStack; th_attr.stack_size = sizeof(cmdThreadStack); th_attr.gp_reg = &_gp; - th_attr.initial_priority = 20; + // (Almost) highest priority: the cmd thread must be able to run pkoReset() even + // when the loaded program has a thread that never blocks (e.g. a busy render or + // audio loop at a priority numerically below 20). The EE kernel schedules strictly + // by priority, so with 20 such a thread starves the cmd thread and "reset" is never + // executed. The thread sleeps in SleepThread() unless a command arrives, so this + // costs the user program nothing. 1 instead of 0 leaves 0 free for anything that + // really has to preempt even ps2link. + th_attr.initial_priority = 1; ret = CreateThread(&th_attr); if (ret < 0) { From 799c19b72129374432f701327c2e061e315d9bfe Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:00:00 +0100 Subject: [PATCH 2/8] ee: pkoReset: wait until the IOP has rebooted 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 --- ee/cmdHandler.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index bfecfac..1e6757d 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -841,7 +841,11 @@ void pkoReset(void) SifExitRpc(); SifIopReset(NULL, 0); - while (SifIopSync()) + // SifIopSync() returns 1 once the IOP has finished rebooting (BOOTEND), so wait + // while it returns 0 -- the same loop as restartIOP() in ps2link.c. The old + // "while (SifIopSync())" fell through at once and called SifInitRpc() on an IOP + // that was still rebooting. + while (!SifIopSync()) ; SifInitRpc(0); From ef6c1cb1db6909b02e7a86b27cbae1594971b7e9 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:00:00 +0100 Subject: [PATCH 3/8] ee: pkoReset: terminate all threads of the loaded program 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 --- ee/cmdHandler.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index 1e6757d..ea360d5 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -89,6 +89,47 @@ static int makeArgs(int cmdargc, char *cmdargv, struct sargs_start *arg_data) return 0; } +//////////////////////////////////////////////////////////////////////// +// Threads that existed before the first program was started: ps2link's own and any +// kernel/patch threads. pkoReset() leaves these alone and terminates all others. +// Kept in .bss, so the list starts empty again after the ExecPS2() in pkoReset(). +static unsigned char baseThreads[MAX_THREADS]; +static int baseThreadsValid; + +static void rememberBaseThreads(void) +{ + ee_thread_status_t status; + int id; + + if (baseThreadsValid) + return; + // Id 0 means "the calling thread" for ReferThreadStatus() (TH_SELF), so start at 1. + for (id = 1; id < MAX_THREADS; id++) + baseThreads[id] = (ReferThreadStatus(id, &status) >= 0); + baseThreadsValid = 1; +} + +// Terminate and delete every thread the loaded program created (its main thread, +// audio, pad, ... threads), not only userThreadID. +static void terminateUserThreads(void) +{ + ee_thread_status_t status; + int id, self; + + if (!baseThreadsValid) + return; // no program was started since ps2link came up + self = GetThreadId(); + for (id = 1; id < MAX_THREADS; id++) { + if (id == self || baseThreads[id]) + continue; + if (ReferThreadStatus(id, &status) < 0) + continue; + if (status.status != THS_DORMANT) + TerminateThread(id); + DeleteThread(id); + } +} + //////////////////////////////////////////////////////////////////////// // Load the actual elf, and create a thread for it // Return the thread id @@ -156,6 +197,7 @@ pkoExecEE(pko_pkt_execee_req *cmd) scr_printf("Executing file %s...\n", path); + rememberBaseThreads(); pid = pkoLoadElf(path); if (pid < 0) { scr_printf("Could not execute file %s\n", path); @@ -832,6 +874,10 @@ void pkoReset(void) } userThreadID = 0; + // The program's other threads (audio, pad, ...) would otherwise keep running, + // e.g. calling RPCs into the IOP while it is being reset below. + terminateUserThreads(); + if (sif0HandlerId >= 0) { DisableDmac(DMAC_SIF0); RemoveDmacHandler(DMAC_SIF0, sif0HandlerId); From 84a4d1e0357be099296df08b7906314ee677e192 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:00:00 +0100 Subject: [PATCH 4/8] ee: pkoReset: stop GIF and VIF1 DMA and reset GIF/VIF1 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 --- ee/cmdHandler.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index ea360d5..bdde319 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -130,6 +130,22 @@ static void terminateUserThreads(void) } } +// Stop GIF (channel 2) and VIF1 (channel 1) DMA the program may have left running +// and reset VIF1 and GIF, so no transfer continues across the IOP reset and the +// restart. All registers are in kuseg (0x1000xxxx): no KSEG access, fine in user mode. +static void stopGraphicsDma(void) +{ + unsigned int enabler = D_ENABLER; + + D_ENABLEW = enabler | 0x10000; // CPND: hold all channels while STR is cleared + D1_CHCR &= ~0x100; + D2_CHCR &= ~0x100; + D_ENABLEW = enabler; + VIF1_FBRST = 1; // VIF1 reset (also drops a MSKPATH3 mask) + GIF_CTRL = 1; // GIF reset + asm __volatile__("sync.l"); +} + //////////////////////////////////////////////////////////////////////// // Load the actual elf, and create a thread for it // Return the thread id @@ -877,6 +893,7 @@ void pkoReset(void) // The program's other threads (audio, pad, ...) would otherwise keep running, // e.g. calling RPCs into the IOP while it is being reset below. terminateUserThreads(); + stopGraphicsDma(); if (sif0HandlerId >= 0) { DisableDmac(DMAC_SIF0); From 9a8a35fdc81a7b403b0f345a8c96fa738f3d69cb Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:30:00 +0100 Subject: [PATCH 5/8] ee: pkoReset: let stdio re-initialize after the restart 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 --- ee/cmdHandler.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index bdde319..c858e4a 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -880,6 +880,18 @@ int initCmdRpc(void) } //////////////////////////////////////////////////////////////////////// +// Start of struct _reent as newlib-nano is built (_REENT_SMALL, see the +// "#ifdef _REENT_SMALL" variant in ), up to __cleanup. +struct reentSmallHead { + int errno_; + void *stdin_, *stdout_, *stderr_; + int inc; + char *emergency; + void *locale; + void *mp; + void (*cleanup)(struct _reent *); +}; + void pkoReset(void) { char *argv[1]; @@ -917,5 +929,19 @@ void pkoReset(void) argv[0] = elfName; SifLoadFileExit(); + // The restart below keeps .data but crt0 clears .bss. newlib keeps the "stdio is + // initialized" mark (__cleanup) in _impure_data (.data), while the stdio FILE structs + // and their locks (__sf) are in .bss. Without clearing the mark, __sinit() skips the + // re-initialization after the restart and the first printf() runs with a NULL lock + // (TLB miss at address 8 in __retarget_lock_acquire_recursive). This happens whenever + // stdio was used before the restart, e.g. the initial boot printing "Error reading + // ipconfig.dat" when there is no IPCONFIG.DAT. + // ps2sdk's libcglue clears the mark in __locks_init(), but through the full + // struct _reent of the installed headers, while ps2link links newlib-nano + // (NEWLIB_NANO = 1), which is built with _REENT_SMALL: there __cleanup sits at + // another offset (32 instead of 52), so that reset hits the wrong field. Clear + // it through the _REENT_SMALL layout that libc_nano's __sinit() actually reads. + ((struct reentSmallHead *)_impure_ptr)->cleanup = NULL; + ExecPS2(&__start, 0, 1, argv); } From 9c59dfe6ecd658541d2a0a43b7236db9af460103 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:52:20 +0100 Subject: [PATCH 6/8] iop: reset: forward the command to the EE before removing host: and tty 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 --- iop/cmdHandler.c | 9 +++++++-- iop/imports.lst | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/iop/cmdHandler.c b/iop/cmdHandler.c index ea7e38f..efe1038 100644 --- a/iop/cmdHandler.c +++ b/iop/cmdHandler.c @@ -196,12 +196,17 @@ pkoReset(char *buf, int len) return; } + // Tell the EE first: it terminates the program's threads and resets the IOP. + // Removing host: and tty before that broke the reset with programs that keep + // talking to the IOP (e.g. an SDL2 program printing once a second while + // streaming audio): the reset never ran (host: gone, TCP 18193 refused, only + // ICMP answered). + pkoSendSifCmd(PKO_RPC_RESET, buf, len); + printf("unmounting\n"); fsysUnmount(); printf("unmounted\n"); DelDrv("tty"); - - pkoSendSifCmd(PKO_RPC_RESET, buf, len); }; static void diff --git a/iop/imports.lst b/iop/imports.lst index 31562a1..c5bfb49 100644 --- a/iop/imports.lst +++ b/iop/imports.lst @@ -22,6 +22,7 @@ I_DeleteThread I_StartThread I_GetThreadId I_ExitDeleteThread +I_DelayThread thbase_IMPORTS_end ioman_IMPORTS_start From ca8873b27c6d3425b32e413e07460093a6fa16bd Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:52:20 +0100 Subject: [PATCH 7/8] iop: retry the SIF DMA when the queue is full 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 --- iop/cmdHandler.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/iop/cmdHandler.c b/iop/cmdHandler.c index efe1038..60dfb00 100644 --- a/iop/cmdHandler.c +++ b/iop/cmdHandler.c @@ -130,6 +130,7 @@ pkoSendSifCmd(unsigned int cmd, void *src, unsigned int len) { unsigned int dmaId; unsigned int dmaLen; + int i; rpc_data[0] = cmd; @@ -144,6 +145,13 @@ pkoSendSifCmd(unsigned int cmd, void *src, unsigned int len) dmaLen = (len + 15) & ~15; dmaId = pkoSetSifDma(PKO_DMA_DEST, rpc_data, dmaLen, 4); + // sceSifSetDma() returns 0 when the SIF DMA queue is full, e.g. while a program + // streams audio or pad data over SIF. Retry for up to ~1 s instead of dropping + // the command (a dropped reset leaves the EE program running and ps2link dead). + for (i = 0; dmaId == 0 && i < 100; i++) { + DelayThread(10000); + dmaId = pkoSetSifDma(PKO_DMA_DEST, rpc_data, dmaLen, 4); + } if (dmaId == 0) { printf("IOP: sifSendCmd %x failed\n", cmd); From f181aa457265699199946810edede67d78b24b35 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:30:00 +0100 Subject: [PATCH 8/8] ee: on reset, let the command thread preempt the program 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 --- ee/cmdHandler.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ee/cmdHandler.c b/ee/cmdHandler.c index c858e4a..b80a9c2 100644 --- a/ee/cmdHandler.c +++ b/ee/cmdHandler.c @@ -734,6 +734,29 @@ pkoStartVU(pko_pkt_start_vu *cmd) // Sif dma interrupt handler, wakes up the cmd handler thread if // data was sent to our dma area +// A reset has to get through even when the program keeps the CPU busy at a high +// priority: ps2sdk's InitThread() moves a program's main thread to priority 1, SDL2 +// runs its audio thread one above that (0), and while the IOP side goes down their +// RPC calls can spin. Right here in the interrupt, demote every thread of the +// program to the lowest priority and raise the command thread to 0, so the kernel +// switches to the command thread when this handler returns. +static void prepareResetFromIntr(void) +{ + ee_thread_status_t status; + int id; + + if (baseThreadsValid) { + for (id = 1; id < MAX_THREADS; id++) { + if (baseThreads[id]) + continue; + if (iReferThreadStatus(id, &status) < 0 || status.status == THS_DORMANT) + continue; + iChangeThreadPriority(id, MAX_PRIORITY - 2); + } + } + iChangeThreadPriority(cmdThreadID, 0); +} + static int pkoCmdIntrHandler(int channel) { int flag; @@ -746,6 +769,9 @@ static int pkoCmdIntrHandler(int channel) // Clear new packet flag *sifDmaDataPtr = 0; + if (flag == PKO_RPC_RESET) + prepareResetFromIntr(); + iWakeupThread(cmdThreadID); }