Exporting functions from user mode prx to kernel mode prx

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

Moderators: cheriff, TyRaNiD

Post Reply
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Exporting functions from user mode prx to kernel mode prx

Post by Pirata Nervo »

Is it possible or not? I have tried multiple ways and I can't seem to get it working :/

I really need to export a function from user mode as the kernel LIBC is not the same as the user LIBC and some of the functions I Need are not included in the kernel LIBC.

I have two options, create my own functions or export the function from user mode.
Image
Upgrade your PSP
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I know that 0x0001 attrib is for export a function from kernel to kernel or user to user, and 0x4001 for export kernel to user!
I've never tried to export a function from a user to a kernel prx module...
Have you tried with these flags?
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Where do I use them?
The Only place I think of is the mode attribute
Image
Upgrade your PSP
phobox
Posts: 127
Joined: Mon Mar 24, 2008 6:22 pm

Post by phobox »

in the exports file.
PSP_EXPORT_START(libName, 0, 0x4001)

the 3rd param is where to put your flags
Ciao! from Italy
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

phobox wrote:in the exports file.
PSP_EXPORT_START(libName, 0, 0x4001)

the 3rd param is where to put your flags
Thats only useful if he wants to use functions exported by a PRX which he made. It looks like he wants to call other sce user mode functions from the firmware PRX, from inside a kernel PRX.

I think the only way will be to use setddrmemoryprotection function to make the kernel memory as user mode, and then call your user mode function by getting the function address or something. You can't use any kernel function in that time. I'm just guessing, I'm no expert at this.

Generally it only causes various problems. User mode function should be called from user mode prx only, thats it.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

oops forgot about that.
Yeah it's currently set for kernel to user.

so 0x0001 is kernel > kernel and 0x4001 is kernel -> user, then 0x4000 should be user -> user ? tried it and did not work either

EDit:
torch I am exporting from a prx i made to another prx I made :)
Image
Upgrade your PSP
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

I guess you could do it like this:

Code the original function in a kernel PRX as kernel to kernel export. Then make a wrapper function for it in the same PRX as a kernel to user export.

Then you can call the kernel version from other kernel PRXs, as well as the user wrapper from other user PRXs.

Its impossible to directly call a kernel function from a user prx, so I assume the reverse is true as well. When you have a kernel to user export, it uses syscalls. I don't know if user to kernel export is possible like the way you want..
phobox
Posts: 127
Joined: Mon Mar 24, 2008 6:22 pm

Post by phobox »

Pirata Nervo wrote: so 0x0001 is kernel > kernel and 0x4001 is kernel -> user, then 0x4000 should be user -> user ?
not exactely..
i think everybody should have read http://ps2dev.org/psp/Tutorials/PSP_Mod ... s.download
Ciao! from Italy
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

0x0001 means direct jump to address. It works for user to user or kernel to kernel I think.

0x4001 means use syscall. AFAIK this is only for calling a kernel to user export from a user prx.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I think you haven't read my first pos torch.
kernel LIBC does not have fopen, fwrite, fclose, fprintf, etc and I need them for my function as the library I am using makes of those functions.

The only way to use that function is exporting from a user prx to a kernel prx (which needs kernel mode to work).

Of course I can the user prx to execute the function when it gets loaded but that's not what I want.

@phobox I already read that file some time ago :) but of course I don't remember everything
Image
Upgrade your PSP
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Does it compiles with USE_KERNEL_LIBC=0 ? That might provide the functions you need.

Are you writing a plugin or an EBOOT?
You only need to worry about user modules if you are writing a plugin.

If you are writing an EBOOT, the correct way IS to have the function executed from a user module. All your main working code should be user mode. You only need to load a kernel prx to do, well, kernel stuff.

If you are writing a plugin however, then you have no choice but to use various tricks to get user mode code working.

Have the user module continuously 'poll' the kernel prx for various commands. I mean export a function get_commands() from your kernel prx. The user prx should keep calling this function to see if the kernel prx needs something done.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I am writing a prx of course, otherwise I would not need to export a user function from a user prx and I could do it a from a EBOOT.PBP (as it must be user mode to be executed).

graphics.prx is the user prx.
patchexit_driver.prx is the kernel prx.

patchexit_driver.prx works perfectly since NervOS 2.1 (it's a shell) but now for 2.3 I am working on some new features and I need patchexit_driver.prx to call a user mode function.
patchexit_driver.prx only works with KERNEL_LIBC and KERNEL_LIBS set to 1.
This means I cannot use the stdio.h functions. I have already created the stdio.c and stdio.h and malloc.c and malloc.h with the necessary functions but I still need time() and I couldn't find time.c anywhere so I gave up and deleted my new code.

I am trying graphics.prx in kernel mode with user LIBC but with kernel LIBS and the only error I am getting is:

Code: Select all

/usr/local/pspdev/lib/gcc/psp/4.3.1/../../../../psp/lib/libc.a(_sbrk.o): In function `_sbrk':
../../../../../../newlib/libc/sys/psp/libcglue.c:549: undefined reference to `sceKernelMaxFreeMemSize'
collect2: ld returned 1 exit status
make: *** [graphics.elf] Error 1
I've tried adding -lpspsysmem_user/kernel to my LIBS and using the .a and .h that comes with the m33 sdk but didn't work.

Edit:
adding -lc as the first library worked. :) time to test the prx
Image
Upgrade your PSP
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Delete the output files every time you recompile. Usually when you change the makefile, it doesn't reflect the next time you compile it unless you clean up previous output files.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

