forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Hooking Usermode Functions

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 6:21 am    Post subject: Hooking Usermode Functions Reply with quote

Right, this is a bit of pain lately. I'm hooking various functions located in the Paf and I can correctly replace the JAL's with the Syscalls to the function in my kernel module. However, when I try to call the original function (in Paf) it always crashes (obviously). I am however at loss as I do not want to create a usermode module and would rather keep it all in one kernel mode module.

Any help? If you need anymore information just ask.
Thanks a lot.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
angelo



Joined: 29 Aug 2007
Posts: 168

PostPosted: Fri Oct 17, 2008 7:25 am    Post subject: Reply with quote

I need to make a PRX in the appropriate mode and then load it. This scenario was common for audio I believe.

Angelo
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 7:39 am    Post subject: Reply with quote

Hmm, I'm trying to not load another module. Surely there is a way to execute a usermode function under kernel mode?
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Fri Oct 17, 2008 8:09 am    Post subject: Reply with quote

Well, there is an exotic way to do that...

sceKernelSetDdrMemoryProtection((void *)0x08000000, 2*1024*1024, 0xF);

after that you would be able to redirect a user call to a kernel function replacing a jal by other jal, and the function will execute in user mode. Of course, if you call kernel functions inside that function, you may expect a crash...
Back to top
View user's profile Send private message
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 8:23 am    Post subject: Reply with quote

I'll give that a shot. I'll post back with results.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 8:55 am    Post subject: Reply with quote

It crashes when the patched function is first executed. Damn.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
angelo



Joined: 29 Aug 2007
Posts: 168

PostPosted: Fri Oct 17, 2008 9:03 am    Post subject: Reply with quote

Just use a shim by using a user / kernel PRX and loadstart it. It may be your only way...

Angelo
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 9:17 am    Post subject: Reply with quote

I'd rather not. I believe user memory is very limited in VSH. I'd rather keep away from it.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Fri Oct 17, 2008 11:47 pm    Post subject: Reply with quote

Is there any other alternatives?

Code:
#include <pspkernel.h>
#include <pspsysmem_kernel.h>
#include <pspsdk.h>

#include <systemctrl.h>
#include <string.h>

#include "debugutils.h"

PSP_MODULE_INFO("UserHook", 0x1000, 1, 0);

#define MAKE_CALL(a, f) _sw(0x0C000000 | (((u32)(f) >> 2)  & 0x03ffffff), a);

int (*scePafMemoryAlloc)(int, int) = NULL;
STMOD_HANDLER previous = NULL;

int scePafMemoryAllocPatched(int allignment, int size)
{
   return scePafMemoryAlloc(allignment, size);
}

int OnModuleStart(SceModule2 *mod)
{
   char *modname = mod->modname;
   u32 text_addr = mod->text_addr;
   
   if (strcmp(modname, "vsh_module") == 0)
   {
      scePafMemoryAlloc = (void *)FindProc("scePaf_Module", "scePaf", 0x31AC0624);
      
      MAKE_CALL(text_addr + 0xB3D8, scePafMemoryAllocPatched);
      
      sceKernelDcacheWritebackAll();
      sceKernelIcacheClearAll();
   }
   
   if (!previous)
      return 0;
   
   return previous(mod);
}


int module_start(SceSize args, void *argp)
{
   sceKernelSetDdrMemoryProtection((void *)0x08000000, 2*1024*1024, 0xF); 
   
   previous = sctrlHENSetStartModuleHandler(OnModuleStart);   
   return 0;
}


Thats my code, all updated for 5.00 but it crashes =/
Got any other suggestions? Moonlight? Anyone?
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Sun Oct 19, 2008 12:33 am    Post subject: Reply with quote

Super Sheep wrote:
I'd rather not. I believe user memory is very limited in VSH. I'd rather keep away from it.



Uhm....

if you search for a way to do it without another PRX, the way will be more complex and will take up more space than a dumb prx... Plus, there is plenty memory in VSH if you know where to look.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sun Oct 19, 2008 2:27 am    Post subject: Reply with quote

I am assuming you want to call the function then do something with the result rather than just hooking the entry to the function, this makes it harder.

You _could_ implement a trick using exception handlers to do it. Stick in a 'break' instruction in the delay slot of the j address; nop; import for the function in the module you want to hook then it will crash when the function is called. You could catch this and hand off to a function to handle your side of it, when you need to call the original you can either just tweak EPC to point to the actual function and return from the exception, which would not return back to you, or you change the saved RA register then when the function returns it crashes again at an appropriately selected address (so you can decode which function it was) and so an epilog for the function.

Of course it is just simpler to use a dumb user mode prx as adrahil has pointed out :)
Back to top
View user's profile Send private message
Super Sheep



Joined: 23 Mar 2008
Posts: 31

PostPosted: Sun Oct 19, 2008 2:33 am    Post subject: Reply with quote

I'll just load a usermode prx in the volatile memspace and leech off the paf libc =P
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group