PRX doesn't have Kernel Priviliges

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

Moderators: cheriff, TyRaNiD

Post Reply
Need2P
Posts: 6
Joined: Sat Mar 21, 2009 5:38 pm

PRX doesn't have Kernel Priviliges

Post by Need2P »

Here is the PRX:

Code: Select all

#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&#40;"TestPRX", 0x1000, 0, 2&#41;;

PSP_MAIN_THREAD_ATTR&#40;0&#41;;

PSP_HEAP_SIZE_KB&#40; 2048&#41;;

PSP_MAIN_THREAD_STACK_SIZE_KB&#40; 2048&#41;;

int main&#40;int argc, char **argv&#41;
&#123;
	return 1;
&#125;

int Test&#40;&#41;
&#123;
	return 1;
&#125;

int getMode&#40;&#41;
&#123;
	u32 k1;

	k1 = pspSdkSetK1&#40;0&#41;; 

	int ret = sceKernelIsUserModeThread&#40;&#41;;

	pspSdkSetK1&#40;k1&#41;;	

	return ret; 
&#125;



Here is the PRX makefile:

Code: Select all

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 = $&#40;srcdir&#41;
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
And the Exports:

Code: Select all

PSP_BEGIN_EXPORTS 

PSP_EXPORT_START&#40;syslib, 0, 0x8000&#41;
PSP_EXPORT_FUNC&#40;module_start&#41;
PSP_EXPORT_VAR&#40;module_info&#41;
PSP_EXPORT_END 

# Name
PSP_EXPORT_START&#40;TestPRX, 0, 0x4001&#41; 

# Functions
PSP_EXPORT_FUNC&#40;Test&#41;
PSP_EXPORT_FUNC&#40;getMode&#41;

PSP_EXPORT_END

PSP_END_EXPORTS 
How I load the PRX:

Code: Select all

SceUID mod = pspSdkLoadStartModule&#40;"TestPRX.prx", PSP_MEMORY_PARTITION_KERNEL&#41;;
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?
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

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?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

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.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

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.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

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).
Need2P
Posts: 6
Joined: Sat Mar 21, 2009 5:38 pm

Post by Need2P »

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: Select all

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&#58; In function `_main'&#58;
/tmp/buildd/devkitpro-20090301/buildscripts/pspsdk/src/startup/crt0_prx.c&#58;88&#58; undefined reference to `atexit'
/tmp/buildd/devkitpro-20090301/buildscripts/pspsdk/src/startup/crt0_prx.c&#58;94&#58; undefined reference to `exit'
collect2&#58; ld returned 1 exit status
make&#58; *** &#91;WiFi.elf&#93; 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.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

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.
Need2P
Posts: 6
Joined: Sat Mar 21, 2009 5:38 pm

Post by Need2P »

Torch wrote:
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.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Your heap size if too big for kernel mode. And don't use the net functions to detect.
Need2P
Posts: 6
Joined: Sat Mar 21, 2009 5:38 pm

Post by Need2P »

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
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Your PRX is ALWAYS in kernel mode. Thats what 0x1000 means.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
Need2P
Posts: 6
Joined: Sat Mar 21, 2009 5:38 pm

Post by Need2P »

Your PRX is ALWAYS in kernel mode. Thats what 0x1000 means.
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 =/
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
Post Reply