Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 104 additions & 3 deletions ee/cmdHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,63 @@ 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);
}
}

// 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
Expand Down Expand Up @@ -156,6 +213,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);
Expand Down Expand Up @@ -676,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;
Expand All @@ -688,6 +769,9 @@ static int pkoCmdIntrHandler(int channel)
// Clear new packet flag
*sifDmaDataPtr = 0;

if (flag == PKO_RPC_RESET)
prepareResetFromIntr();

iWakeupThread(cmdThreadID);
}

Expand Down Expand Up @@ -780,7 +864,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) {
Expand Down Expand Up @@ -814,7 +905,6 @@ int initCmdRpc(void)
return 0;
}

////////////////////////////////////////////////////////////////////////
void pkoReset(void)
{
char *argv[1];
Expand All @@ -825,6 +915,11 @@ 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();
stopGraphicsDma();

if (sif0HandlerId >= 0) {
DisableDmac(DMAC_SIF0);
RemoveDmacHandler(DMAC_SIF0, sif0HandlerId);
Expand All @@ -834,7 +929,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);
Expand All @@ -843,5 +942,7 @@ void pkoReset(void)
argv[0] = elfName;
SifLoadFileExit();

// crt0 calls libcglue's __locks_init() after ExecPS2(), which now resets
// newlib-nano's stdio state as well (ps2sdk #969).
ExecPS2(&__start, 0, 1, argv);
}
17 changes: 15 additions & 2 deletions iop/cmdHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -196,12 +204,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
Expand Down
1 change: 1 addition & 0 deletions iop/imports.lst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ I_DeleteThread
I_StartThread
I_GetThreadId
I_ExitDeleteThread
I_DelayThread
thbase_IMPORTS_end

ioman_IMPORTS_start
Expand Down
Loading