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 

PRX doesn't have Kernel Priviliges

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



Joined: 21 Mar 2009
Posts: 6

PostPosted: Sat Mar 21, 2009 5:50 pm    Post subject: PRX doesn't have Kernel Priviliges Reply with quote

Here is the PRX:

Code:

#include <pspkernel.h>

#include <pspmodulemgr_kernel.h>
#include <psppower.h>
#include <pspgu.h>
#include <pspwlan.h>
#include <pspnet.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <arpa/inet.h>
#include <pspthreadman_kernel.h>

#include <pspsdk.h>
#include <string.h>



PSP_MODULE_INFO("TestPRX", 0x1000, 0, 2);

PSP_MAIN_THREAD_ATTR(0);

PSP_HEAP_SIZE_KB( 2048);

PSP_MAIN_THREAD_STACK_SIZE_KB( 2048);

int main(int argc, char **argv)
{
   return 1;
}

int Test()
{
   return 1;
}

int getMode()
{
   u32 k1;

   k1 = pspSdkSetK1(0);

   int ret = sceKernelIsUserModeThread();

   pspSdkSetK1(k1);   

   return ret;
}





Here is the PRX makefile:

Code:

TARGET = TestPRX
OBJS = main.o

USE_KERNEL_LIBC=1

BUILD_PRX=1
PSP_FW_VERSION=371
PRX_EXPORTS=exports.exp

USE_PSPSDK_LIBC = 1

LIBS = -lpspgu -lpsppower -lpspwlan

INCDIR = $(srcdir)
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


And the Exports:

Code:

PSP_BEGIN_EXPORTS

PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC(module_start)
PSP_EXPORT_VAR(module_info)
PSP_EXPORT_END

# Name
PSP_EXPORT_START(TestPRX, 0, 0x4001)

# Functions
PSP_EXPORT_FUNC(Test)
PSP_EXPORT_FUNC(getMode)

PSP_EXPORT_END

PSP_END_EXPORTS


How I load the PRX:
Code:
SceUID mod = pspSdkLoadStartModule("TestPRX.prx", PSP_MEMORY_PARTITION_KERNEL);


The PRX resides in the same folder as the Eboot.

When the PRX is loaded it can be accessed fine but when I run:
getMode();
It returns 1, meaning the PRX is in User Mode.

What went wrong?
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Mar 21, 2009 8:19 pm    Post subject: Reply with quote

The function is executed from the context of a the thread that calls it, i.e. user mode if your calling it from a user module, but it can do kernel stuff inside the function.

pspSdkSetK1(0) should make it seem like kernel mode, but I dunno whats wrong.

Can you actually call any kernel functions from inside the getMode function?
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Mar 21, 2009 9:22 pm    Post subject: Reply with quote

Torch wrote:
pspSdkSetK1(0) should make it seem like kernel mode, but I dunno whats wrong.


why ? sceKernelIsUserModeThread() is probably just inquiring whether if the thread was created as a user thread regardless if we are running a kernel-mode function through syscall. And $k1 register is just a general purpose register : setting it to 0 doesn't make the thread runs in kernel-mode. That's just a paranoiac and silly use of Sony to protect kernel function calls when not properly set. I say silly because if Sony thought to kick out hackers this way, that's a stupid protection.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Mar 21, 2009 10:08 pm    Post subject: Reply with quote

hlide wrote:
And $k1 register is just a general purpose register : setting it to 0 doesn't make the thread runs in kernel-mode.


I know, I was guessing that the sceKernelIsUserModeThread might be tricked in returning 0 if k1 was set.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Mar 21, 2009 10:39 pm    Post subject: Reply with quote

No because the type of thread (user vs. kernel) is stored in the thread's control UID block, k1 was the early mechanism for determining how the function was called to allow single functions to do different things depending on how they are called (e.g. sceIoRead will error if trying to read into kernel memory if k1 is not 0).
Back to top
View user's profile Send private message
Need2P



Joined: 21 Mar 2009
Posts: 6

PostPosted: Sat Mar 21, 2009 10:46 pm    Post subject: Reply with quote

Good idea, maybe sceKernelIsUserModeThread doesn't return weather the PRX is in user mode.

I altered my code to:
int ret = sceNetInetInit();
As it would only return 0 if it would have executed correctly which means it must be in kernel mode.

Now testing my PRX, editing when done.

The problem could also lie in my make file (I think):
USE_KERNEL_LIBS=1
is not included. I now included it.

Wait for my Edit =D

Edit: compiler is acting weird

The compiler gives me errors when I use this:
USE_KERNEL_LIBC=1
USE_KERNEL_LIBS=1

Compilers results:
Code:

