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 

Kernel to user jump?

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



Joined: 06 May 2008
Posts: 74

PostPosted: Fri Aug 07, 2009 5:46 am    Post subject: Kernel to user jump? Reply with quote

Hi.
Someone knows if it's possible to do a kernel-to-user jump?
Something like this:

Code:
/* Simple user mode function */
int userF() {
   return 0;
}


can i jump to userF from a kernel module? If yes, it's just a simple jump or a more complicated thing? userF gets executed as user or kernel? (i'll aspect it to be executed as kernel, because when userF returns the context cannot be switched from user to kernel with a jump like "jump to $ra", only with syscalls)

Thanks.

Bye,
ab5000.
_________________
Code:
%:include<stdio.h>
int _(int __,int ___,int ____,int _____)
<%for(;____<___;_____=_____*__,____++);
return _____;%>main()<%printf
("%d\n",_(2,5,0,1));%>
Back to top
View user's profile Send private message
Bubbletune



Joined: 03 Jan 2009
Posts: 28

PostPosted: Fri Aug 07, 2009 5:15 pm    Post subject: Reply with quote

Yes, you can, using a simple jump, but it'll be executed in kernel mode.
Back to top
View user's profile Send private message
ab5000



Joined: 06 May 2008
Posts: 74

PostPosted: Fri Aug 07, 2009 7:50 pm    Post subject: Reply with quote

Bubbletune wrote:
Yes, you can, using a simple jump, but it'll be executed in kernel mode.


Thanks! :)
_________________
Code:
%:include<stdio.h>
int _(int __,int ___,int ____,int _____)
<%for(;____<___;_____=_____*__,____++);
return _____;%>main()<%printf
("%d\n",_(2,5,0,1));%>
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Aug 08, 2009 1:36 am    Post subject: Reply with quote

When you execute user functions from kernel mode, you need to set k1:

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);
   function(args);
   pspSdkSetK1(k1);
Back to top
View user's profile Send private message AIM Address
Bubbletune



Joined: 03 Jan 2009
Posts: 28

PostPosted: Sat Aug 08, 2009 2:58 am    Post subject: Reply with quote

J.F. wrote:
When you execute user functions from kernel mode, you need to set k1:

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);
   function(args);
   pspSdkSetK1(k1);


No, you don't. That's when you hooked a system call and want to make it seem like you're coming from kernel mode.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Aug 08, 2009 3:06 am    Post subject: Reply with quote

Bubbletune wrote:
J.F. wrote:
When you execute user functions from kernel mode, you need to set k1:

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);
   function(args);
   pspSdkSetK1(k1);


No, you don't. That's when you hooked a system call and want to make it seem like you're coming from kernel mode.


Yes, you do. Look at the MediaEnginePRX... you're calling user functions from kernel mode because the ME only runs in kernel mode at the moment. You HAVE to set k1 or the whole thing bombs out. There are other examples. But it has NOTHING at all to do with hooks, system or otherwise.
Back to top
View user's profile Send private message AIM Address
Bubbletune



Joined: 03 Jan 2009
Posts: 28

PostPosted: Sat Aug 08, 2009 3:11 am    Post subject: Reply with quote

J.F. wrote:
Bubbletune wrote:
J.F. wrote:
When you execute user functions from kernel mode, you need to set k1:

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);
   function(args);
   pspSdkSetK1(k1);


No, you don't. That's when you hooked a system call and want to make it seem like you're coming from kernel mode.


Yes, you do. Look at the MediaEnginePRX... you're calling user functions from kernel mode because the ME only runs in kernel mode at the moment. You HAVE to set k1 or the whole thing bombs out. There are other examples. But it has NOTHING at all to do with hooks, system or otherwise.


I guess I didn't phrase it correctly when I said hooks, but I keep up the fact that it's to make it look like you're coming from a kernel function as opposed to a syscall. I just downloaded the MediaEnginePRX and I don't see a single call to a user mode function from kernel mode in there.
Back to top
View user's profile Send private message
Davee



Joined: 22 Jun 2009
Posts: 59

PostPosted: Sat Aug 08, 2009 4:29 am    Post subject: Reply with quote

J.F. wrote:
When you execute user functions from kernel mode, you need to set k1:

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);
   function(args);
   pspSdkSetK1(k1);


Other way around, calling kernel functions from user mode SOMETIMES requires $k1 changing. It's a way the kernel can check for usermode calls and escalate to allow kernel mode argument passing later in the function. There is a lot more behind the scenes though with the syscall exception obviously though.

In kernel mode $k1 is 0, so setting it to 0 is pointless.
Looks like you got the two mixed around =/

To call from kernel to user you either OR the address with 0x80000000 or you create a user mode thread. Depending on the circumstances, a usermode thread may be more suitable.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Aug 08, 2009 7:09 am    Post subject: Reply with quote

Look at the kernel mode PRX example thread for details, but when you call a user function from kernel mode, you set k1. It's in every example of kernel mode prx's there are... it's in every kernel mode prx you'll find. Look at cooleye's kernel prx to set the sample rate... or the kernel prx to get the HOME button... or the MediaEnginePRX. We didn't just make this up to laugh at people - if you don't set k1, it DOESN'T WORK.

@Bubbletune: The call is in the loop.

Code:
static void me_loop(volatile struct me_struct *mei)
{
   unsigned int k1;

   k1 = pspSdkSetK1(0);

   while (mei->init) // ME runs this loop until killed
   {
      while (mei->start == 0); // wait for function
      mei->start = 0;
      if (mei->precache_len)
      {
         if (mei->precache_len < 0)
            dcache_inv_all();
         else
            dcache_inv_range(mei->precache_addr, mei->precache_len);
      }
      mei->result = mei->func(mei->param); // run function
      if (mei->postcache_len)
      {
         if (mei->postcache_len < 0)
            dcache_wbinv_all();
         else
            dcache_wbinv_range(mei->postcache_addr, mei->postcache_len);
      }
      mei->done = 1;
   }

   pspSdkSetK1(k1);

   while (1); // loop forever until ME reset
}


Specifically

Code:
mei->result = mei->func(mei->param); // run function


Those functions passed in are to user functions in the program.
Back to top
View user's profile Send private message AIM Address
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Aug 08, 2009 8:02 am    Post subject: Reply with quote

Code:
%:include<stdio.h>
int _(int __,int ___,int ____,int _____)
<%for(;____<___;_____=_____*__,____++);
return _____;%>main()<%printf
("%d\n",_(2,5,0,1));%>


Code:
main()<%printf("32\n");%>

:P
Back to top
View user's profile Send private message
ab5000



Joined: 06 May 2008
Posts: 74

PostPosted: Sat Aug 08, 2009 6:45 pm    Post subject: Reply with quote

hlide wrote:
Code:
%:include<stdio.h>
int _(int __,int ___,int ____,int _____)
<%for(;____<___;_____=_____*__,____++);
return _____;%>main()<%printf
("%d\n",_(2,5,0,1));%>


Code:
main()<%printf("32\n");%>

:P


right xD
_________________
Code:
%:include<stdio.h>
int _(int __,int ___,int ____,int _____)
<%for(;____<___;_____=_____*__,____++);
return _____;%>main()<%printf
("%d\n",_(2,5,0,1));%>
Back to top
View user's profile Send private message
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