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 

Kenel mode / User Mode switch

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



Joined: 04 Jun 2004
Posts: 76

PostPosted: Sun Jul 10, 2005 9:03 am    Post subject: Kenel mode / User Mode switch Reply with quote

Is there a way if I start my thread in user mode that I can change to kernel mode and vice versa? On the ps2 it was quite easy: DI(); ee_kmode_enter(); EI(); etc..

Thx!
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Jul 10, 2005 9:20 am    Post subject: Reply with quote

Unless you put yourself in user mode by setting the appropiate bit in the cop0 status register, once you create a user mode thread you cannot flip back and forth between user and kernel mode.

If you are flipping back and forth, just use the code in the ee_kmode_enter() and ee_kmode_exit() macros.
Back to top
View user's profile Send private message
apache37



Joined: 04 Jun 2004
Posts: 76

PostPosted: Sun Jul 10, 2005 9:34 am    Post subject: Reply with quote

Well if I start in kernel mode can I set the cop0 register and drop into user mode?
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Jul 10, 2005 9:41 am    Post subject: Reply with quote

Yes, that's the first thing I said.
Back to top
View user's profile Send private message
apache37



Joined: 04 Jun 2004
Posts: 76

PostPosted: Sun Jul 10, 2005 9:59 am    Post subject: Reply with quote

static inline int kmode_exit(void)
{
int status;

__asm__ volatile (
".set\tnoreorder\n\t" \
"mfc0\t%0, $12\n\t" \
"ori\t%0, 0x10\n\t" \
"mtc0\t%0, $12\n\t" \
"sync.p\n\t" : "=r" (status));

return status;
}

kmode_exit();
freezes the PSP.

Is it that I have the wrong registers?
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Jan 17, 2009 1:22 am    Post subject: Reply with quote

Is this method still applicable in 2009? Or only on older firmware. I'm wondering if I can make user mode calls from my kernel thread if I do this.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 17, 2009 1:51 am    Post subject: Reply with quote

The code as presented would never have worked on the PSP, it isn't quite as simple as that :)
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Jan 17, 2009 11:07 pm    Post subject: Reply with quote

Basically is there any way to redirect a user function call to a function thats in a kernel plugin?
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sun Jan 18, 2009 1:29 am    Post subject: Reply with quote

So what you _actually_ want to do is get something in user mode to call a kernel function and not the other way around? That is what syscalls are for ;) In all serious you could export a function from your kernel module, find the syscall (that is up to you to work out :P) and replace the call to a syscall.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Jan 18, 2009 1:30 am    Post subject: Reply with quote

TyRaNiD wrote:
So what you _actually_ want to do is get something in user mode to call a kernel function and not the other way around? That is what syscalls are for ;) In all serious you could export a function from your kernel module, find the syscall (that is up to you to work out :P) and replace the call to a syscall.


No I want to hook a pure user function call from my kernel module, without having to put the new function in user space. Or can I do something like replacing the user ordinary function call with a MAKE_SYSCALL ?? Will that work? I assume I will have to tamper with the surrounding code, set up registers etc, but I don't know MIPS.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Jan 18, 2009 1:37 am    Post subject: Reply with quote

From the stub list, it appears that the vblank functions are syscalls, but hooking them with syscall patch doesnt seem to work. I assume games use a statically linked version or something, so I wanted to hook that.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sun Jan 18, 2009 2:47 am    Post subject: Reply with quote

Stuff like sceDisplayWaitBlankStart are syscalls from what I recall, nothing more nothing less.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Jan 18, 2009 2:51 am    Post subject: Reply with quote

TyRaNiD wrote:
Stuff like sceDisplayWaitBlankStart are syscalls from what I recall, nothing more nothing less.


Regarding my original question, is it possible?

I need to hook a user mode function, from my kernel module (an ordinary function, not a syscall). Can I replace the function call with a syscall?? How would I need to change any surrounding code if necessary?

I read something about the setddrmemoryprotection function that allows you to jump into a kernel function from a user thread, but I don't think any one got it to work. So I assume turning it into a syscall will work.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sun Jan 18, 2009 4:56 am    Post subject: Reply with quote

Well you could unlock kernel memory and jump into it, the issue comes if your code (or any kernel functions you call) uses absolute addresses for any variables etc. as while you will have unlocked the lower 4 megs of ram you still won't be able to access it through Kseg0 (i.e. addresses at 0x8xxxxxxx). If you are just patching the jal instruction then if you went the syscall route all you would need to do would be swap the jal and its delay branch around and then set the syscall as the second instruction.

e.g. if you have:
Code:
jal someuserfunction
move $a0, $s0

You convert to:
Code:
move $a0, $s0
syscall #num
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Jan 18, 2009 5:11 am    Post subject: Reply with quote

I'll try converting it to a syscall. So I just have to make my module export a kernel-user function and when my module starts it will automatically make a syscall table entry for the function right.

How do I get the #num for my function? I understand that its not simply the address value in the syscall table....
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Sun Mar 29, 2009 5:23 am    Post subject: Reply with quote

Sorry to bump a somewhat old topic but I've got the same question as Torch.
Someone know a way to figure out that num?
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Mar 29, 2009 1:53 pm    Post subject: Reply with quote

Its not easy on new firmware. The tables are randomized.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Mar 29, 2009 2:05 pm    Post subject: Reply with quote

The way some are found is to look at the old code when the NIDs were all computed from the name, then look for similar code in the new libs and see which random NID uses that code.
Back to top
View user's profile Send private message AIM Address
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Mar 29, 2009 4:54 pm    Post subject: Reply with quote

J.F. wrote:
The way some are found is to look at the old code when the NIDs were all computed from the name, then look for similar code in the new libs and see which random NID uses that code.

?? But thats to find the NID. We want to find the syscall number for the function in the syscall table so that we can write a MIPS instruction "syscall num".

We already know the NID because we want to make a syscall to OUR OWN coded function in our kernel PRX. Apparently we need to find the syscall number that gets entered in the table when the PRX is loaded.

I'm a bit hazy on how the prx loader/syscall table and stuff works.
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Sun Mar 29, 2009 8:34 pm    Post subject: Reply with quote

Even if we didn't know our own NIDs... we could just compute them via SHA-1 from the functionnames... (which is what I do... I beg you - don't ask... xD)

Any idea where this numtable is located in memory? If we can just dump it it shouldn't be difficult to interpret it...

We would just have to disable interrupts for a few secs, dump it and reenable interrupts then.
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Mar 29, 2009 9:27 pm    Post subject: Reply with quote

The apihook functions from psplink already clearly show how to iterate through the syscall table and find function addresses.

That is what is used for patching one syscall to another for syscall hooks. The thing is you just give the addresses of the function you want to hook and the apihook functions replace it with the address of your hook function. The syscall number of the original function remains the same, only the address it jumps to is changed.

You'll have to figure out yourself and how to find the syscall number for your hook function address. (Because when you load your kernel PRX, its syscall exports will be entered in the table with their own syscall numbers.)
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Sun Mar 29, 2009 9:44 pm    Post subject: Reply with quote

Yeah but that table must be located somewhere afterall... I would need to know where.
Oh well... I will figure out a way I suppose. Got the sourcecode somewhere anyway.
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message 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