psp-gcc -I/usr/local/pspdev/psp/sdk/include/libc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -D_PSP_FW_VERSION=371  -L. -L/usr/local/pspdev/psp/sdk/lib -specs=/usr/local/pspdev/psp/sdk/lib/prxspecs -Wl,-q,-T/usr/local/pspdev/psp/sdk/lib/linkfile.prx   main.o exports.o -lpspgu -lpsppower -lpspwlan -lpspdebug -lpspdisplay_driver -lpspctrl_driver -lpspsdk  -lpspkernel -o WiFi.elf
/opt/devkitPro/devkitPSP/lib/gcc/psp/4.3.3/../../../../psp/lib/crt0_prx.o: In function `_main':
/tmp/buildd/devkitpro-20090301/buildscripts/pspsdk/src/startup/crt0_prx.c:88: undefined reference to `atexit'
/tmp/buildd/devkitpro-20090301/buildscripts/pspsdk/src/startup/crt0_prx.c:94: undefined reference to `exit'
collect2: ld returned 1 exit status
make: *** [WiFi.elf] Error 1


Edit: getMode/sceNetInetInit returns something smaller than 0 meaning it doesn't work. When called twice it freezes the PSP which could be predicted =/

Im now positive that the problem lies here:
USE_KERNEL_LIBC=1
USE_KERNEL_LIBS=1


Last edited by Need2P on Sat Mar 21, 2009 11:05 pm; edited 1 time in total
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Mar 21, 2009 10:59 pm    Post subject: Reply with quote

Quote:
I altered my code to:
int ret = sceNetInetInit();
As it would only return 0 if it would have executed correctly which means it must be in kernel mode.


Why would you even need to test in the first place???
If the module exporting the function is 0x1000 then the function will be kernel mode, even if you call it from a user module.

And as we've now confirmed, sceKernelIsUserModeThread will return the mode of the thread that called the function, even though the function executes in kernel mode.

If you want the function to do different things depending on the mode of the calling thread (which wasn't your intention when asking this question), then I wonder if its correct to simply check k1, since that's what Sony used to do.
Back to top
View user's profile Send private message
Need2P



Joined: 21 Mar 2009
Posts: 6

PostPosted: Sat Mar 21, 2009 11:08 pm    Post subject: Reply with quote

Torch wrote:
Quote:
I altered my code to:
int ret = sceNetInetInit();
As it would only return 0 if it would have executed correctly which means it must be in kernel mode.


Why would you even need to test in the first place???
If the module exporting the function is 0x1000 then the function will be kernel mode, even if you call it from a user module.

Well it didn't work =(

I think the problem is I can't use:
USE_KERNEL_LIBS=1
without getting errors.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Mar 21, 2009 11:10 pm    Post subject: Reply with quote

Your heap size if too big for kernel mode. And don't use the net functions to detect.
Back to top
View user's profile Send private message
Need2P



Joined: 21 Mar 2009
Posts: 6

PostPosted: Sat Mar 21, 2009 11:18 pm    Post subject: Reply with quote

Thanks =D

Ok I changed it to:
PSP_HEAP_SIZE_KB( 64);

PSP_MAIN_THREAD_STACK_SIZE_KB( 64);
as that's the default.

What functions would I then use to detect whether I'm in kernel mode?

I want to detect whether I'm in kernel mode or not because If I code some complicated function and it returns an error I want to be sure that it's not because I'm in user mode.

Testing the PRX with smaller sizes now

Edit: no difference
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Mar 22, 2009 4:32 am    Post subject: Reply with quote

Your PRX is ALWAYS in kernel mode. Thats what 0x1000 means.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Mar 22, 2009 6:13 am    Post subject: Reply with quote

The exports is the only way to call the prx, and the way it's defined assures that you'll only get syscalls into kernel mode when calling the prx.
Back to top
View user's profile Send private message AIM Address
Need2P



Joined: 21 Mar 2009
Posts: 6

PostPosted: Sun Mar 22, 2009 1:55 pm    Post subject: Reply with quote

Quote:
Your PRX is ALWAYS in kernel mode. Thats what 0x1000 means.


Quote:
The exports is the only way to call the prx, and the way it's defined assures that you'll only get syscalls into kernel mode when calling the prx.


Ohhhhhhh, I get it 0.0

Thanks! it must have been a mental short cut of mine =D

Sorry, I'm kind of stupid =/
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Mar 22, 2009 4:13 pm    Post subject: Reply with quote

Don't worry about it - prx's are a bit hard to grasp at first compared to more "normal" libraries. They're about one level lower down than most folks are used to dealing with. After you get it, writing and using them becomes easier.
Back to top
View user's profile Send private message AIM Address
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