Cache invalidation in Kernel PRX not working?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Cache invalidation in Kernel PRX not working?

Post by Hellcat »

Hi everyone :)

I got a problem again....


Lately I got a habit of patching syscalls :D
So far it always worked fine.... mostly....

Usually I have a usermode PRX that loads a pure kernel mode PRX, the kernel mode PRX does all the hooking/patching and everthing is fine, my own functions get called instead of the original ones.
The kernel mode PRX is also compiled against the kernel libs with those two line in the makefile:

Code: Select all

USE_KERNEL_LIBC=1
USE_KERNEL_LIBS=1

So, NOW I have a PRX that is supposed to be a CFW plugin.
I also build it as kernel module, and here comes the thing....

With the above two lines in my makefile theese two lines of code seem to have no effect:

Code: Select all

sceKernelDcacheWritebackInvalidateRange(addr, sizeof(addr)); 
sceKernelIcacheInvalidateRange(addr, sizeof(addr));
Instead of my hooked functions, the original ones are still called!

If I take out the KERNEL_LIB lines mentioned above from my makefile everything works perfectly and my hooked functions are called like they are supposed to.


What am I missing here? :)
Or could I just compile my PRX without those lines without risking mayor compatibility problems with games running?
Another advantage not to compile against those would be that I could use printf() again, couldn't I? :D
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

There are only stubs for the user mode (UtilsForUser) versions of those functions in the SDK.

The kernel mode versions use the same NIDS, but are under the UtilsForKernel library.

Just create a stub file and link against that instead of -lpsputils.
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

Hey, cool! Will try :) Thanks!

I thought it might be something like that, but since the compiler didn't complain about missing references I didn't keep thinking of it....
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Actually, on looking there ARE stubs for those functions in the SDK.

They're part of -lpspkernel and the header is psputilsforkernel.h

You'll have to add the prototype for sceKernelDcacheWritebackInvalidateRange, but it will be the same as the user mode version.
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

Hmm, just got some time to try that.

No go :(

I already liked against lpspkernel and was using psputilsforkernel.h as header.
So, I tried making an own stub, importing from "UtilsForKernel".

But still no go, the cache doesn't get invalidated....


[EDIT]
ARGH!
By a second look at it, it seems the cache stuff works, my hooked functions are called - but my callbacks don't work!

sceKernelNotifyCallback() is the one that seems to ignore me now....
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Hellcat wrote:Hmm, just got some time to try that.
sceKernelNotifyCallback() is the one that seems to ignore me now....
Be aware :

when you create a callback, it is callable only in the thread where it was created. To issue a callback, you just need to call sceKernelNotifyCallback(cbid) in any thread. But you need to call sceKernelCheckCallback() or sceKernelWaitThreadCB() or sceKernelSleepThreadCB() or whatever else ending with CB in the thread where the callback was created, otherwise you will never receive a callback notification.

If the thread of your callback uses sceKernelWaitThread() or sceKernelSleepThread() or whatever else not ending with CB, no callback won't be checked and executed.
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

Yep, the thread creating the callbacks goes to sleep with sceKernelSleepThreadCB().

When NOT compiling against the kernel libs it works perfectly.
The callbacks get "called" and all is fine.
But when I compile against the kernel libs => no go, the callbacks don't get executed anymore.
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

It is kernel code who created the callbacks?
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

Unfortunately yes.
It's a CFW plugin.

[EDIT]
When trying to start the thread creating the callbacks as userthread (PSP_THREAD_ATTR_USER) makes the PSP crash the moment the thread is started - even tho it didn't do anything (besides a sceKernelSleepThread()) at that point....
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Maybe the system don't like callbacks created by kernel to be used by user mode. It happens something like that with file descriptors since some version (3.40 i think).
Try this code after creating the callback in kernel mode, just in case it works:
(this code did work at least with file descriptors)

Code: Select all

u16 *block;

if (sceKernelGetUIDcontrolBlock(cbid, (void *)&block) == 0)
{
	block[0x16/2] |= 0x0015;
}
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

I tried it, but didn't work....

Well, it seems I managed to get it working anyway.
Rearranged some code and added a few SetK1's - now everthing seems OK.


But that snipped you gave me there might come in handy on another thing, where I'm indeed hooking IoOpen :D
Post Reply