Skip to content
This repository was archived by the owner on Mar 18, 2023. It is now read-only.

Commit ce32660

Browse files
committed
Fix #29.
1 parent 02ade93 commit ce32660

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

engine/engine.c

+31-5
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,48 @@ int register_callback(void *func)
146146
return 0;
147147
}
148148

149+
#define SENTINEL_VALUE 0x5AFEC0DE
150+
/* These values should hang around if IGR in OPL is activated */
151+
extern u32 Uninitialized;
152+
static u32 *pOldSetupThread = &Uninitialized;
153+
static u32 *pSentinel = &Uninitialized + 1;
154+
155+
void InstallHook()
156+
{
157+
*pOldSetupThread = GetSyscallHandler(__NR_SetupThread);
158+
SetSyscall(__NR_SetupThread, KSEG0(HookSetupThread));
159+
*pSentinel = SENTINEL_VALUE;
160+
}
161+
162+
void RemoveHook()
163+
{
164+
SetSyscall(__NR_SetupThread, *pOldSetupThread);
165+
}
166+
149167
int __attribute__((section(".init"))) _init(void)
150168
{
151169
#ifdef _HOOK_9
152170
/* hook syscall */
153-
OldSetupThread = GetSyscallHandler(__NR_SetupThread);
154-
j_SetupThread = MAKE_J(OldSetupThread);
155-
SetSyscall(__NR_SetupThread, KSEG0(HookSetupThread));
171+
if(*pSentinel == SENTINEL_VALUE && *pOldSetupThread)
172+
{
173+
/* When the engine is installed it hooks SetupThread and keeps the
174+
* address to the original callback in an uninitialized area that should
175+
* be retained if IGR is activated in OPL. The sentinel value signals
176+
* that the engine has already been installed, so we need to temporarily
177+
* restore the original SetupThread callback before "installing" it
178+
* again.
179+
*/
180+
RemoveHook();
181+
}
182+
InstallHook();
156183
#endif
157184
return 0;
158185
}
159186

160187
int __attribute__((section(".fini"))) _fini(void)
161188
{
162189
#ifdef _HOOK_9
163-
/* unhook syscall */
164-
SetSyscall(__NR_SetupThread, OldSetupThread);
190+
RemoveHook();
165191
#endif
166192
return 0;
167193
}

engine/engine_asm.S

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
.globl eh_backup
5858
.globl CodeHandler
5959
.globl BootCodeHandler
60+
.globl Uninitialized
6061

6162
#ifdef _HOOK_9
6263
.globl HookSetupThread
@@ -798,8 +799,8 @@ HookSetupThread:
798799
addiu $sp, 0x10
799800

800801
/* jump to original SetupThread() */
801-
j_SetupThread:
802-
j 0
802+
lw $t1, Uninitialized
803+
j $t1
803804
nop
804805
.end HookSetupThread
805806
#endif
@@ -860,6 +861,9 @@ numcodes:
860861
codelist:
861862
.space (MAX_CODES*8)
862863

864+
Uninitialized:
865+
.space /* Anything after this address should be retained when activating IGR in OPL. */
866+
863867
.set pop
864868

865869
/* EOF */

0 commit comments

Comments
 (0)