that's why I use do make clean before make :P

It exports the function :now, it crashed when doing fopen I think but no problem, I will find a way to fix it.
Thank you all
Image
Upgrade your PSP
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Can you give me you're "clean" section of the makefile?
I've tried to write one before but failed.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I am not using user mode anymore. I just managed to get graphics.prx (in kernel mode) using standard I/O functions and exporting functions at the same time.

Here's my makefile:

Code: Select all

TARGET = graphics

OBJS = main.o exports.o

PSPLIBSDIR = $(PSPSDK)/..
USE_KERNEL_LIBS=1

INCDIR = ../../../sdk/include

CFLAGS = -Os -G0 -Wall -g

CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti

ASFLAGS = $(CFLAGS)

#LDFLAGS += -mno-crt0 -nostartfiles


BUILD_PRX = 1

PRX_EXPORTS = exports.exp



#USE_KERNEL_LIBC = 1
#USE_USER_LIBC = 1



PSP_FW_VERSION = 500



LIBDIR = ../../../sdk/lib

LIBS = -lc -ljpeg -lpspkernel  -lz -lm -lpsploadexec_kernel -lpspsystemctrl_kernel -lpspmodulemgr_kernel -lpspumd -lpspkubridge



PSPSDK=$(shell psp-config --pspsdk-path)

include $(PSPSDK)/lib/build_prx.mak
I am going to do some debug now to see where it is crashing in my code. It may be crashing due to STD I/O functions
Image
Upgrade your PSP
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

I don't know if it works in latest firmwares but I remember long ago it worked using flags 0x0001, which causes functions to resolve as jumps. But that is very propense to cause inestability and finally crash, as the user functions are being executed in kernel mode and they are programmed as user functions.

I remember I did the first version of nethostfs like that, it was a kernel prx that called user functions (net ones), and that's probably the reason why it was so unstable. Later Ahman improved it and I think he used user mode.

Anyways, I don't know if since then Sony has made more checks to make this not possible.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Ok thanks moonlight. Did Ahman ever released the source for NetHostFS PC and PSP ? I know you did but I don't know about him
I managed to get the standard I/O to work anyway :)
Using a custom stdio.c and stdio.h
Image
Upgrade your PSP
KickinAezz
Posts: 328
Joined: Sun Jun 03, 2007 10:05 pm

Post by KickinAezz »

LOL!

pspiofilemgr_kernel.h.

LIBS = -lpspiofilemgr_kernel (not required since it's already in BUILD.MAK)
Intrigued by PSP system Since December 2006.
Use it more for Development than for Gaming.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

KickinAezz wrote:LOL!

pspiofilemgr_kernel.h.

LIBS = -lpspiofilemgr_kernel (not required since it's already in BUILD.MAK)
what the fu*k are you talking about?
I guess you did not read all posts otherwise you would have not said what you said.

THE STANDARD I/O FUNCTIONS ARE CRASHING. (this means it's stdio.c/h)
They use sceIo functions yes but they got a #ifdef before which makes the functions not being able to be used when LIBC or LIBC (i dont know) are set for kernel. So now I removed the necessary #ifdef #endif from the custom stdio.c and stdio.h I have here and it works.

I hope you can read it better now.
Image
Upgrade your PSP
whistler
Posts: 39
Joined: Tue Mar 04, 2008 7:08 am

Post by whistler »

Torch wrote:Can you give me you're "clean" section of the makefile?
I've tried to write one before but failed.
it's already in build.mak. you can also use make rebuild which is quicker than typing make clean, make
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I thought he meant "clean" as makefile without unneeded things lol.
not the clean "section"
"make clean" is a linux command Torch. Sorry for misunderstanding

Edit:
you can also use this:

Code: Select all

all: myfile.prx
	rm -f /home/username/project/*.o
this will clean all .o files in the specified path everytime you hit make.

rm means remove and the option -f means force. myfile.prx is your prx file.
Image
Upgrade your PSP
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

No I meant sometimes the makefile has a section "clean:' just like "all:" which is used to remove specific files if a general rm -f *.o isn't sufficient.
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

A simple make clean remove all the objs file, the target and the extra targets like pbp's and related sfo...
Last edited by ne0h on Tue Nov 04, 2008 12:25 am, edited 1 time in total.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

@torch I think you cannot use clean: anymore.
Image
Upgrade your PSP
Post Reply